-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgsheet_uploader.py
More file actions
56 lines (45 loc) · 1.7 KB
/
Copy pathgsheet_uploader.py
File metadata and controls
56 lines (45 loc) · 1.7 KB
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
import gspread
from pymongo import MongoClient
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
from bson.objectid import ObjectId
AUTH_JSON_PATH='private/auth.json'
GSHEET_KEY='private/spreadsheet_key'
DATABASE='crossdata-seg'
def auth_gss_client(path, scopes):
credentials = service_account.Credentials.from_service_account_file(
path)
scoped_credentials = credentials.with_scopes(scopes)
gss_client = gspread.Client(auth=scoped_credentials)
gss_client.session = AuthorizedSession(scoped_credentials)
return gss_client
def update_sheet(gss_client, key, sheet_name, exp_data):
wks = gss_client.open_by_key(key)
exists_worksheets = [item.title for item in wks.worksheets()]
if sheet_name not in exists_worksheets:
wks.add_worksheet(sheet_name, 0, 0)
sheet = wks.worksheet(sheet_name)
upload_data = [
str(exp_data['_id']),
exp_data['time']['start'],
exp_data['time']['end'],
exp_data['time']['elapsed'],
exp_data['purpose'],
str(exp_data['args']),
str(exp_data['src'])
]
sheet.insert_row(upload_data, 2)
def main():
gss_scopes = ['https://spreadsheets.google.com/feeds']
gss_client = auth_gss_client(AUTH_JSON_PATH, gss_scopes)
client = MongoClient()
db = client[DATABASE]
collect = db['runs']
test_id='5b14d5e01d41c88d0492623c'
post = collect.find_one({'_id': ObjectId(test_id)})
sheet_name = '{}.archive'.format(DATABASE)
with open(GSHEET_KEY) as f:
skey = f.read().strip()
update_sheet(gss_client, skey, sheet_name, post)
if __name__ == '__main__':
main()