-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_async.py
More file actions
503 lines (421 loc) · 18.1 KB
/
Copy pathapi_async.py
File metadata and controls
503 lines (421 loc) · 18.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
"""
Async API endpoints for Instagram scraping system.
This module contains refactored endpoints that use Celery background tasks.
"""
import os
import uuid
import logging
from typing import Dict, Any
from datetime import datetime, timezone
from flask import jsonify, request
from celery import chord
from tasks import (
scrape_account_batch,
aggregate_scrape_results,
ingest_profiles_batch,
daily_pipeline_orchestrator
)
from utils.base_id_utils import get_base_id_from_request, validate_base_id
logger = logging.getLogger(__name__)
def register_async_endpoints(app, get_supabase_client, limiter=None):
"""
Register async endpoints to Flask app with rate limiting.
Args:
app: Flask application instance
get_supabase_client: Function to get Supabase client
limiter: Flask-Limiter instance for rate limiting
"""
# Decorate scrape endpoint with strict rate limit
@app.route('/api/scrape-followers', methods=['POST'])
@limiter.limit("10 per hour") if limiter else (lambda f: f)
def scrape_followers_async():
"""
ASYNC: Queue follower scraping job for multi-platform support.
RATE LIMITED: 10 requests per hour (expensive Apify operation at scale)
Expected JSON payload:
{
"accounts": ["username1", "username2", ...],
"targetGender": "male" (optional, defaults to "male"),
"totalScrapeCount": 150 (optional, total accounts to scrape),
"platform": "Instagram" (optional, defaults to "Instagram"),
"base_id": "appXYZ123ABC" (optional, defaults to 'default_instagram')
}
OR pass base_id via header:
X-Base-Id: appXYZ123ABC
Supported platforms: "Instagram", "TikTok", "Threads", "X"
Returns:
{
"success": true,
"job_id": "uuid",
"base_id": "appXYZ123ABC",
"platform": "instagram",
"status_url": "/api/job-status/uuid",
"results_url": "/api/job-results/uuid",
"message": "Job queued successfully. Poll status_url for progress."
}
"""
try:
# Get JSON data from request
data = request.get_json()
if not data or 'accounts' not in data:
return jsonify({
'success': False,
'error': 'Missing "accounts" field in request body'
}), 400
accounts = data['accounts']
target_gender = data.get('targetGender', 'male')
total_scrape_count = data.get('totalScrapeCount', None)
# ADDED: Platform support with default to Instagram for backward compatibility
platform = data.get('platform', 'Instagram').lower()
# Validate platform
valid_platforms = ['instagram', 'tiktok', 'threads', 'x']
if platform not in valid_platforms:
return jsonify({
'success': False,
'error': f'Invalid platform "{platform}". Must be one of: {", ".join(valid_platforms)}'
}), 400
if not isinstance(accounts, list) or len(accounts) == 0:
return jsonify({
'success': False,
'error': 'Accounts must be a non-empty list'
}), 400
# Extract base_id with fallback to default
base_id = get_base_id_from_request()
if not validate_base_id(base_id):
return jsonify({
'success': False,
'error': f'Invalid base_id format: {base_id}'
}), 400
# Compute per-account scrape count
if total_scrape_count is not None:
if total_scrape_count <= 0:
return jsonify({
'success': False,
'error': 'totalScrapeCount must be positive'
}), 400
per_account_count = int(total_scrape_count / len(accounts))
if per_account_count == 0:
return jsonify({
'success': False,
'error': 'totalScrapeCount too small for number of accounts'
}), 400
else:
per_account_count = 5 # Default
# Create job record
job_id = str(uuid.uuid4())
supabase = get_supabase_client()
# Split accounts into batches of 50
batch_size = 50
account_batches = [accounts[i:i + batch_size] for i in range(0, len(accounts), batch_size)]
total_batches = len(account_batches)
logger.info(f"Creating {platform} job {job_id} with {total_batches} batches for base_id={base_id}")
# Insert job record with base_id and platform
supabase.table('scrape_jobs').insert({
'job_id': job_id,
'status': 'queued',
'accounts': accounts,
'target_gender': target_gender,
'max_count_per_account': per_account_count,
'total_batches': total_batches,
'current_batch': 0,
'progress': 0.0,
'profiles_scraped': 0,
'base_id': base_id,
'platform': platform, # ADDED: Platform support
'created_at': datetime.now(timezone.utc).isoformat()
}).execute()
logger.info(f"Job {job_id} created, queueing {platform} scraping tasks")
# Queue batch tasks using Celery chord pattern
# All batches run in parallel, then aggregation runs after all complete
# FIXED: Pass base_id and platform to all tasks for multi-tenant & multi-platform support
batch_tasks = []
for i, batch in enumerate(account_batches, 1):
task = scrape_account_batch.s(
job_id=job_id,
accounts=batch,
target_gender=target_gender,
max_per_account=per_account_count,
batch_number=i,
base_id=base_id, # Multi-tenant isolation
platform=platform # ADDED: Platform support
)
batch_tasks.append(task)
# Create chord: all batches → aggregation
# FIXED: Pass base_id to aggregation task
workflow = chord(batch_tasks)(
aggregate_scrape_results.s(job_id=job_id, base_id=base_id)
)
# Update job to processing
supabase.table('scrape_jobs')\
.update({
'status': 'processing',
'started_at': datetime.now(timezone.utc).isoformat()
})\
.eq('job_id', job_id)\
.execute()
logger.info(f"Job {job_id} queued successfully with {total_batches} batches for {platform} (base_id={base_id})")
return jsonify({
'success': True,
'job_id': job_id,
'base_id': base_id,
'platform': platform, # ADDED: Include platform in response
'status_url': f'/api/job-status/{job_id}',
'results_url': f'/api/job-results/{job_id}',
'total_batches': total_batches,
'message': f'{platform.capitalize()} scraping job queued successfully. Poll status_url for progress.'
}), 202 # 202 Accepted
except Exception as e:
logger.error(f"Error queueing scrape job: {str(e)}")
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/api/job-status/<job_id>', methods=['GET'])
def get_job_status(job_id: str):
"""
Get status of a scraping job.
Returns:
{
"success": true,
"job_id": "uuid",
"status": "processing",
"progress": 45.5,
"profiles_scraped": 1200,
"total_batches": 10,
"current_batch": 5,
"error_message": null,
"created_at": "2025-10-14T...",
"completed_at": null
}
"""
try:
supabase = get_supabase_client()
# Query job
job = supabase.table('scrape_jobs')\
.select('*')\
.eq('job_id', job_id)\
.execute()
if not job.data or len(job.data) == 0:
return jsonify({
'success': False,
'error': f'Job {job_id} not found'
}), 404
job_data = job.data[0]
return jsonify({
'success': True,
'job_id': job_id,
'status': job_data['status'],
'progress': float(job_data['progress']) if job_data['progress'] else 0.0,
'profiles_scraped': job_data['profiles_scraped'],
'total_scraped': job_data.get('total_scraped'),
'total_filtered': job_data.get('total_filtered'),
'total_batches': job_data.get('total_batches', 0),
'current_batch': job_data.get('current_batch', 0),
'error_message': job_data.get('error_message'),
'created_at': job_data['created_at'],
'started_at': job_data.get('started_at'),
'completed_at': job_data.get('completed_at')
})
except Exception as e:
logger.error(f"Error fetching job status: {str(e)}")
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/api/job-results/<job_id>', methods=['GET'])
def get_job_results(job_id: str):
"""
Get results from a completed scraping job with pagination.
Query parameters:
page: Page number (default: 1)
limit: Results per page (default: 1000, max: 5000)
Returns:
{
"success": true,
"job_id": "uuid",
"page": 1,
"limit": 1000,
"total": 5000,
"profiles": [
{
"id": "123",
"username": "john_doe",
"full_name": "John Doe",
"created_at": "..."
}
]
}
"""
try:
supabase = get_supabase_client()
# Verify job exists and is completed
job = supabase.table('scrape_jobs')\
.select('status')\
.eq('job_id', job_id)\
.execute()
if not job.data or len(job.data) == 0:
return jsonify({
'success': False,
'error': f'Job {job_id} not found'
}), 404
job_status = job.data[0]['status']
if job_status != 'completed':
return jsonify({
'success': False,
'error': f'Job is not completed yet (status: {job_status})'
}), 400
# Pagination parameters
page = int(request.args.get('page', 1))
limit = min(int(request.args.get('limit', 1000)), 5000) # Max 5000 per page
offset = (page - 1) * limit
# Get total count
count_result = supabase.table('scrape_results')\
.select('id', count='exact')\
.eq('job_id', job_id)\
.execute()
total = count_result.count if count_result.count else 0
# Get paginated results
results = supabase.table('scrape_results')\
.select('profile_id, username, full_name, created_at')\
.eq('job_id', job_id)\
.order('created_at', desc=True)\
.range(offset, offset + limit - 1)\
.execute()
profiles = []
if results.data:
for row in results.data:
profiles.append({
'id': row['profile_id'],
'username': row['username'],
'full_name': row.get('full_name', ''),
'created_at': row['created_at']
})
return jsonify({
'success': True,
'job_id': job_id,
'page': page,
'limit': limit,
'total': total,
'profiles': profiles
})
except Exception as e:
logger.error(f"Error fetching job results: {str(e)}")
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/api/ingest', methods=['POST'])
@limiter.limit("30 per hour") if limiter else (lambda f: f)
def ingest_profiles_async():
"""
ASYNC: Queue profile ingestion job.
RATE LIMITED: 30 requests per hour (database-intensive operation)
Expected JSON payload:
{
"profiles": [
{
"id": "123456",
"username": "john_doe",
"full_name": "John Doe"
}
]
}
Returns:
{
"success": true,
"batch_count": 5,
"total_profiles": 5000,
"message": "Ingestion queued successfully"
}
"""
try:
data = request.get_json()
if not data or 'profiles' not in data:
return jsonify({
'success': False,
'error': 'Missing "profiles" field in request body'
}), 400
profiles = data['profiles']
if not isinstance(profiles, list):
return jsonify({
'success': False,
'error': 'Profiles must be a list'
}), 400
if len(profiles) == 0:
return jsonify({
'success': True,
'batch_count': 0,
'total_profiles': 0,
'message': 'No profiles to ingest'
})
# Extract base_id with fallback to default
base_id = get_base_id_from_request()
# Split profiles into batches of 1000
batch_size = 1000
profile_batches = [profiles[i:i + batch_size] for i in range(0, len(profiles), batch_size)]
batch_id = str(uuid.uuid4())
logger.info(f"Queueing ingestion {batch_id}: {len(profiles)} profiles in {len(profile_batches)} batches (base_id={base_id})")
# Queue batch tasks - FIXED: Pass base_id to tasks
for i, batch in enumerate(profile_batches, 1):
ingest_profiles_batch.delay(
batch_id=batch_id,
profiles=batch,
batch_number=i,
base_id=base_id # ADDED: Multi-tenant isolation
)
return jsonify({
'success': True,
'batch_id': batch_id,
'batch_count': len(profile_batches),
'total_profiles': len(profiles),
'message': f'Ingestion queued successfully ({len(profile_batches)} batches)'
}), 202 # 202 Accepted
except Exception as e:
logger.error(f"Error queueing ingest job: {str(e)}")
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/api/run-daily', methods=['POST'])
@limiter.limit("5 per day") if limiter else (lambda f: f)
def run_daily_async():
"""
ASYNC: Queue daily pipeline orchestration.
RATE LIMITED: 5 requests per day (should only run once daily)
Optional JSON payload:
{
"campaign_date": "2025-10-14" (optional, defaults to today),
"profiles_per_table": 180 (optional)
}
Returns:
{
"success": true,
"task_id": "celery-task-id",
"message": "Daily pipeline queued successfully"
}
"""
try:
data = request.get_json() or {}
campaign_date = data.get('campaign_date')
profiles_per_table = data.get('profiles_per_table', 180)
# Extract base_id with fallback to default
base_id = get_base_id_from_request()
logger.info(f"Queueing daily pipeline: date={campaign_date}, profiles_per_table={profiles_per_table}, base_id={base_id}")
# Queue orchestrator task - FIXED: Pass base_id
task = daily_pipeline_orchestrator.delay(
campaign_date=campaign_date,
profiles_per_table=profiles_per_table,
base_id=base_id # ADDED: Multi-tenant isolation
)
return jsonify({
'success': True,
'task_id': task.id,
'message': 'Daily pipeline queued successfully. Check logs for progress.'
}), 202 # 202 Accepted
except Exception as e:
logger.error(f"Error queueing daily pipeline: {str(e)}")
return jsonify({
'success': False,
'error': str(e)
}), 500
logger.info("Async endpoints registered successfully")