-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheetops.py
57 lines (45 loc) · 1.74 KB
/
sheetops.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
# All the things that do something with Google Spreadsheet
import gspread
import constants
from pydantic import BaseModel, ValidationError
from typing import Optional
from datetime import datetime
gconnection = gspread.service_account(filename=constants.GOOGLESHEETSKEYFILE)
spreadsheet = gconnection.open_by_key(constants.SPREADSHEET_ID)
scores = spreadsheet.worksheet(constants.SCORES)
log = spreadsheet.worksheet(constants.LOG)
class SheetData(BaseModel):
'''Container for data used in GoogleSheets. Dataset goes through pydantic,
so it could be validated more easily.'''
datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
discordid: str
name: str
nick: Optional[str]
amount: int
body: str
status = 'nowy'
def as_row(self):
'''returns data as a list so it could be added to
Google Sheets easily'''
return [self.datetime, self.discordid, self.name, self.nick,
self.amount, self.body, self.status]
def negativevalue(self):
'''switches the amount to its negative value'''
self.amount = -self.amount
class SheetOps:
def sendentry(row):
'''adds entry to the Google Spreadsheet / logs.
Should be passed as a list [name, pts, body, status]'''
log.append_row(row)
def topscores(howmany):
'''Returns list of lists with top scores recorded'''
query = scores.get(f"A2:B{howmany+1}")
# +1 because first row reserved for titles
return query
def myscore(discord_name):
'''Returns user score'''
query = scores.get_all_records()
for entry in query:
if entry[constants.DISCORDNAME] == discord_name:
return entry[constants.TOTALUSERSCORE]
break