1
1
import os
2
2
import csv
3
3
import json
4
- import sys
5
4
from google .oauth2 import service_account
6
5
from googleapiclient .discovery import build
7
6
from googleapiclient .errors import HttpError
8
7
8
+ # If modifying these scopes, update the GCP_JSON secret accordingly.
9
+ SCOPES = ["https://www.googleapis.com/auth/spreadsheets" ]
10
+
9
11
def get_env_var (var_name ):
10
12
value = os .environ .get (var_name )
11
13
if value is None :
12
- print (f"Error: { var_name } environment variable is not set" )
13
- sys .exit (1 )
14
+ raise ValueError (f"{ var_name } environment variable is not set" )
14
15
return value
15
16
16
17
def setup_credentials ():
17
- creds_json = get_env_var ('GCP_JSON' ) # Changed from GCP_SA_KEY to GCP_JSON
18
- try :
19
- creds_dict = json .loads (creds_json )
20
- return service_account .Credentials .from_service_account_info (
21
- creds_dict ,
22
- scopes = ['https://www.googleapis.com/auth/spreadsheets' ]
23
- )
24
- except json .JSONDecodeError :
25
- print ("Error: Invalid JSON in GCP_JSON" ) # Changed error message
26
- sys .exit (1 )
18
+ gcp_json = get_env_var ('GCP_JSON' )
19
+ creds_dict = json .loads (gcp_json )
20
+ return service_account .Credentials .from_service_account_info (
21
+ creds_dict , scopes = SCOPES )
27
22
28
23
def read_csv (file_path ):
29
- try :
30
- with open (file_path , 'r' ) as file :
31
- return list (csv .reader (file ))
32
- except FileNotFoundError :
33
- print (f"Error: CSV file not found at { file_path } " )
34
- sys .exit (1 )
24
+ with open (file_path , 'r' ) as file :
25
+ return list (csv .reader (file ))
35
26
36
27
def upload_to_sheets (service , spreadsheet_id , data ):
37
- sheet_range = 'Sheet1'
28
+ sheet_range = 'Sheet1' # Update this if you want to use a different sheet name
38
29
body = {'values' : data }
39
30
40
31
try :
@@ -52,19 +43,20 @@ def upload_to_sheets(service, spreadsheet_id, data):
52
43
body = body
53
44
).execute ()
54
45
print (f"{ result .get ('updatedCells' )} cells updated." )
55
- except HttpError as e :
56
- print (f"HTTP error occurred: { e } " )
57
- if e .resp .status == 404 :
58
- print ("Error 404: Make sure the Google Sheets ID is correct and the service account has access to the sheet." )
59
- sys .exit (1 )
46
+ except HttpError as err :
47
+ print (f"An error occurred: { err } " )
48
+ raise
60
49
61
50
def main ():
62
- creds = setup_credentials ()
63
- service = build ('sheets' , 'v4' , credentials = creds )
64
- spreadsheet_id = get_env_var ('GOOGLE_SHEETS_ID' )
65
-
66
- csv_content = read_csv ('public/merged.csv' )
67
- upload_to_sheets (service , spreadsheet_id , csv_content )
51
+ try :
52
+ creds = setup_credentials ()
53
+ service = build ("sheets" , "v4" , credentials = creds )
54
+ spreadsheet_id = get_env_var ('GOOGLE_SHEETS_ID' )
55
+
56
+ csv_content = read_csv ('public/merged.csv' )
57
+ upload_to_sheets (service , spreadsheet_id , csv_content )
58
+ except HttpError as err :
59
+ print (err )
68
60
69
61
if __name__ == "__main__" :
70
62
main ()
0 commit comments