-
-
Save leah/a1caaa73acb69a3807d9 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
import json | |
import urllib2 | |
import base64 | |
def main(): | |
# Auth info | |
username = raw_input('Username: ') | |
password = raw_input('Password: ') | |
if not username and password: | |
print 'Username and password required for export.' | |
return | |
base64string = base64.encodestring('%s:%s' % (username, password))[:-1] | |
# Helper to fetch from API | |
def get_resp(url): | |
req = urllib2.Request(url) | |
req.add_header("Authorization", "Basic %s" % base64string) | |
r = urllib2.urlopen(req) | |
raw = r.read() | |
resp = json.loads(raw) | |
r.close() | |
return resp, raw | |
# Helper to get all messages for a channel | |
def get_messages(channel, until_id=None): | |
print '.' | |
url = 'https://grove.io/api/channels/%s/messages' % channel.get('id') | |
if until_id: | |
url = '%s?until_id=%s' % (url, until_id) | |
resp, raw = get_resp(url) | |
if not resp: | |
return | |
# Save messages to file | |
f = open('%s.json' % channel.get('name'), 'w') | |
f.write(raw) | |
f.close() | |
# Moare messages! | |
until = resp[0].get('id') | |
print 'until_id:', until | |
get_messages(channel, until_id=until) | |
resp, raw = get_resp('https://grove.io/api/auth') | |
orgs = resp.get('organizations') | |
print 'Found %s organization(s) to export.' % len(orgs) | |
for org in orgs: | |
print '\nExporting organization:', org.get('name') | |
try: | |
resp, raw = get_resp('https://grove.io/api/organizations/%s' % org.get('id')) | |
except urllib2.HTTPError, ex: | |
print 'Error: most likely this org has been suspended.' | |
continue | |
channels = resp.get('channels') | |
print 'Found %s channel(s) to export.' % len(channels) | |
for channel in channels: | |
print 'Exporting channel:', channel.get('irc_name') | |
get_messages(channel) | |
if __name__ == '__main__': | |
print 'Exporting Grove data.\n' | |
main() | |
print '\nExport completed.' |
Line 37 actually overwrites the dump on each pagination. It should use 'a'
for append. Got a working fork here: https://gist.github.com/d3eb3c06d6eb0c741684
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
as noted by bilalhusain, line 10 does not do what leah intends for it to do. password may still be blank.