-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (55 loc) · 1.92 KB
/
main.py
File metadata and controls
72 lines (55 loc) · 1.92 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 asyncio
import signal
import sys
import aiohttp
from core.github_scraper import run_full_scraper
from core.storage import save_results
from core.utils import log
fetched_matches = []
save_task = None # reference to the background save coroutine
def handle_shutdown(signum, frame):
log("🛑 Shutdown signal received. Saving progress...")
if fetched_matches:
save_results(fetched_matches)
log(f"💾 Saved {len(fetched_matches)} results before exit.")
sys.exit(0)
signal.signal(signal.SIGINT, handle_shutdown)
signal.signal(signal.SIGTERM, handle_shutdown)
async def periodic_saver():
"""Saves fetched matches every 10 seconds."""
while True:
await asyncio.sleep(10)
if fetched_matches:
save_results(fetched_matches)
log(f"💾 Auto-saved {len(fetched_matches)} results.")
async def main():
global fetched_matches, save_task
log("🚀 Starting API Miner...")
# Start periodic save task
save_task = asyncio.create_task(periodic_saver())
try:
fetched_matches = await run_full_scraper()
except aiohttp.ClientConnectorError as e:
log(f"🌐 Internet disconnected: {e}")
save_results(fetched_matches)
log(f"💾 Saved {len(fetched_matches)} results after net issue.")
sys.exit(1)
except Exception as e:
log(f"❌ Unexpected error: {e}")
save_results(fetched_matches)
log(f"💾 Saved {len(fetched_matches)} results after error.")
sys.exit(1)
else:
save_results(fetched_matches)
log(f"✅ Mining completed. {len(fetched_matches)} secrets saved.")
# Cancel the periodic save task after scraping completes
save_task.cancel()
try:
await save_task
except asyncio.CancelledError:
pass
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
handle_shutdown(signal.SIGINT, None)