-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglogger.py
48 lines (44 loc) · 1.86 KB
/
glogger.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
import gspread
import datetime
from oauth2client.service_account import ServiceAccountCredentials
import traceback
import time
import math
import random
import logging
class GSheetLogger:
def __init__(self, google_sheets_credentials_filename, spreadsheet_id):
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
self.credentials = ServiceAccountCredentials.from_json_keyfile_name(google_sheets_credentials_filename, scope)
self.spreadsheet_id = spreadsheet_id
def log(self, worksheet, params, depth=0):
try:
if(depth > 0):
time.sleep(math.pow(2, depth)+random.random())
gc = gspread.authorize(self.credentials)
sheet = gc.open_by_key(self.spreadsheet_id).worksheet(worksheet)
sheet_keys = sheet.row_values(1)
params = {k.lower(): v for k, v in params.items()}
values = list()
for key in sheet_keys:
key = key.lower()
if key == "date":
val = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
elif key in params:
val = params[key]
else:
val = "?"
if type(val) == list:
values.append(", ".join(val))
else:
values.append(val)
sheet.append_row(values, insert_data_option="INSERT_ROWS")
print("Logged")
except Exception:
if(depth > 7):
logging.info("Exception while adding the row to Google Sheet")
print(traceback.format_exc())
else:
logging.info("Exception while adding the row to Google Sheet - retry #" + str(depth+1) + "/8")
self.log(worksheet, params, depth+1)