Added connectivity check in iCalendar section

This commit is contained in:
Ace 2019-05-03 20:47:36 +02:00 committed by GitHub
parent b21e23dd7b
commit fa659f17c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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...")
@ -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,6 +294,9 @@ 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))
if internet_available() is True:
print('Internet connection test passed'+'\n')
print('Fetching events from your calendar'+'\n')
for icalendars in ical_urls: for icalendars in ical_urls:
decode = str(urlopen(icalendars).read().decode()) decode = str(urlopen(icalendars).read().decode())
beginAlarmIndex = 0 beginAlarmIndex = 0
@ -327,6 +336,11 @@ def main():
upcoming.sort(key=event_begins) 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.")
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"""
if len(upcoming) is not 0: if len(upcoming) is not 0: