Created
April 29, 2018 23:40
-
-
Save vitorio/a5594f5ddd954cbfd9fc5fe1c03a947b to your computer and use it in GitHub Desktop.
Given a Storify username, write <username>.json, a <username> folder, and a <username>/<storify-slug>.json for each story
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
try: | |
import simplejson as json | |
except: | |
import json | |
import requests | |
import math | |
import sys | |
import os | |
import errno | |
if len(sys.argv) != 2: | |
sys.stderr.write('usage: python dump-storify-json.py <storify_screen_name>\n') | |
sys.exit(2) | |
STORIFY_USER = sys.argv[1] | |
try: | |
os.makedirs(STORIFY_USER) | |
except OSError as e: | |
if e.errno != errno.EEXIST: | |
sys.exit('failure creating user directory\n') | |
else: | |
sys.stderr.write('warning: user directory exists, contents will be overwritten\n') | |
API_URL = 'https://api.storify.com/v1' | |
STORIFY_API_KEY = '' | |
STORIFY_PAGINATION = 50 | |
ENDPOINT = "{0}/{1}/{2}?api_key={3}".format(API_URL, 'users', STORIFY_USER, STORIFY_API_KEY) | |
try: | |
USER_JSON = requests.get(ENDPOINT).json() | |
except: | |
sys.exit('request failure on username\n') | |
with open('{0}.json'.format(STORIFY_USER), 'w') as f: | |
json.dump(USER_JSON, f) | |
NUM_STORIES = USER_JSON['content']['stats']['stories'] | |
STORY_PAGES = int(math.ceil(NUM_STORIES / STORIFY_PAGINATION)) + 1 | |
STORIES = list() | |
for page in range(0, STORY_PAGES): | |
endpoint = "{0}/{1}/{2}?api_key={3}&per_page={4}&page={5}".format(API_URL, 'stories', STORIFY_USER, STORIFY_API_KEY, STORIFY_PAGINATION, page) | |
try: | |
stories_json = requests.get(endpoint).json() | |
except: | |
sys.exit('request failure on stories\n') | |
STORIES.extend(stories_json['content']['stories']) | |
for story in STORIES: | |
NUM_ELEMENTS = sum(story['stats']['elements'].values()) | |
ELEMENT_PAGES = int(math.ceil(NUM_ELEMENTS / STORIFY_PAGINATION)) + 1 | |
ELEMENTS = list() | |
for page in range(0, ELEMENT_PAGES): | |
endpoint = "{0}/{1}/{2}/{3}/?api_key={4}&per_page={5}&page={6}".format(API_URL, 'stories', STORIFY_USER, story['slug'], STORIFY_API_KEY, STORIFY_PAGINATION, page) | |
try: | |
elements_json = requests.get(endpoint).json() | |
except: | |
sys.exit('request failure on story elements\n') | |
ELEMENTS.extend(elements_json['content']['elements']) | |
story['elements'] = ELEMENTS | |
with open('{0}/{1}.json'.format(STORIFY_USER, story['slug']), 'w') as f: | |
json.dump(story, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment