diff options
author | Ernesto Rico-Schmidt <e.rico.schmidt@gmail.com> | 2014-07-21 19:13:18 (GMT) |
---|---|---|
committer | Ernesto Rico-Schmidt <e.rico.schmidt@gmail.com> | 2014-07-21 20:51:17 (GMT) |
commit | d5394e0444461627f04407c0c85539c18be94cb1 (patch) | |
tree | f28bb3e25aa7f250e7410f9ed9a1cdfcd2102c82 /program | |
parent | f21d277b7f912eca2213852831e656f0666b559e (diff) |
handle errors better
Diffstat (limited to 'program')
-rw-r--r-- | program/utils.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/program/utils.py b/program/utils.py index cdc6a09..9a086da 100644 --- a/program/utils.py +++ b/program/utils.py @@ -2,12 +2,27 @@ from django.conf import settings import json import urllib +from os.path import join def get_automation_id_choices(): base_url = getattr(settings, 'AUTOMATION_BASE_URL', None) + cache_dir = getattr(settings, 'AUTOMATION_CACHE_DIR', 'cache') + cached_shows = join(cache_dir, 'shows.json') shows = [] if base_url: - shows_list = json.load(urllib.urlopen(base_url))['shows'] + try: + shows_json = urllib.urlopen(base_url).read() + shows_list = json.loads(shows_json)['shows'] + except IOError: + try: + with open(cached_shows) as cache: + shows_list = json.loads(cache.read())['shows'] + except IOError: + shows_list = [] + else: + with open(cached_shows, 'w') as cache: + cache.write(shows_json) + shows = [(s['id'], s['title']) for s in shows_list] return shows |