-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
426 lines (338 loc) · 12.4 KB
/
Copy pathapp.py
File metadata and controls
426 lines (338 loc) · 12.4 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
"""RESTful API for Lyubitori."""
import logging
import threading
import time
import uuid
from dataclasses import asdict, dataclass
from datetime import datetime
from enum import Enum
from typing import Any
from flask import Flask, jsonify, request
from flask_cors import CORS
from twitter_scraper.config import Config
from twitter_scraper.driver import DriverManager
from twitter_scraper.scraper import TwitterImageScraper
from twitter_scraper.session import SessionManager
type TaskDict = dict[str, TaskInfo]
type ThreadDict = dict[str, threading.Thread]
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class TaskStatus(Enum):
"""Task status enumeration."""
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
FAILED = "failed"
CANCELLED = "cancelled"
@dataclass
class TaskInfo:
"""Information about a download task."""
task_id: str
status: TaskStatus
url: str | None
max_scroll: int
created_at: datetime
started_at: datetime | None = None
completed_at: datetime | None = None
error_message: str | None = None
progress: dict[str, Any] = None
results: dict[str, Any] = None
def to_dict(self) -> dict[str, Any]:
"""Convert to dictionary for JSON response."""
data = asdict(self)
data["status"] = self.status.value
data["created_at"] = self.created_at.isoformat() if self.created_at else None
data["started_at"] = self.started_at.isoformat() if self.started_at else None
data["completed_at"] = (
self.completed_at.isoformat() if self.completed_at else None
)
return data
# Global state
config = Config()
tasks: TaskDict = {}
active_tasks: ThreadDict = {}
task_lock = threading.Lock()
# Shared driver components
driver_manager: DriverManager | None = None
session_manager = SessionManager(config.session_path)
scraper: TwitterImageScraper | None = None
driver_lock = threading.Lock()
# Create Flask app
app = Flask(__name__)
CORS(app)
def ensure_driver() -> bool:
"""Ensure driver and related components are initialized."""
global driver_manager, scraper
try:
if not driver_manager:
driver_manager = DriverManager(config)
driver = driver_manager.get_driver()
if not scraper:
scraper = TwitterImageScraper(driver, config)
return True
except Exception as e:
logger.error(f"Failed to ensure driver: {e}")
return False
def check_authentication() -> bool:
"""Check if currently authenticated (assumes user is logged in)."""
try:
if not ensure_driver():
return False
return session_manager.has_saved_session()
except Exception as e:
logger.error(f"Authentication check error: {e}")
return False
def cleanup_driver():
"""Clean up driver resources."""
global driver_manager, scraper
if driver_manager:
driver_manager.quit_driver()
driver_manager = None
scraper = None
def run_download_task(task_id: str, debug_mode: bool = False):
"""Run a download task in background."""
global tasks
with task_lock:
task = tasks.get(task_id)
if not task:
return
try:
# Update task status
with task_lock:
task.status = TaskStatus.RUNNING
task.started_at = datetime.now()
logger.info(f"Starting download task {task_id}: {task.url} (debug: {debug_mode})")
# Enable debug mode if requested
if debug_mode:
config.enable_debug()
# Ensure driver is ready
with driver_lock:
if not ensure_driver():
raise Exception("Failed to initialize driver")
# Navigate to likes page (assuming user is logged in)
driver = driver_manager.get_driver()
driver.get(config.likes_url)
# Start downloading with progress tracking
total_images = 0
loop = 0
previous_urls = set()
while loop < task.max_scroll:
# Check if task was cancelled
with task_lock:
if task.status == TaskStatus.CANCELLED:
logger.info(f"Task {task_id} was cancelled")
return
try:
last_img, viewed_urls = scraper.download_visible_images()
# Update progress
new_images = len(viewed_urls - previous_urls)
total_images += new_images
previous_urls = viewed_urls
with task_lock:
task.progress = {
"images_processed": total_images,
"scroll_iterations": loop + 1,
"current_page_images": len(viewed_urls),
}
# Check if no new content
if len(viewed_urls.union(previous_urls)) == len(previous_urls):
logger.info(f"No new content found for task {task_id}")
break
# Scroll down
if last_img:
from selenium.webdriver import ActionChains
ActionChains(scraper.driver).scroll_to_element(
last_img
).perform()
time.sleep(config.scroll_delay)
except Exception as e:
logger.warning(
f"Error in scroll iteration {loop} for task {task_id}: {e}"
)
loop += 1
# Task completed successfully
with task_lock:
task.status = TaskStatus.COMPLETED
task.completed_at = datetime.now()
task.results = {
"total_images_processed": total_images,
"scroll_iterations_completed": loop,
"download_path": str(config.save_path),
"debug_enabled": debug_mode,
"debug_path": str(config.debug_path) if debug_mode else None,
}
logger.info(
f"Task {task_id} completed successfully: {total_images} images processed"
)
except Exception as e:
logger.error(f"Task {task_id} failed: {e}")
with task_lock:
task.status = TaskStatus.FAILED
task.completed_at = datetime.now()
task.error_message = str(e)
finally:
# Clean up active task reference
with task_lock:
active_tasks.pop(task_id, None)
# API Routes
@app.route("/status", methods=["GET"])
def status():
"""Get API and authentication status."""
try:
with driver_lock:
is_authenticated = check_authentication()
return jsonify(
{
"status": "ok",
"authenticated": is_authenticated,
"session_saved": session_manager.has_saved_session(),
"active_tasks": len(active_tasks),
"total_tasks": len(tasks),
"config": {
"save_path": str(config.save_path),
"likes_url": config.likes_url,
"debug_mode": config.debug_mode,
"debug_path": str(config.debug_path) if config.debug_mode else None,
},
}
)
except Exception as e:
logger.error(f"Status check error: {e}")
return jsonify({"status": "error", "message": str(e)}), 500
@app.route("/download", methods=["POST"])
def download():
"""Start a new download task."""
try:
data = request.get_json() or {}
max_scroll = data.get("max_scroll", 100)
debug_mode = data.get("debug", False)
task_id = str(uuid.uuid4())
task = TaskInfo(
task_id=task_id,
status=TaskStatus.PENDING,
url=config.likes_url,
max_scroll=max_scroll,
created_at=datetime.now(),
progress={"images_processed": 0, "scroll_iterations": 0},
results={},
)
with task_lock:
tasks[task_id] = task
# Start background task
thread = threading.Thread(
target=run_download_task, args=(task_id, debug_mode), daemon=True
)
thread.start()
with task_lock:
active_tasks[task_id] = thread
return jsonify(
{
"status": "success",
"task_id": task_id,
"message": "Download task started",
"debug_enabled": debug_mode,
}
)
except Exception as e:
logger.error(f"Download start error: {e}")
return jsonify({"status": "error", "message": str(e)}), 500
@app.route("/refresh", methods=["POST"])
def refresh():
"""Refresh/restart the current page and download new content."""
try:
data = request.get_json() or {}
max_scroll = data.get("max_scroll", 50)
debug_mode = data.get("debug", False)
# Create a refresh task (similar to download but different semantics)
task_id = str(uuid.uuid4())
task = TaskInfo(
task_id=task_id,
status=TaskStatus.PENDING,
url=config.likes_url,
max_scroll=max_scroll,
created_at=datetime.now(),
progress={"images_processed": 0, "scroll_iterations": 0},
results={},
)
with task_lock:
tasks[task_id] = task
# Start background task
thread = threading.Thread(
target=run_download_task, args=(task_id, debug_mode), daemon=True
)
thread.start()
with task_lock:
active_tasks[task_id] = thread
return jsonify(
{
"status": "success",
"task_id": task_id,
"message": "Refresh task started",
"debug_enabled": debug_mode,
}
)
except Exception as e:
logger.error(f"Refresh error: {e}")
return jsonify({"status": "error", "message": str(e)}), 500
@app.route("/tasks", methods=["GET"])
def get_tasks():
"""Get all tasks."""
with task_lock:
tasks_data = [task.to_dict() for task in tasks.values()]
return jsonify({"tasks": tasks_data})
@app.route("/tasks/<task_id>", methods=["GET"])
def get_task(task_id):
"""Get specific task information."""
with task_lock:
task = tasks.get(task_id)
if not task:
return jsonify({"status": "error", "message": "Task not found"}), 404
return jsonify(task.to_dict())
@app.route("/tasks/<task_id>/cancel", methods=["POST"])
def cancel_task(task_id):
"""Cancel a running task."""
with task_lock:
task = tasks.get(task_id)
if not task:
return jsonify({"status": "error", "message": "Task not found"}), 404
if task.status in [
TaskStatus.COMPLETED,
TaskStatus.FAILED,
TaskStatus.CANCELLED,
]:
return jsonify({"status": "error", "message": "Task already finished"}), 400
task.status = TaskStatus.CANCELLED
task.completed_at = datetime.now()
return jsonify({"status": "success", "message": "Task cancelled"})
@app.route("/health", methods=["GET"])
def health():
"""Health check endpoint."""
return jsonify({"status": "healthy", "timestamp": datetime.now().isoformat()})
@app.route("/", methods=["GET"])
def index():
"""Basic info page."""
return jsonify(
{
"service": "Lyubitori API",
"version": "1.0.0",
"endpoints": {
"GET /status": "Get API status and authentication state",
"POST /download": "Start download task",
"POST /refresh": "Refresh and download new content",
"GET /tasks": "Get all tasks",
"GET /tasks/<id>": "Get specific task",
"POST /tasks/<id>/cancel": "Cancel task",
"GET /health": "Health check",
},
}
)
if __name__ == "__main__":
import atexit
atexit.register(cleanup_driver)
import os
host = os.getenv("API_HOST", "0.0.0.0")
port = int(os.getenv("API_PORT", 5000))
debug = os.getenv("API_DEBUG", "false").lower() == "true"
logger.info(f"Starting Lyubitori API on {host}:{port}")
app.run(host=host, port=port, debug=debug)