Skip to content

Commit

Permalink
tools/upload_sheet.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nya3jp committed Jul 9, 2015
1 parent a7e715b commit c879fba
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions tools/upload_sheet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/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> <title>
"""

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:
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(0)
reader = csv.reader(f, dialect)
return list(reader)


def upload_main(argv):
if len(argv) != 3:
return __doc__

path, title = argv[1:]
title = title.decode('utf-8')

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.')

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

print 'https://docs.google.com/spreadsheets/d/%s/edit' % spreadsheet.key


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)))

0 comments on commit c879fba

Please sign in to comment.