This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoog.py
52 lines (46 loc) · 1.69 KB
/
goog.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
from datetime import date
from json import loads
from random import choice
import os
from requests import get
from .utils import last_sunday, extract_url
API_ENDPOINT = ("https://sheets.googleapis.com/v4/spreadsheets"
"/{sheet_id}/values/{range_}"
"?key={key}&valueRenderOption=FORMULA"
"&dateTimeRenderOption=SERIAL_NUMBER"
)
SHEET_ID = "1pJ8NdaQ8SMGVoDWx6SGKMhonkFY7s6wyYjgi-w6DEsE"
API_KEY = os.environ.get('GOOGLE_API_KEY')
DATE_OFFSET = date(1899, 12, 30).toordinal()
def get_current_dictator():
values = loads(get(API_ENDPOINT.format(
sheet_id=SHEET_ID,
range_="Schedule!A:F",
key=API_KEY
)).content)["values"]
headers = values.pop(0)
for row in values:
if date.fromordinal(row[0] + DATE_OFFSET) == last_sunday():
return Dictator(**{head:item for head, item in zip(headers, row)})
class Dictator():
def __init__(self, week, name, **kwargs):
self.name = name
self.week = date.fromordinal(week + DATE_OFFSET)
for key in ["monday", "thursday"]:
if key in kwargs:
setattr(self, key, extract_url(kwargs[key]) if kwargs[key] else
None)
else:
setattr(self, key, None)
def get_random_rest(self, vegan=False):
values = loads(get(API_ENDPOINT.format(
sheet_id=SHEET_ID,
range_="Restaurants and ratings!A:H",
key=API_KEY
)).content)["values"]
headers = values.pop(0)
if vegan:
# NOTE: Expects that vegan / not vegan in Column C.
values = [row for row in values if len(row) > 2 if row[2]]
val = choice(values)
return val[1]