File size: 6,129 Bytes
879e72a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2469229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d335a5
 
 
 
 
 
 
 
 
 
 
2469229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
879e72a
 
 
 
 
 
 
 
 
 
2469229
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# from fastapi import FastAPI
# import gspread
# from google.oauth2.service_account import Credentials
# from google.auth.exceptions import GoogleAuthError
# import os

# # Define the Academic and Non-Academic panic buttons
# academic_panic_buttons = ["MISSED CLASSES", "BACKLOGS", "LACK OF MOTIVATION", "NOT UNDERSTANDING", "BAD MARKS"]
# non_academic_panic_buttons = ["EMOTIONAL FACTORS", "PROCRASTINATE", "LOST INTEREST", "LACK OF FOCUS", "GOALS NOT ACHIEVED", "LACK OF DISCIPLINE"]

# app = FastAPI()

# # Function to fetch the credentials
# def get_credentials_from_env():
#     service_account_info = {
#         "type": os.getenv("SERVICE_ACCOUNT_TYPE"),
#         "project_id": os.getenv("PROJECT_ID"),
#         "private_key_id": os.getenv("PRIVATE_KEY_ID"),
#         "private_key": os.getenv("PRIVATE_KEY").replace('\\n', '\n'),
#         "client_email": os.getenv("CLIENT_EMAIL"),
#         "client_id": os.getenv("CLIENT_ID"),
#         "auth_uri": os.getenv("AUTH_URI"),
#         "token_uri": os.getenv("TOKEN_URI"),
#         "auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
#         "client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
#         "universe_domain": os.getenv("UNIVERSE_DOMAIN")
#     }
    
#     scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
#     creds = Credentials.from_service_account_info(service_account_info, scopes=scope)
#     return creds

# # Main function to fetch the panic button occurrences
# @app.get("/panic-button-occurances")
# def get_panic_button_occurrences():
#     try:
#         # Set up credentials
#         creds = get_credentials_from_env()
#         client = gspread.authorize(creds)

#         # Open the Google Sheet
#         sheet = client.open_by_url('https://docs.google.com/spreadsheets/d/1nFZGkCvRV6qS-mhsORhX3dxI0JSge32_UwWgWKl3eyw/edit?gid=0#gid=0').worksheet('Sheet1')

#         # Get all values from the sheet
#         data = sheet.get_all_values()

#         # Initialize the lists with panic button names and 0 counts
#         academic_list = {button: 0 for button in academic_panic_buttons}
#         non_academic_list = {button: 0 for button in non_academic_panic_buttons}

#         # Iterate through all rows in the data
#         for row in data:
#             panic_button = row[1]  # Assuming the panic button values are in the second column
#             if panic_button in academic_list:
#                 academic_list[panic_button] += 1
#             elif panic_button in non_academic_list:
#                 non_academic_list[panic_button] += 1

#         return {
#             "academic": academic_list,
#             "non_academic": non_academic_list
#         }

#     except GoogleAuthError as e:
#         return {"error": f"Authentication error: {e}"}
#     except gspread.exceptions.SpreadsheetNotFound:
#         return {"error": "Spreadsheet not found. Please check the URL."}
#     except Exception as e:
#         return {"error": f"An error occurred: {e}"}
from fastapi import FastAPI
import gspread
from google.oauth2.service_account import Credentials
from google.auth.exceptions import GoogleAuthError
import os

# Define the Academic and Non-Academic panic buttons
academic_panic_buttons = ["MISSED CLASSES", "BACKLOGS", "LACK OF MOTIVATION", "NOT UNDERSTANDING", "BAD MARKS"]
non_academic_panic_buttons = ["EMOTIONAL FACTORS", "PROCRASTINATE", "LOST INTEREST", "LACK OF FOCUS", "GOALS NOT ACHIEVED", "LACK OF DISCIPLINE"]

app = FastAPI()

# Function to fetch the credentials
def get_credentials_from_env():
    service_account_info = {
        "type": os.getenv("SERVICE_ACCOUNT_TYPE"),
        "project_id": os.getenv("PROJECT_ID"),
        "private_key_id": os.getenv("PRIVATE_KEY_ID"),
        "private_key": os.getenv("PRIVATE_KEY").replace('\\n', '\n'),
        "client_email": os.getenv("CLIENT_EMAIL"),
        "client_id": os.getenv("CLIENT_ID"),
        "auth_uri": os.getenv("AUTH_URI"),
        "token_uri": os.getenv("TOKEN_URI"),
        "auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
        "client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
        "universe_domain": os.getenv("UNIVERSE_DOMAIN")
    }
    
    scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
    creds = Credentials.from_service_account_info(service_account_info, scopes=scope)
    return creds

# Main function to fetch the panic button occurrences
@app.get("/panic-button-occurances")
def get_panic_button_occurrences():
    try:
        # Set up credentials
        creds = get_credentials_from_env()
        client = gspread.authorize(creds)

        # Open the Google Sheet
        sheet = client.open_by_url('https://docs.google.com/spreadsheets/d/1nFZGkCvRV6qS-mhsORhX3dxI0JSge32_UwWgWKl3eyw/edit?gid=0#gid=0').worksheet('Sheet1')

        # Get all values from the sheet
        data = sheet.get_all_values()

        # Initialize the lists with panic button names and 0 counts
        academic_list = {button: 0 for button in academic_panic_buttons}
        non_academic_list = {button: 0 for button in non_academic_panic_buttons}

        # Iterate through all rows in the data
        for row in data:
            for panic_button in row:
                # Stop when an empty column is encountered
                if not panic_button:
                    break

                # Check and update counts for academic and non-academic panic buttons
                if panic_button in academic_list:
                    academic_list[panic_button] += 1
                elif panic_button in non_academic_list:
                    non_academic_list[panic_button] += 1

        return {
            "academic": academic_list,
            "non_academic": non_academic_list
        }

    except GoogleAuthError as e:
        return {"error": f"Authentication error: {e}"}
    except gspread.exceptions.SpreadsheetNotFound:
        return {"error": "Spreadsheet not found. Please check the URL."}
    except Exception as e:
        return {"error": f"An error occurred: {e}"}