Update google_sheets.py
Browse files- google_sheets.py +2 -20
google_sheets.py
CHANGED
@@ -4,22 +4,15 @@ from googleapiclient.discovery import build
|
|
4 |
from env_setup import config
|
5 |
import pandas as pd
|
6 |
|
7 |
-
# Define the required scopes for accessing Google Sheets
|
8 |
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
|
9 |
|
10 |
def authenticate_google_account():
|
11 |
-
"""
|
12 |
-
Authenticate the Google account using service account credentials.
|
13 |
-
"""
|
14 |
service_account_file = config["google_creds"]
|
15 |
if not service_account_file:
|
16 |
raise ValueError("Service account credentials path is missing in env_setup.py.")
|
17 |
return service_account.Credentials.from_service_account_file(service_account_file, scopes=SCOPES)
|
18 |
|
19 |
def store_data_in_sheet(sheet_id, chunks, summary, overall_sentiment):
|
20 |
-
"""
|
21 |
-
Store data into a Google Sheet.
|
22 |
-
"""
|
23 |
creds = authenticate_google_account()
|
24 |
service = build('sheets', 'v4', credentials=creds)
|
25 |
sheet = service.spreadsheets()
|
@@ -50,43 +43,32 @@ def store_data_in_sheet(sheet_id, chunks, summary, overall_sentiment):
|
|
50 |
print(f"Error updating Google Sheets: {e}")
|
51 |
|
52 |
def fetch_call_data(sheet_id, sheet_range="Sheet1!A1:E"):
|
53 |
-
"""
|
54 |
-
Fetches data from the specified Google Sheet and returns a pandas DataFrame.
|
55 |
-
"""
|
56 |
try:
|
57 |
-
# Authenticate and get credentials
|
58 |
creds = authenticate_google_account()
|
59 |
service = build('sheets', 'v4', credentials=creds)
|
60 |
sheet = service.spreadsheets()
|
61 |
|
62 |
-
# Fetch the data
|
63 |
result = sheet.values().get(
|
64 |
spreadsheetId=sheet_id,
|
65 |
range=sheet_range
|
66 |
).execute()
|
67 |
|
68 |
-
# Get the rows
|
69 |
rows = result.get("values", [])
|
70 |
|
71 |
-
# Log
|
72 |
-
print(f"Fetched rows: {rows}")
|
73 |
|
74 |
-
# If rows exist, convert to DataFrame
|
75 |
if rows:
|
76 |
-
# Use the first row as column headers
|
77 |
headers = rows[0]
|
78 |
data = rows[1:]
|
79 |
|
80 |
-
# Create DataFrame
|
81 |
df = pd.DataFrame(data, columns=headers)
|
|
|
82 |
|
83 |
return df
|
84 |
else:
|
85 |
-
# Return an empty DataFrame if no data
|
86 |
print("No data found in the specified range.")
|
87 |
return pd.DataFrame()
|
88 |
|
89 |
except Exception as e:
|
90 |
print(f"Error fetching data from Google Sheets: {e}")
|
91 |
-
# Return an empty DataFrame in case of error
|
92 |
return pd.DataFrame()
|
|
|
4 |
from env_setup import config
|
5 |
import pandas as pd
|
6 |
|
|
|
7 |
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
|
8 |
|
9 |
def authenticate_google_account():
|
|
|
|
|
|
|
10 |
service_account_file = config["google_creds"]
|
11 |
if not service_account_file:
|
12 |
raise ValueError("Service account credentials path is missing in env_setup.py.")
|
13 |
return service_account.Credentials.from_service_account_file(service_account_file, scopes=SCOPES)
|
14 |
|
15 |
def store_data_in_sheet(sheet_id, chunks, summary, overall_sentiment):
|
|
|
|
|
|
|
16 |
creds = authenticate_google_account()
|
17 |
service = build('sheets', 'v4', credentials=creds)
|
18 |
sheet = service.spreadsheets()
|
|
|
43 |
print(f"Error updating Google Sheets: {e}")
|
44 |
|
45 |
def fetch_call_data(sheet_id, sheet_range="Sheet1!A1:E"):
|
|
|
|
|
|
|
46 |
try:
|
|
|
47 |
creds = authenticate_google_account()
|
48 |
service = build('sheets', 'v4', credentials=creds)
|
49 |
sheet = service.spreadsheets()
|
50 |
|
|
|
51 |
result = sheet.values().get(
|
52 |
spreadsheetId=sheet_id,
|
53 |
range=sheet_range
|
54 |
).execute()
|
55 |
|
|
|
56 |
rows = result.get("values", [])
|
57 |
|
58 |
+
print(f"Fetched rows: {rows}") # Log fetched rows
|
|
|
59 |
|
|
|
60 |
if rows:
|
|
|
61 |
headers = rows[0]
|
62 |
data = rows[1:]
|
63 |
|
|
|
64 |
df = pd.DataFrame(data, columns=headers)
|
65 |
+
print(f"DataFrame: {df}") # Log DataFrame
|
66 |
|
67 |
return df
|
68 |
else:
|
|
|
69 |
print("No data found in the specified range.")
|
70 |
return pd.DataFrame()
|
71 |
|
72 |
except Exception as e:
|
73 |
print(f"Error fetching data from Google Sheets: {e}")
|
|
|
74 |
return pd.DataFrame()
|