prince1604 commited on
Commit ·
c50a4e1
1
Parent(s): 059751d
Add Automatic 4-Hour Crawler Scheduler
Browse files
api.py
CHANGED
|
@@ -45,9 +45,59 @@ class KeepAlive(threading.Thread):
|
|
| 45 |
print(f"[KeepAlive] Error: {e}")
|
| 46 |
time.sleep(60)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
pinger.start()
|
|
|
|
|
|
|
|
|
|
| 51 |
# ----------------------------
|
| 52 |
|
| 53 |
@app.route('/')
|
|
|
|
| 45 |
print(f"[KeepAlive] Error: {e}")
|
| 46 |
time.sleep(60)
|
| 47 |
|
| 48 |
+
class ScheduledCrawler(threading.Thread):
|
| 49 |
+
def __init__(self, interval=14400): # 14400 seconds = 4 Hours
|
| 50 |
+
super().__init__()
|
| 51 |
+
self.interval = interval
|
| 52 |
+
self.daemon = True
|
| 53 |
+
self.running = True
|
| 54 |
+
|
| 55 |
+
def run(self):
|
| 56 |
+
print(f"[Scheduler] Auto-Crawler initialized. Schedule: Every {self.interval/3600} hours.")
|
| 57 |
+
# Initial delay to let server start
|
| 58 |
+
time.sleep(60)
|
| 59 |
+
|
| 60 |
+
while self.running:
|
| 61 |
+
try:
|
| 62 |
+
# 1. Identify Target
|
| 63 |
+
# User configures this via Environment Variable
|
| 64 |
+
target_domain = os.environ.get("AUTO_CRAWL_TARGET")
|
| 65 |
+
|
| 66 |
+
if target_domain:
|
| 67 |
+
print(f"\n[Scheduler] 🕒 Triggering scheduled crawl for: {target_domain}")
|
| 68 |
+
|
| 69 |
+
# 2. Run Crawl (Reuse logic via internal call or simulating request)
|
| 70 |
+
# We instantiate the classes directly to avoid network overhead
|
| 71 |
+
crawler = Crawler()
|
| 72 |
+
site_data, _, _ = crawler.crawl_domain(target_domain, max_pages=50) # Limit to 50 for auto-runs
|
| 73 |
+
|
| 74 |
+
if site_data:
|
| 75 |
+
analyzer = ImageAnalyzer()
|
| 76 |
+
results = analyzer.analyze_site(site_data)
|
| 77 |
+
|
| 78 |
+
# Save Report
|
| 79 |
+
with open(REPORT_FILE, 'w', encoding='utf-8') as f:
|
| 80 |
+
json.dump(results, f, indent=4)
|
| 81 |
+
|
| 82 |
+
print(f"[Scheduler] ✅ Crawl finished for {target_domain}. Report saved.")
|
| 83 |
+
else:
|
| 84 |
+
print(f"[Scheduler] ⚠️ Crawl returned no data.")
|
| 85 |
+
else:
|
| 86 |
+
print("[Scheduler] ℹ️ waiting... (Set 'AUTO_CRAWL_TARGET' Env Var to enable auto-crawling)")
|
| 87 |
+
|
| 88 |
+
# 3. Wait for next interval
|
| 89 |
+
time.sleep(self.interval)
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(f"[Scheduler] Error: {e}")
|
| 93 |
+
time.sleep(60)
|
| 94 |
+
|
| 95 |
+
# Start KeepAlive & Scheduler
|
| 96 |
+
pinger = KeepAlive(interval=300)
|
| 97 |
pinger.start()
|
| 98 |
+
|
| 99 |
+
scheduler = ScheduledCrawler(interval=14400) # 4 Hours
|
| 100 |
+
scheduler.start()
|
| 101 |
# ----------------------------
|
| 102 |
|
| 103 |
@app.route('/')
|