Added connectivity check in iCalendar section
This commit is contained in:
		| @@ -91,6 +91,13 @@ def main(): | |||||||
|                 ImageDraw.Draw(space).text((x, y), text, fill='black', font=font) |                 ImageDraw.Draw(space).text((x, y), text, fill='black', font=font) | ||||||
|                 image.paste(space, tuple) |                 image.paste(space, tuple) | ||||||
|  |  | ||||||
|  |             def internet_available(): | ||||||
|  |                 try: | ||||||
|  |                     urlopen('https://google.com',timeout=5) | ||||||
|  |                     return True | ||||||
|  |                 except URLError as err: | ||||||
|  |                     return False | ||||||
|  |  | ||||||
|             """Connect to Openweathermap API and fetch weather data""" |             """Connect to Openweathermap API and fetch weather data""" | ||||||
|             if top_section is "Weather" and api_key != "" and owm.is_API_online() is True: |             if top_section is "Weather" and api_key != "" and owm.is_API_online() is True: | ||||||
|                 print("Connecting to Openweathermap API servers...") |                 print("Connecting to Openweathermap API servers...") | ||||||
| @@ -164,7 +171,7 @@ def main(): | |||||||
|             """Using the built-in calendar to generate the monthly Calendar |             """Using the built-in calendar to generate the monthly Calendar | ||||||
|             template""" |             template""" | ||||||
|             cal = calendar.monthcalendar(time.year, time.month) |             cal = calendar.monthcalendar(time.year, time.month) | ||||||
|                  |  | ||||||
|             if middle_section is "Calendar": |             if middle_section is "Calendar": | ||||||
|                 """Add the icon with the current month's name""" |                 """Add the icon with the current month's name""" | ||||||
|                 image.paste(im_open(mpath+str(time.strftime("%B")+'.jpeg')), monthplace) |                 image.paste(im_open(mpath+str(time.strftime("%B")+'.jpeg')), monthplace) | ||||||
| @@ -278,7 +285,6 @@ def main(): | |||||||
|             if middle_section is "Calendar" or "Agenda": |             if middle_section is "Calendar" or "Agenda": | ||||||
|                 """Algorithm for filtering and sorting events from your |                 """Algorithm for filtering and sorting events from your | ||||||
|                 iCalendar/s""" |                 iCalendar/s""" | ||||||
|                 print('Fetching events from your calendar'+'\n') |  | ||||||
|                 events_this_month = [] |                 events_this_month = [] | ||||||
|                 upcoming = [] |                 upcoming = [] | ||||||
|                 now = arrow.now() |                 now = arrow.now() | ||||||
| @@ -288,44 +294,52 @@ def main(): | |||||||
|                 to filter events in that range""" |                 to filter events in that range""" | ||||||
|                 agenda_max_days = arrow.now().replace(days=+22) |                 agenda_max_days = arrow.now().replace(days=+22) | ||||||
|                 calendar_max_days = arrow.now().replace(days=+int(events_max_range)) |                 calendar_max_days = arrow.now().replace(days=+int(events_max_range)) | ||||||
|                 for icalendars in ical_urls: |                 if internet_available() is True: | ||||||
|                     decode = str(urlopen(icalendars).read().decode()) |                     print('Internet connection test passed'+'\n') | ||||||
|                     beginAlarmIndex = 0 |                     print('Fetching events from your calendar'+'\n') | ||||||
|                     while beginAlarmIndex >= 0: |                     for icalendars in ical_urls: | ||||||
|                         beginAlarmIndex = decode.find('BEGIN:VALARM') |                         decode = str(urlopen(icalendars).read().decode()) | ||||||
|                         if beginAlarmIndex >= 0: |                         beginAlarmIndex = 0 | ||||||
|                             endAlarmIndex = decode.find('END:VALARM') |                         while beginAlarmIndex >= 0: | ||||||
|                             decode = decode[:beginAlarmIndex] + decode[endAlarmIndex+12:] |                             beginAlarmIndex = decode.find('BEGIN:VALARM') | ||||||
|                     ical = Calendar(decode) |                             if beginAlarmIndex >= 0: | ||||||
|                     for events in ical.events: |                                 endAlarmIndex = decode.find('END:VALARM') | ||||||
|                         if re.search('RRULE',str(events)) is not None: |                                 decode = decode[:beginAlarmIndex] + decode[endAlarmIndex+12:] | ||||||
|                             r = re.search('RRULE:(.+?)\n',str(events)) |                         ical = Calendar(decode) | ||||||
|                             r_start = re.search('DTSTART:(.+?)\n',str(events)) |                         for events in ical.events: | ||||||
|                             if r_start is not None: # if r_start is None the format of DTSTART is not recognized |                             if re.search('RRULE',str(events)) is not None: | ||||||
|                                 if time.now().month == 12: |                                 r = re.search('RRULE:(.+?)\n',str(events)) | ||||||
|                                     r_string=(r.group(1).rstrip()+';UNTIL='+'%04d%02d%02d'+'T000000Z') % (time.now().year+1, 1, 1) |                                 r_start = re.search('DTSTART:(.+?)\n',str(events)) | ||||||
|                                 else: |                                 if r_start is not None: # if r_start is None the format of DTSTART is not recognized | ||||||
|                                     r_string=(r.group(1).rstrip()+';UNTIL='+'%04d%02d%02d'+'T000000Z') % (time.now().year, time.now().month+1, 1) |                                     if time.now().month == 12: | ||||||
|                                 rule=rrulestr(r_string,dtstart=parse(r_start.group(1))) |                                         r_string=(r.group(1).rstrip()+';UNTIL='+'%04d%02d%02d'+'T000000Z') % (time.now().year+1, 1, 1) | ||||||
|                                 for i in rule: |                                     else: | ||||||
|                                     if i.year == time.now().year and i.month == time.now().month and i.day >= time.now().day: |                                         r_string=(r.group(1).rstrip()+';UNTIL='+'%04d%02d%02d'+'T000000Z') % (time.now().year, time.now().month+1, 1) | ||||||
|                                         upcoming.append(events) |                                     rule=rrulestr(r_string,dtstart=parse(r_start.group(1))) | ||||||
|                                         if i.day not in events_this_month: |                                     for i in rule: | ||||||
|                                             events_this_month.append(i.day) |                                         if i.year == time.now().year and i.month == time.now().month and i.day >= time.now().day: | ||||||
|                                    # uncomment this line to see fetched recurring events |                                             upcoming.append(events) | ||||||
|                                    #print ("Appended recurring event: " + events.name + " on " + str(time.now().year) + " " + time.now().strftime('%m')+ " " + str(i.day).zfill(2)) |                                             if i.day not in events_this_month: | ||||||
|                         else: |                                                 events_this_month.append(i.day) | ||||||
|                             if events.begin.date().year == today.year and events.begin.date().month is today.month and int((events.begin).format('D')) not in events_this_month: |                                        # uncomment this line to see fetched recurring events | ||||||
|                                 events_this_month.append(int((events.begin).format('D'))) |                                        #print ("Appended recurring event: " + events.name + " on " + str(time.now().year) + " " + time.now().strftime('%m')+ " " + str(i.day).zfill(2)) | ||||||
|                             if middle_section is 'Agenda' and events in ical.timeline.included(now, agenda_max_days): |                             else: | ||||||
|                                 upcoming.append(events) |                                 if events.begin.date().year == today.year and events.begin.date().month is today.month and int((events.begin).format('D')) not in events_this_month: | ||||||
|                             if middle_section is 'Calendar' and events in ical.timeline.included(now, calendar_max_days): |                                     events_this_month.append(int((events.begin).format('D'))) | ||||||
|                                 upcoming.append(events) |                                 if middle_section is 'Agenda' and events in ical.timeline.included(now, agenda_max_days): | ||||||
|  |                                     upcoming.append(events) | ||||||
|  |                                 if middle_section is 'Calendar' and events in ical.timeline.included(now, calendar_max_days): | ||||||
|  |                                     upcoming.append(events) | ||||||
|  |  | ||||||
|                 def event_begins(elem): |                     def event_begins(elem): | ||||||
|                     return elem.begin |                         return elem.begin | ||||||
|  |  | ||||||
|  |                     upcoming.sort(key=event_begins) | ||||||
|  |  | ||||||
|  |                 else: | ||||||
|  |                     print("Could not fetch events from your iCalendar.") | ||||||
|  |                     print("Either the internet connection is too slow or we're offline.") | ||||||
|  |  | ||||||
|                 upcoming.sort(key=event_begins) |  | ||||||
|  |  | ||||||
|                 if middle_section is 'Agenda': |                 if middle_section is 'Agenda': | ||||||
|                     """For the agenda view, create a list containing dates and events of the next 22 days""" |                     """For the agenda view, create a list containing dates and events of the next 22 days""" | ||||||
| @@ -359,7 +373,7 @@ def main(): | |||||||
|                             write_text(384, 25, agenda_list[lines]['value'], agenda_view_lines['line'+str(lines+1)], alignment='left') |                             write_text(384, 25, agenda_list[lines]['value'], agenda_view_lines['line'+str(lines+1)], alignment='left') | ||||||
|                         else: |                         else: | ||||||
|                             write_text(384, 25, agenda_list[lines]['value'], agenda_view_lines['line'+str(lines+1)]) |                             write_text(384, 25, agenda_list[lines]['value'], agenda_view_lines['line'+str(lines+1)]) | ||||||
|        |  | ||||||
|             if middle_section is 'Calendar': |             if middle_section is 'Calendar': | ||||||
|                 """Draw smaller squares on days with events""" |                 """Draw smaller squares on days with events""" | ||||||
|                 for numbers in events_this_month: |                 for numbers in events_this_month: | ||||||
| @@ -394,7 +408,7 @@ def main(): | |||||||
|                     for events in range(len(upcoming)): |                     for events in range(len(upcoming)): | ||||||
|                         write_text(314, 25, (upcoming[events]['event']), event_positions['e'+str(events+3)], alignment = 'left') |                         write_text(314, 25, (upcoming[events]['event']), event_positions['e'+str(events+3)], alignment = 'left') | ||||||
|  |  | ||||||
|              |  | ||||||
|             """ |             """ | ||||||
|             Map all pixels of the generated image to red, white and black |             Map all pixels of the generated image to red, white and black | ||||||
|             so that the image can be displayed 'correctly' on the E-Paper |             so that the image can be displayed 'correctly' on the E-Paper | ||||||
| @@ -409,7 +423,7 @@ def main(): | |||||||
|             if display_colours is "bw": |             if display_colours is "bw": | ||||||
|                 buffer[np.logical_and(r > 240, g > 240)] = [255,255,255] #white |                 buffer[np.logical_and(r > 240, g > 240)] = [255,255,255] #white | ||||||
|                 buffer[g < 255] = [0,0,0] #black |                 buffer[g < 255] = [0,0,0] #black | ||||||
|              |  | ||||||
|             improved_image = Image.fromarray(buffer).rotate(270, expand=True) |             improved_image = Image.fromarray(buffer).rotate(270, expand=True) | ||||||
|             print('Initialising E-Paper Display') |             print('Initialising E-Paper Display') | ||||||
|             epd.init() |             epd.init() | ||||||
| @@ -427,7 +441,7 @@ def main(): | |||||||
|             if bottom_section is 'RSS': |             if bottom_section is 'RSS': | ||||||
|                 del rss_feed |                 del rss_feed | ||||||
|                 del news |                 del news | ||||||
|                  |  | ||||||
|             if middle_section is 'Agenda': |             if middle_section is 'Agenda': | ||||||
|                 del agenda_list |                 del agenda_list | ||||||
|  |  | ||||||
| @@ -450,7 +464,7 @@ def main(): | |||||||
|                 for update_times in timings: |                 for update_times in timings: | ||||||
|                     if update_times >= mins: |                     if update_times >= mins: | ||||||
|                         sleep_for_minutes = update_times - mins |                         sleep_for_minutes = update_times - mins | ||||||
|                  |  | ||||||
|                 next_update_countdown = sleep_for_minutes*60 + (60-seconds) |                 next_update_countdown = sleep_for_minutes*60 + (60-seconds) | ||||||
|  |  | ||||||
|                 print(sleep_for_minutes,'Minutes and ', (60-seconds),'Seconds left until next loop') |                 print(sleep_for_minutes,'Minutes and ', (60-seconds),'Seconds left until next loop') | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user