Skip to content

Commit 000dd22

Browse files
committed
add ci/cd to google sheets
1 parent ced4d88 commit 000dd22

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

.github/workflows/scrape.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ jobs:
8282
env:
8383
GITHUB_TOKEN: ${{ secrets.MEONG }}
8484

85+
- name: Set up Google Cloud SDK
86+
uses: google-github-actions/setup-gcloud@v1
87+
with:
88+
project_id: ${{ secrets.GCP_PROJECT_ID }}
89+
service_account_key: ${{ secrets.GCP_SA_KEY }}
90+
export_default_credentials: true
91+
92+
- name: Install Google Sheets API client library
93+
run: pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
94+
95+
- name: Update Google Sheets
96+
env:
97+
SHEET_ID: ${{ secrets.GOOGLE_SHEET_ID }}
98+
run: python update_sheets.py
99+
85100
- name: Upload CSV files as artifact
86101
uses: actions/upload-artifact@v4
87102
with:

update_sheets.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import pandas as pd
3+
from google.oauth2 import service_account
4+
from googleapiclient.discovery import build
5+
6+
# Set up credentials
7+
creds = service_account.Credentials.from_service_account_info(
8+
eval(os.environ['GCP_SA_KEY']),
9+
scopes=['https://www.googleapis.com/auth/spreadsheets']
10+
)
11+
12+
# Build the Sheets API service
13+
service = build('sheets', 'v4', credentials=creds)
14+
15+
# Get the Sheet ID from environment variable
16+
SHEET_ID = os.environ['SHEET_ID']
17+
18+
# Read the existing merged CSV file
19+
csv_file = 'public/merged.csv'
20+
df = pd.read_csv(csv_file)
21+
22+
# Convert DataFrame to list of lists
23+
values = [df.columns.tolist()] + df.values.tolist()
24+
25+
# Sheet name
26+
sheet_name = 'Merged Jobs'
27+
28+
# Update or create sheet
29+
try:
30+
# Try to clear existing content
31+
service.spreadsheets().values().clear(
32+
spreadsheetId=SHEET_ID,
33+
range=f'{sheet_name}!A1:Z'
34+
).execute()
35+
except:
36+
# If sheet doesn't exist, add a new one
37+
service.spreadsheets().batchUpdate(
38+
spreadsheetId=SHEET_ID,
39+
body={'requests': [{'addSheet': {'properties': {'title': sheet_name}}}]}
40+
).execute()
41+
42+
# Update sheet with new data
43+
service.spreadsheets().values().update(
44+
spreadsheetId=SHEET_ID,
45+
range=f'{sheet_name}!A1',
46+
valueInputOption='RAW',
47+
body={'values': values}
48+
).execute()
49+
50+
print("Google Sheets updated successfully with merged data!")

0 commit comments

Comments
 (0)