forked from google/hyou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_sheet.py
executable file
·110 lines (82 loc) · 2.82 KB
/
upload_sheet.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python
#
# Copyright 2015 Google Inc. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Uploads a sheet file (CSV/TSV) to Google Spreadsheet.
Usage:
upload_sheet.py --authenticate
upload_sheet.py <filename>
"""
import csv
import os
import sys
import gflags
import hyou
import hyou.client
import oauth2client.client
CREDENTIAL_PATH = os.path.join(os.environ['HOME'], '.hyou.credential.json')
TEST_CLIENT_ID = '958069810280-th697if59r9scrf1qh0sg6gd9d9u0kts.apps.googleusercontent.com'
TEST_CLIENT_SECRET = '5nlcvd54WycOd8h8w7HD0avT'
FLAGS = gflags.FLAGS
gflags.DEFINE_bool('authenticate', False, '')
gflags.DEFINE_string('client_id', TEST_CLIENT_ID, '')
gflags.DEFINE_string('client_secret', TEST_CLIENT_SECRET, '')
gflags.MarkFlagAsRequired('client_id')
gflags.MarkFlagAsRequired('client_secret')
def load_sheet(path):
with open(path, 'rb') as f:
reader = csv.reader(f)
return list(reader)
def upload_main(argv):
if len(argv) != 2:
return __doc__
path = argv[1]
sheet = load_sheet(path)
try:
collection = hyou.login(json_path=CREDENTIAL_PATH)
except Exception:
return ('Your credential is missing, expired or invalid.'
'Please authenticate again by --authenticate.')
title = os.path.basename(path).decode('utf-8')
spreadsheet = collection.create_spreadsheet(
title, rows=len(sheet), cols=len(sheet[0]))
with spreadsheet[0] as worksheet:
for srow, trow in zip(sheet, worksheet):
for i, value in enumerate(srow):
trow[i] = value.decode('utf-8')
print spreadsheet.url
def authenticate_main(argv):
flow = oauth2client.client.OAuth2WebServerFlow(
client_id=FLAGS.client_id,
client_secret=FLAGS.client_secret,
scope=hyou.client.GOOGLE_SPREADSHEET_SCOPES)
url = flow.step1_get_authorize_url('urn:ietf:wg:oauth:2.0:oob')
print
print 'Please visit this URL to get the authorization code:'
print url
print
code = raw_input('Code: ').strip()
credentials = flow.step2_exchange(code)
with open(CREDENTIAL_PATH, 'w') as f:
os.fchmod(f.fileno(), 0600)
f.write(credentials.to_json())
print
print 'OK! Credentials were saved at %s' % CREDENTIAL_PATH
def main(argv):
if FLAGS.authenticate:
return authenticate_main(argv)
else:
return upload_main(argv)
if __name__ == '__main__':
sys.exit(main(FLAGS(sys.argv)))