forked from ecthros/102
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
executable file
·84 lines (68 loc) · 2.38 KB
/
log.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python3
import json
import gspread
import argparse
import os
from oauth2client.client import SignedJwtAssertionCredentials
# Finds the first empty row of the worksheet.
def find_next_row(worksheet):
val_list = worksheet.col_values(1)
row_num = 1
for row in val_list:
if row == '':
return row_num
row_num += 1
return row_num
#Authenticates the user to be able to use the sheet
def authenticate(key):
json_key = json.load(open(key))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
gc = gspread.authorize(credentials)
return gc
def main():
#parse arguments
parser = argparse.ArgumentParser(prog="log")
parser.add_argument("-k", "--key", required=True, help="The json key file needed for authentication")
parser.add_argument("-s", "--sheet", required=True, help="The URL of the google sheet")
parser.add_argument("-d", "--data", required=True, help="Data to update the sheet with, separated by commas")
parser.add_argument("-w", "--worksheet", help="Worksheet name - only if you are using a sheet that isn't the first sheet")
parser.add_argument("-D", "--delimiter", help="Delimiter if you want to use something other than commas")
args = parser.parse_args()
#just in case... we'll write it ti this file.
file = open("/logging/log.txt", "a+")
file.write(args.data)
file.write("\n")
file.close()
try:
auth = authenticate(str(args.key))
spread = auth.open_by_url(str(args.sheet))
if(args.worksheet == None):
wks = spread.sheet1
else:
wks = spread.worksheet(str(args.worksheet))
startCol = 'A'
startRow = str(find_next_row(wks))
wrap = 0
if(args.delimiter == None):
for segment in args.data.split(','):
if(startCol == 'Z'):
wrap = 1
wks.update_acell(str(startCol) + startRow, segment)
startCol = 'A'
elif (wrap == 1):
wks.update_acell("A" + str(startCol) + startRow, segment)
startCol = chr(ord(startCol) + 1)
else:
wks.update_acell(str(startCol) + startRow, segment)
startCol = chr(ord(startCol) + 1)
else:
for segment in args.data.split(args.delimiter):
wks.update_acell(str(startCol) + startRow, segment)
startCol = chr(ord(startCol) + 1)
print("Successfully updated!")
#ICOF
except Exception as e:
print("Error:")
print(e)
main()