-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (61 loc) · 2.53 KB
/
Copy pathmain.py
File metadata and controls
72 lines (61 loc) · 2.53 KB
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
import functions_framework
import json
import os
from gmail_scraper import main as scraper_main, backfill_alloydb
@functions_framework.http
def run_scraper(request):
"""HTTP Cloud Function to trigger the Gmail scraper.
Routes:
- GET /: Health check
- POST /: Trigger scraper with optional parameters
- POST / with {"action": "backfill"}: Backfill AlloyDB from BigQuery
POST body for scrape (default):
{
"query": "subject:RFI", # Gmail search query
"max_per_user": 100, # Max emails per user
"incremental": true # Only fetch new emails (default: true)
}
POST body for backfill:
{
"action": "backfill",
"user_email": "flow@envsn.com" # Optional: backfill single user
}
"""
# Handle health check
if request.method == 'GET':
return json.dumps({
'status': 'healthy',
'service': 'gmail-scraper',
'project': os.getenv('PROJECT_ID', 'claude-mcp-457317'),
'dataset': os.getenv('DATASET_ID', 'gmail_analytics'),
'table': os.getenv('TABLE_ID', 'messages'),
'alloydb': 'enabled' if os.getenv('ALLOYDB_URL') else 'disabled',
'mode': 'incremental'
}), 200, {'Content-Type': 'application/json'}
try:
request_json = request.get_json(silent=True) or {}
# Route: backfill AlloyDB from BigQuery
if request_json.get('action') == 'backfill':
user_email = request_json.get('user_email')
print(f"Starting backfill: user_email={user_email or 'ALL'}")
results = backfill_alloydb(user_email_filter=user_email)
return json.dumps(results, default=str), 200, {'Content-Type': 'application/json'}
# Route: normal scrape (default)
query = request_json.get('query', '')
max_per_user = request_json.get('max_per_user', 100)
incremental = request_json.get('incremental', True)
print(f"Starting scraper: query='{query}', max_per_user={max_per_user}, incremental={incremental}")
results = scraper_main(
query=query,
max_per_user=max_per_user,
incremental=incremental
)
return json.dumps(results, default=str), 200, {'Content-Type': 'application/json'}
except Exception as e:
print(f"Error running scraper: {e}")
import traceback
traceback.print_exc()
return json.dumps({
'status': 'error',
'error': str(e)
}), 500, {'Content-Type': 'application/json'}