Spaces:
Sleeping
Sleeping
Upload main.py
Browse files
main.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import gspread
|
3 |
+
from google.oauth2.service_account import Credentials
|
4 |
+
from google.auth.exceptions import GoogleAuthError
|
5 |
+
from google.colab import userdata
|
6 |
+
|
7 |
+
# Define the Academic and Non-Academic panic buttons
|
8 |
+
academic_panic_buttons = ["MISSED CLASSES", "BACKLOGS", "LACK OF MOTIVATION", "NOT UNDERSTANDING", "BAD MARKS"]
|
9 |
+
non_academic_panic_buttons = ["EMOTIONAL FACTORS", "PROCRASTINATE", "LOST INTEREST", "LACK OF FOCUS", "GOALS NOT ACHIEVED", "LACK OF DISCIPLINE"]
|
10 |
+
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
# Function to fetch the credentials
|
14 |
+
def get_credentials_from_env():
|
15 |
+
service_account_info = {
|
16 |
+
"type": userdata.get("SERVICE_ACCOUNT_TYPE"),
|
17 |
+
"project_id": userdata.get("PROJECT_ID"),
|
18 |
+
"private_key_id": userdata.get("PRIVATE_KEY_ID"),
|
19 |
+
"private_key": userdata.get("PRIVATE_KEY").replace('\\n', '\n'),
|
20 |
+
"client_email": userdata.get("CLIENT_EMAIL"),
|
21 |
+
"client_id": userdata.get("CLIENT_ID"),
|
22 |
+
"auth_uri": userdata.get("AUTH_URI"),
|
23 |
+
"token_uri": userdata.get("TOKEN_URI"),
|
24 |
+
"auth_provider_x509_cert_url": userdata.get("AUTH_PROVIDER_X509_CERT_URL"),
|
25 |
+
"client_x509_cert_url": userdata.get("CLIENT_X509_CERT_URL"),
|
26 |
+
"universe_domain": userdata.get("UNIVERSE_DOMAIN")
|
27 |
+
}
|
28 |
+
|
29 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
30 |
+
creds = Credentials.from_service_account_info(service_account_info, scopes=scope)
|
31 |
+
return creds
|
32 |
+
|
33 |
+
# Main function to fetch the panic button occurrences
|
34 |
+
@app.get("/panic-button-occurances")
|
35 |
+
def get_panic_button_occurrences():
|
36 |
+
try:
|
37 |
+
# Set up credentials
|
38 |
+
creds = get_credentials_from_env()
|
39 |
+
client = gspread.authorize(creds)
|
40 |
+
|
41 |
+
# Open the Google Sheet
|
42 |
+
sheet = client.open_by_url('https://docs.google.com/spreadsheets/d/1nFZGkCvRV6qS-mhsORhX3dxI0JSge32_UwWgWKl3eyw/edit?gid=0#gid=0').worksheet('Sheet1')
|
43 |
+
|
44 |
+
# Get all values from the sheet
|
45 |
+
data = sheet.get_all_values()
|
46 |
+
|
47 |
+
# Initialize the lists with panic button names and 0 counts
|
48 |
+
academic_list = {button: 0 for button in academic_panic_buttons}
|
49 |
+
non_academic_list = {button: 0 for button in non_academic_panic_buttons}
|
50 |
+
|
51 |
+
# Iterate through all rows in the data
|
52 |
+
for row in data:
|
53 |
+
panic_button = row[1] # Assuming the panic button values are in the second column
|
54 |
+
if panic_button in academic_list:
|
55 |
+
academic_list[panic_button] += 1
|
56 |
+
elif panic_button in non_academic_list:
|
57 |
+
non_academic_list[panic_button] += 1
|
58 |
+
|
59 |
+
return {
|
60 |
+
"academic": academic_list,
|
61 |
+
"non_academic": non_academic_list
|
62 |
+
}
|
63 |
+
|
64 |
+
except GoogleAuthError as e:
|
65 |
+
return {"error": f"Authentication error: {e}"}
|
66 |
+
except gspread.exceptions.SpreadsheetNotFound:
|
67 |
+
return {"error": "Spreadsheet not found. Please check the URL."}
|
68 |
+
except Exception as e:
|
69 |
+
return {"error": f"An error occurred: {e}"}
|