-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleSheetsManager.py
More file actions
70 lines (58 loc) · 3.02 KB
/
GoogleSheetsManager.py
File metadata and controls
70 lines (58 loc) · 3.02 KB
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
import httplib2
import os
import time
from apiclient import discovery
from oauth2client import client, tools
from oauth2client.file import Storage
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'client_ID.json'
APPLICATION_NAME = 'Felo UCI'
class GoogleSheetsManager:
def __init__(self):
self._credentials = _get_credentials()
http = self._credentials.authorize(httplib2.Http())
discovery_url = 'https://sheets.googleapis.com/$discovery/rest?version=v4'
self._SHEETS = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discovery_url)
def store_scores(self, group_name: str, scored_fencers: "[(Fencer, score),...,(Fencer, score)]"):
"""
Creates a google sheet spreadsheet with the title of Group Name: {Current Time}
and a list of scored fencers
"""
init_sheet_data = {'properties': {'title': '{}: {}'.format(group_name, time.ctime())}}
res = self._SHEETS.spreadsheets().create(body=init_sheet_data).execute()
sheet_id = res['spreadsheetId']
self._SHEETS.spreadsheets().values().update(spreadsheetId=sheet_id,
range="A1",
body={'values': [[group_name], [time.ctime()], [""],
["Name", "Score"]]},
valueInputOption='RAW').execute()
self._SHEETS.spreadsheets().values().update(spreadsheetId=sheet_id,
range="A5",
body={'values': scored_fencers},
valueInputOption='RAW').execute()
print("Created {}".format(res['properties']['title']))
def _get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'sheets.googleapis.com-python-FeloUCI.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
if __name__ == '__main__':
GoogleSheetsManager().store_scores("Felo Fencing UCI", [("Matthew", 2000), ("Marco", 2000)])