Ejemplo n.º 1
0
    def _html_to_ics(self, markup: str, timezone: pytz.timezone) -> Calendar:
        soup = BeautifulSoup(markup, "lxml")
        monthly_prayers = soup.find("div", {"id": "tab-1"}).table
        headers = [i.text for i in monthly_prayers.thead.tr.find_all('th')][1:]
        data = [[i.text for i in tr.find_all('td')]
                for tr in monthly_prayers.tbody.find_all('tr')]
        calendar = Calendar(creator=app.config['APPLICATION_ID'])
        for day_record in data:
            events_starts = [
                timezone.localize(
                    datetime.strptime(day_record[0] + ' ' + _time,
                                      '%d.%m.%Y %H:%M'))
                for _time in day_record[1:]
            ]
            for i in range(len(headers)):
                display_alarm = DisplayAlarm(trigger=events_starts[i],
                                             display_text=headers[i])
                audio_alarm = AudioAlarm(trigger=events_starts[i])
                audio_alarm.sound = ContentLine('ATTACH',
                                                {'FMTTYPE': ['audio/mpeg']},
                                                app.config['ADHAN_AUDIO'])

                calendar.events.add(
                    Event(name=headers[i],
                          begin=events_starts[i],
                          duration=timedelta(minutes=1),
                          alarms=[audio_alarm, display_alarm]))
        return calendar
Ejemplo n.º 2
0
def test_generate_calendar():
    test = APTest('Latin', (5, 12), (12, 0), 'idk')
    cal = test.calendar_entry()

    assert cal.name == 'AP Latin Exam'
    assert cal.begin == arrow.Arrow(2020, 5, 12, 12, 0, 0, tzinfo=ahs_timezone)
    assert cal.end == arrow.Arrow(2020, 5, 12, 12, 45, 0, tzinfo=ahs_timezone)
    assert list(cal.categories) == ['AP Exams 2020']
    assert cal.description == 'idk'
    assert list(cal.alarms) == [AudioAlarm(-datetime.timedelta(minutes=30)),
                                AudioAlarm(-datetime.timedelta(minutes=45))]
Ejemplo n.º 3
0
    def test_alarm_output(self):
        attach = "ftp://example.com/pub/sounds/bell-01.aud"
        attach_params = {"FMTTYPE": ["audio/basic"]}
        a = AudioAlarm(trigger=timedelta(minutes=15))
        a.sound = ContentLine("ATTACH", value=attach, params=attach_params)

        desired_output = CRLF.join([
            "BEGIN:VALARM",
            "ACTION:AUDIO",
            "ATTACH;FMTTYPE=audio/basic:{0}".format(attach),
            "TRIGGER:PT15M",
            "END:VALARM",
        ])

        self.assertEqual(desired_output, str(a))
Ejemplo n.º 4
0
def build_calender(time_table, alarm=15):
    routine = {
        1: ['00:00:00', '00:45:00'],
        2: ['00:50:00', '01:35:00'],
        3: ['02:00:00', '02:45:00'],
        4: ['02:50:00', '03:35:00'],
        5: ['05:30:00', '06:15:00'],
        6: ['06:20:00', '07:05:00'],
        7: ['07:30:00', '08:15:00'],
        8: ['08:20:00', '09:05:00'],
        9: ['10:30:00', '11:15:00'],
        10: ['11:20:00', '12:05:00'],
    }
    days = {'一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '日': 7}

    calendar = Calendar()
    time_detail_re = re.compile(u'星期([\u4e00-\u9fa5])(\d+)-(\d+)')
    duration_re = re.compile(r'(\d+)-(\d+)')

    term_start = datetime.strptime(time_table['开始时间'], '%Y-%m-%d').date()
    term_end = datetime.strptime(time_table['结束时间'], '%Y-%m-%d').date()

    for lesson in time_table['课表信息']:
        lesson_duration = lesson['学时分布']
        start_week, end_week = duration_re.match(lesson_duration).groups()

        for time_detail in lesson['节次信息']:
            weekday, class_start, class_end = time_detail_re.match(
                time_detail['节次']).groups()
            for week in range(int(start_week), int(end_week) + 1):
                if time_detail['时间类型'] == '单周':
                    if week % 2 != 1:
                        continue
                if time_detail['时间类型'] == '双周':
                    if week % 2 != 0:
                        continue

                event = Event()
                event.name, event.organizer, event.location \
                    = lesson['课程名称'], lesson['教师姓名'], time_detail['教室']
                a = AudioAlarm(trigger=timedelta(minutes=alarm))
                event.alarms.append(a)
                base_day = term_start + timedelta(weeks=week - 1,
                                                  days=days[weekday] - 1)
                class_start_time = datetime.strptime(
                    routine[int(class_start)][0], '%H:%M:%S').time()
                class_end_time = datetime.strptime(routine[int(class_end)][1],
                                                   '%H:%M:%S').time()

                event.begin = datetime.combine(base_day, class_start_time)
                event.end = datetime.combine(base_day, class_end_time)
                calendar.events.add(event)

    return calendar
Ejemplo n.º 5
0
    def test_alarm_output(self):
        attach = 'ftp://example.com/pub/sounds/bell-01.aud'
        attach_params = {'FMTTYPE': ['audio/basic']}
        a = AudioAlarm(trigger=timedelta(minutes=15),
                       attach=attach,
                       attach_params=attach_params)

        desired_output = CRLF.join([
            'BEGIN:VALARM', 'TRIGGER:-PT15M', 'ACTION:AUDIO',
            'ATTACH;FMTTYPE=audio/basic:{0}'.format(attach), 'END:VALARM'
        ])

        self.assertEqual(desired_output, str(a))
Ejemplo n.º 6
0
 def test_attach_repr(self):
     attach = 'ftp://example.com/pub/sounds/bell-01.aud'
     attach_params = {'FMTTYPE': ['audio/basic']}
     a = AudioAlarm(trigger=timedelta(minutes=15),
                    repeat=2,
                    duration=timedelta(minutes=10),
                    attach=attach,
                    attach_params=attach_params)
     self.assertEqual(
         repr(a),
         "<<class 'ics.alarm.AudioAlarm'> trigger:0:15:00 repeat:2 "
         "duration:0:10:00 attach:ftp://example.com/pub/sounds/bell-01.aud "
         "attach_params:{'FMTTYPE': ['audio/basic']}>")
Ejemplo n.º 7
0
 def test_plain_repr(self):
     a = AudioAlarm(trigger=timedelta(minutes=15))
     self.assertEqual(repr(a), "<AudioAlarm trigger:0:15:00>")
Ejemplo n.º 8
0
 def test_alarm(self):
     a = AudioAlarm(trigger=timedelta(minutes=15))
     self.assertEqual("AUDIO", a.action)