Last active
December 10, 2015 23:58
-
-
Save maliabadi/4512978 to your computer and use it in GitHub Desktop.
Provides a local JSON dump of a remote Google Drive spreadsheet.
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
from json import dumps | |
from gdata.spreadsheet.service import SpreadsheetsService, DocumentQuery | |
from optparse import OptionParser | |
usage = "usage: python doc2json.py --email=EMAIL --pass=PASS title=TITLE --output=OUTFILE" | |
parser = OptionParser(usage=usage) | |
parser.add_option("-e", "--email", | |
metavar="EMAIL", help="email address for doc service") | |
parser.add_option("-p", "--password", | |
metavar="PASS", help="account password") | |
parser.add_option("-t", "--title", | |
metavar="TITLE", help="spreadsheet title") | |
parser.add_option("-o", "--output", | |
metavar="OUTFILE", help="file name to write to", | |
default='output.json') | |
def entry_to_sheet_id(entry): | |
return entry.id.text.rsplit('/',1)[1] | |
def sheet_ids(entry): | |
return map(lambda y: (y.title.text, entry_to_sheet_id(y)), entry) | |
def row_to_dict(row): | |
return dict(map(lambda x: (x[0], x[1].text), row.custom.items())) | |
def safe(string): | |
if not string: | |
return | |
return string.encode('ascii', 'ignore') | |
def build_client(opts): | |
try: | |
gd_client = SpreadsheetsService() | |
gd_client.email = opts.email | |
gd_client.password = opts.password | |
gd_client.source = 'doc2json' | |
gd_client.ProgrammaticLogin() | |
return gd_client | |
except: | |
raise Exception('Error connecting to Google Spreadsheet. Double-check your email and password.') | |
def get_sheet_ids(client, opts): | |
try: | |
q = DocumentQuery() | |
q['title'] = opts.title | |
q['title-exact'] = 'true' | |
feed = client.GetSpreadsheetsFeed(query=q) | |
spreadsheet_id = feed.entry[0].id.text.rsplit('/',1)[1] | |
feed = client.GetWorksheetsFeed(spreadsheet_id) | |
return spreadsheet_id, sheet_ids(feed.entry) | |
except: | |
raise Exception('Error retrieving Document "%s"' % opts.title) | |
def dump_sheet(opts): | |
client = build_client(opts) | |
dump_dict = {} | |
spreadsheet_id, sheet_ids = get_sheet_ids(client, opts) | |
for title, sheet_id in sheet_ids: | |
dump_dict[title] = [] | |
rows = client.GetListFeed(spreadsheet_id, sheet_id).entry | |
for row in rows: | |
dump_dict[title].append(row_to_dict(row)) | |
with open(opts.output, 'w+') as jfile: | |
jfile.write(dumps(dump_dict)) | |
if __name__ == '__main__': | |
(options, args) = parser.parse_args() | |
dump_sheet(options) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment