-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportGoogleDocs.py
86 lines (75 loc) · 2.65 KB
/
exportGoogleDocs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
# Copyright 2011-2013 National Evolutionary Synthesis Center (NESCent).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import getopt
import gdata.docs.service
import gdata.docs.client
import os.path
import copyGoogleDocs
def DownloadFeed(client,feed,dir):
if not feed.entry:
print 'No entries in feed\n'
for entry in feed.entry:
title=entry.title.text.encode('UTF-8')
destinationFile=os.path.join(dir, title+'.csv')
if not os.path.exists(destinationFile):
print 'Downloading spreadsheet to %s' % destinationFile
client.DownloadResource(entry, destinationFile, extra_params={'gid': 0, 'exportFormat': 'csv'}) # export the first sheet as csv
else:
print '%s already exists' % destinationFile
def usage():
print 'python exportGoogleDocs.py --user [username] --pw [password] --sheets [spreadsheet names] --download-dir [download dir]'
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], '', ['user=', 'pw=', 'sheets=', 'download-dir='])
except getopt.error, msg:
usage()
sys.exit(2)
user = None
pw = None
sheets = None
download_dir = None
# Process options
for option, arg in opts:
if option == '--user':
user = arg
elif option == '--pw':
pw = arg
elif option == '--sheets':
sheets = arg
elif option == '--download-dir':
download_dir = arg
if user is None or pw is None or sheets is None or download_dir is None:
usage()
sys.exit(2)
# Source is the application name
client = gdata.docs.client.DocsClient(source='nescent-parkNotebooks-v1')
client.ssl = True
client.http_client.debug = False
try:
client.ClientLogin(user,pw,client.source)
except gdata.service.BadAuthentication:
print "Error logging in with these credentials"
return
sheetNames = copyGoogleDocs.getDestinationNames(sheets)
for sheetName in sheetNames:
feed = client.GetResources(uri='/feeds/default/private/full/-/spreadsheet?title='+sheetName)
if not feed.entry:
print 'No spreadsheet titled '+sheetName+'\n'
else:
DownloadFeed(client,feed,download_dir)
if __name__ == '__main__':
main()