-
-
Save larsyencken/d3eb3c06d6eb0c741684 to your computer and use it in GitHub Desktop.
Grove Export
This file contains 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/env python | |
import getpass | |
import sys | |
import json | |
import requests | |
def get_messages(auth, channel): | |
filename = '%s.json' % channel.get('name') | |
print 'Exporting channel %s to %s' % (channel.get('irc_name'), filename) | |
url = 'https://grove.io/api/channels/%s/messages' % channel.get('id') | |
resp = requests.get(url, auth=auth) | |
if resp.status_code != 200: | |
raise ValueError(resp.status_code) | |
i = 0 | |
stdout = sys.stdout | |
with open(filename, 'w') as ostream: | |
while resp.json: | |
until = resp.json[0].get('id') | |
for rec in resp.json: | |
print >> ostream, json.dumps(rec) | |
stdout.write('.') | |
stdout.flush() | |
i += 1 | |
if i % 50 == 0: | |
stdout.write(str(i)) | |
stdout.flush() | |
url = 'https://grove.io/api/channels/%s/messages/?until_id=%s' % ( | |
channel.get('id'), | |
until, | |
) | |
resp = requests.get(url, auth=auth) | |
if resp.status_code != 200: | |
raise ValueError(resp.status_code) | |
def main(): | |
# Auth info | |
username = raw_input('Username: ') | |
password = getpass.getpass('Password: ') | |
if not username or not password: | |
print 'Username and password required for export.' | |
return | |
auth = (username, password) | |
resp = requests.get('https://grove.io/api/auth', auth=auth) | |
orgs = resp.json.get('organizations') | |
if not orgs: | |
print 'ERROR: no organisations found to export' | |
sys.exit(1) | |
print 'Found %s organization(s) to export.' % len(orgs) | |
for org in orgs: | |
print '\nExporting organization:', org.get('name') | |
org_url = 'https://grove.io/api/organizations/%s' % org.get('id') | |
resp = requests.get(org_url, auth=auth) | |
if resp.status_code != 200: | |
print 'Error: most likely this org has been suspended.' | |
continue | |
channels = resp.json.get('channels') | |
print 'Found %s channel(s) to export.' % len(channels) | |
for channel in channels: | |
get_messages(auth, channel) | |
if __name__ == '__main__': | |
print 'Exporting Grove data.\n' | |
main() | |
print '\nExport completed.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment