-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import gspread, base64, os | ||
|
||
sizes = {} | ||
|
||
with open('auth.json', 'w', encoding='utf-8') as f: | ||
f.write(base64.b64decode(os.getenv('AUTH')).decode('utf-8')) | ||
client = gspread.service_account('auth.json') | ||
os.remove('auth.json') | ||
sheet = client.open_by_key(os.getenv('SHEET')) | ||
|
||
def get(name): | ||
global size | ||
records = sheet.worksheet(name).get_all_records() | ||
sizes[name] = len(records) | ||
return records | ||
|
||
def set(name, records, header=None): | ||
global sizes | ||
header = header or list(records[0].keys()) | ||
size = sizes[name] if name in sizes else 0 | ||
cells = [header] | ||
if name in sizes: | ||
cells += [['' for i in range(len(header))] for i in range(sizes[name])] | ||
size = 0 | ||
|
||
for record in records: | ||
size += 1 | ||
if size == len(cells): | ||
cells.append([''] * len(header)) | ||
for key, value in record.items(): | ||
if key in header: | ||
cells[size][header.index(key)] = value | ||
|
||
sizes[name] = size | ||
sheet.worksheet(name).update(cells) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
from keep_alive import keep_alive | ||
import data | ||
|
||
records = data.get('TEST') | ||
records.append({'test': 'g', 'test1': 'h'}) | ||
records[1]['fsd'] = 'q' | ||
print(records) | ||
data.set('TEST', records) | ||
|
||
keep_alive() |