-
Notifications
You must be signed in to change notification settings - Fork 0
/
googlesheets.py
37 lines (32 loc) · 1.23 KB
/
googlesheets.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
import gspread
from pathlib import Path
from redis import Redis
from rq import Queue
from rq import Retry
from oauth2client.service_account import ServiceAccountCredentials
def process(timestamp, datapoint):
CREDENTIALS_FILE = "key.json"
TEMPERATURE_DOC = "TemperatureLL208DY"
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive']
# Reading Credentials from ServiceAccount Keys file
credentialsPath = str(Path(__file__).parent.resolve()) + '/' + CREDENTIALS_FILE
credentials = ServiceAccountCredentials.from_json_keyfile_name(credentialsPath, scope)
# intitialize authorization object
gc = gspread.authorize(credentials)
# Open Google Sheets file
sheet = gc.open(TEMPERATURE_DOC).sheet1
try:
sheet.append_row([timestamp, datapoint])
except Exception as e: print(e)
return
def write(timestamp, datapoint, previous_job):
q = Queue(connection=Redis())
result = q.enqueue(process,
timestamp,
datapoint,
retry=Retry(max=3, interval=[60, 600, 3600]),
depends_on = previous_job)
return result