Given a set of S3 keys for a single storymap, analyze them to find the "best" JSON file. Read that file in, and "annotate" it with a list of the key names so that we can cache the data and not have to read from S3 if we make changes to what we pull out.
(keys, error_log)
| 28 | return path, filename |
| 29 | |
| 30 | def prepare_data_from_keys(keys, error_log): |
| 31 | """Given a set of S3 keys for a single storymap, analyze them to find the "best" JSON file. Read that file in, |
| 32 | and "annotate" it with a list of the key names so that we can cache the data and not have to read from S3 |
| 33 | if we make changes to what we pull out.""" |
| 34 | |
| 35 | user, title_slug = keys[0].name.split('/')[1:3] |
| 36 | |
| 37 | image_keys = [] |
| 38 | pub = draft = None |
| 39 | for k in keys: |
| 40 | if '/_images/' in k.name: |
| 41 | image_keys.append(k.name) |
| 42 | elif k.name.endswith('draft.json'): |
| 43 | draft = k |
| 44 | elif k.name.endswith('published.json'): |
| 45 | pub = k |
| 46 | |
| 47 | status = 'draft' |
| 48 | config_key = draft |
| 49 | if pub: |
| 50 | status = 'published' |
| 51 | config_key = pub |
| 52 | if config_key is None: |
| 53 | error_log.writerow(["%s/%s" % (user, title_slug), "No JSON for storymap"]) |
| 54 | return None |
| 55 | |
| 56 | try: |
| 57 | data = json.loads(config_key.get_contents_as_string()) |
| 58 | except Exception, e: |
| 59 | error_log.writerow([config_key.name, "Error loading from S3: %s" % e]) |
| 60 | return None |
| 61 | |
| 62 | # add things which we would otherwise need the entire keyset to know. |
| 63 | data['status'] = status |
| 64 | data['image_count'] = len(image_keys) |
| 65 | data['keys'] = [k.name for k in keys] |
| 66 | filename = 'draft.html' if status == 'draft' else 'index.html' |
| 67 | |
| 68 | data['url'] = 'http://uploads.knightlab.com/storymapjs/{}/{}/{}'.format(user, title_slug, filename) |
| 69 | |
| 70 | return data |
| 71 | |
| 72 | |
| 73 |