Upload 2 files
Browse files- google_drive.py +29 -0
- google_sheet.py +29 -0
google_drive.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from googleapiclient.discovery import build
|
2 |
+
from googleapiclient.http import MediaFileUpload
|
3 |
+
from google.oauth2 import service_account
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
|
7 |
+
def upload_to_drive(local_path, filename):
|
8 |
+
creds_dict = json.loads(os.environ["GOOGLE_CREDS_JSON"])
|
9 |
+
creds = service_account.Credentials.from_service_account_info(creds_dict)
|
10 |
+
service = build("drive", "v3", credentials=creds)
|
11 |
+
|
12 |
+
file_metadata = {
|
13 |
+
"name": filename,
|
14 |
+
"parents": ["1RQx-f1UGOAY248bCIRasenJiQCHsD4Eq"]
|
15 |
+
}
|
16 |
+
media = MediaFileUpload(local_path, mimetype="application/zip")
|
17 |
+
try:
|
18 |
+
uploaded_file = service.files().create(
|
19 |
+
body=file_metadata,
|
20 |
+
media_body=media,
|
21 |
+
fields="id"
|
22 |
+
).execute()
|
23 |
+
except Exception as e:
|
24 |
+
import traceback
|
25 |
+
st.error("Upload failed:")
|
26 |
+
st.code(traceback.format_exc())
|
27 |
+
raise
|
28 |
+
|
29 |
+
return uploaded_file.get("id")
|
google_sheet.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ========================
|
2 |
+
# Google Sheets Integration
|
3 |
+
# ========================
|
4 |
+
import json
|
5 |
+
import gspread
|
6 |
+
import os
|
7 |
+
import pandas as pd
|
8 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
9 |
+
|
10 |
+
def connect_to_sheet():
|
11 |
+
creds_dict = json.loads(os.environ["GOOGLE_CREDS_JSON"])
|
12 |
+
scope = [
|
13 |
+
"https://spreadsheets.google.com/feeds",
|
14 |
+
"https://www.googleapis.com/auth/spreadsheets",
|
15 |
+
"https://www.googleapis.com/auth/drive"
|
16 |
+
]
|
17 |
+
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
18 |
+
client = gspread.authorize(creds)
|
19 |
+
sheet = client.open("Hackathon Leaderboard").sheet1
|
20 |
+
return sheet
|
21 |
+
|
22 |
+
def append_score(timestamp, score, filename):
|
23 |
+
sheet = connect_to_sheet()
|
24 |
+
sheet.append_row([timestamp, score, filename])
|
25 |
+
|
26 |
+
def fetch_leaderboard():
|
27 |
+
sheet = connect_to_sheet()
|
28 |
+
data = sheet.get_all_records()
|
29 |
+
return pd.DataFrame(data)
|