-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
58 lines (47 loc) · 2.21 KB
/
helper.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
from config import structure, cards, tests, collection, deconstruct_time, sheetService, driveService, pp
from auth import authorize
import re
import datetime
from sheet import Sheet
from pymongo import UpdateOne
import time
# TODO: Construct search for sheetsAPI
def genSearchString(card, test, year, month):
return "name contains " + "'" + card + "_" + test + "_" + str(year) + "_" + str(month).zfill(2) + "'"
""" This function takes the file name and generates the epoch time so that the
the database can be more easily sorted in relation to time"""
def getEpochTime(fileName):
match = re.match(deconstruct_time, fileName)
(yr, mon, day, hr, min, sec) = match.groups()
fileTime = datetime.datetime(int(yr), int(mon), int(day), int(hr), int(min), int(sec))
epoch = datetime.datetime.utcfromtimestamp(0)
return int((fileTime - epoch).total_seconds())
def runUpdate(monthStart, yearStart, monthEnd, yearEnd):
curMonth = monthStart
curYear = yearStart
updateList = []
while(curYear < yearEnd or (curYear == yearEnd and curMonth <= monthEnd)):
for card in cards:
for test in tests:
search = genSearchString(card, test, curYear, curMonth)
results = driveService.files().list( includeTeamDriveItems = True,
supportsTeamDrives = True, pageSize = 100,
q = search, corpora = "domain").execute()
files = results["files"]
for file in files:
datetime = getEpochTime(file["name"])
curSheet = Sheet(test, card, datetime, file["id"])
updateList = updateList + curSheet.genUpdate(sheetService)
time.sleep(1.3)
curYear = curYear + 1 if curMonth == 12 else curYear
curMonth = (curMonth % 12) + 1
query = []
for update in updateList:
query.append(UpdateOne(
{
"sheetId": update["sheetId"],
"subtest": update["subtest"],
"test": update["test"],
"type": update["type"]
}, {"$set":update}, upsert = True))
collection.bulk_write(query)