Skip to content

Instantly share code, notes, and snippets.

@dbrgn
Created February 10, 2013 11:44
Show Gist options
  • Save dbrgn/4749329 to your computer and use it in GitHub Desktop.
Save dbrgn/4749329 to your computer and use it in GitHub Desktop.
Memonic export script
"""
Dump item and set data from memonic into .json files.
Please issue ``mkdir -p data/{sets,items}`` before running the script.
"""
from __future__ import print_function, division, absolute_import, unicode_literals
import json
import requests
API_KEY = '<api-key>'
API_URL = 'https://api.memonic.com/v2/'
USER_ID = '<user-uuid>'
headers = {
'Accept': 'application/json',
}
params = {
'apikey': API_KEY,
}
auth = ('<username>', '<password>')
def get_response(resource):
try:
response = requests.get(API_URL + resource, headers=headers, params=params, auth=auth)
except requests.exceptions.ConnectionError:
return None
else:
return response
if __name__ == '__main__':
# Fetch sets
resource = 'users/{0}/sets'.format(USER_ID)
response = get_response(resource)
data = response.json()
with open('data/sets.json', 'w') as f:
print('Writing sets.json...')
f.write(json.dumps(data['sets'], indent=2))
# Fetch setitems
for mset in response.json()['sets']:
resource = 'users/{0}/sets/{1}/items'.format(USER_ID, mset['id'])
response = get_response(resource)
data = response.json()
with open('data/sets/{0}.json'.format(mset['id']), 'w') as f:
print('Writing sets/{0}.json'.format(mset['id']))
f.write(json.dumps(data['items'], indent=2))
resource = 'users/{0}/items'.format(USER_ID)
response = get_response(resource)
data = response.json()
for item in data['items']:
resource = 'users/{0}/items/{1}/'.format(USER_ID, item['id'])
response = get_response(resource)
if response is None:
print('Invalid response')
continue
else:
data = response.json()
with open('data/items/{0}.json', 'w') as f:
print('Writing items/{0}.json'.format(item['id']))
f.write(json.dumps(data, indent=2))
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment