Update google_sheets.py
Browse files- google_sheets.py +86 -85
google_sheets.py
CHANGED
|
@@ -1,85 +1,86 @@
|
|
| 1 |
-
import uuid
|
| 2 |
-
from google.oauth2 import service_account
|
| 3 |
-
from googleapiclient.discovery import build
|
| 4 |
-
from env_setup import config
|
| 5 |
-
import pandas as pd
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
from google.oauth2 import service_account
|
| 3 |
+
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()
|
| 26 |
+
|
| 27 |
+
call_id = str(uuid.uuid4())
|
| 28 |
+
print(f"Call ID: {call_id}")
|
| 29 |
+
|
| 30 |
+
values = []
|
| 31 |
+
if chunks:
|
| 32 |
+
first_chunk, first_sentiment, _ = chunks[0]
|
| 33 |
+
values.append([call_id, first_chunk, first_sentiment, summary, overall_sentiment])
|
| 34 |
+
for chunk, sentiment, _ in chunks[1:]:
|
| 35 |
+
values.append(["", chunk, sentiment, "", ""])
|
| 36 |
+
|
| 37 |
+
header = ["Call ID", "Chunk", "Sentiment", "Summary", "Overall Sentiment"]
|
| 38 |
+
all_values = [header] + values
|
| 39 |
+
|
| 40 |
+
body = {'values': all_values}
|
| 41 |
+
try:
|
| 42 |
+
result = sheet.values().append(
|
| 43 |
+
spreadsheetId=sheet_id,
|
| 44 |
+
range="Sheet1!A1",
|
| 45 |
+
valueInputOption="RAW",
|
| 46 |
+
body=body
|
| 47 |
+
).execute()
|
| 48 |
+
print(f"{result.get('updates').get('updatedCells')} cells updated in Google Sheets.")
|
| 49 |
+
except Exception as e:
|
| 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 |
+
# If rows exist, convert to DataFrame
|
| 72 |
+
if rows:
|
| 73 |
+
headers = rows[0]
|
| 74 |
+
data = rows[1:]
|
| 75 |
+
if not data:
|
| 76 |
+
print("No data available in the specified range.")
|
| 77 |
+
return pd.DataFrame(columns=headers) # Return DataFrame with headers only
|
| 78 |
+
df = pd.DataFrame(data, columns=headers)
|
| 79 |
+
return df
|
| 80 |
+
else:
|
| 81 |
+
print("No data available in the Google Sheet.")
|
| 82 |
+
return pd.DataFrame()
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error fetching data from Google Sheets: {e}")
|
| 86 |
+
return pd.DataFrame()
|