-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheets.py
40 lines (33 loc) · 1.23 KB
/
sheets.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 12 20:11:05 2017
@author: clesiemo3
"""
import os
import pygsheets
def ya_schedule():
message = '<br>'
error_message = '<br> Error: Failed to find schedule data from google sheets. <br>'
try:
gc = pygsheets.authorize(service_file='client_secret.json')
except FileNotFoundError:
with open('client_secret.json', 'w') as cs_file:
cs_file.write(os.environ['google_client_secret'])
gc = pygsheets.authorize(service_file='client_secret.json')
try:
ws = gc.open_by_key(os.environ['schedule_sheets_id']).worksheet_by_title('Teaching Schedule')
next_week = ws.get_values('A1', 'E2', include_all=True)
# ugly dict comprehension to turn 1st list into keys
nw_dict = {next_week[0][i]: next_week[1][i] for i in range(0, len(next_week[0]))}
for k, v in nw_dict.items():
if v == "":
continue
message += "%s: %s <br>" % (k, v)
except KeyError as e:
print("KeyError: %s" % e)
message = error_message
except pygsheets.PyGsheetsException as e:
print("PyGsheetsException: %s" % e)
message = error_message
return message