-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
54 lines (42 loc) · 1.41 KB
/
scraper.py
File metadata and controls
54 lines (42 loc) · 1.41 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
import logging
import time
import signal
import sys
from apscheduler.schedulers.background import BackgroundScheduler
from src.scrapers.games_scraper import fetch_game_schedule
from src.scrapers.youtube_stats import fetch_videos
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
scheduler = BackgroundScheduler(daemon=True)
def scrape_schedules():
start_time = time.time()
logging.info("Starting scraping games")
fetch_game_schedule()
elapsed_time = time.time() - start_time
logging.info(f"Completed scraping games in {elapsed_time:.2f} seconds")
def scrape_videos():
start_time = time.time()
logging.info("Scraping YouTube videos")
fetch_videos()
elapsed_time = time.time() - start_time
logging.info(f"Completed scraping videos in {elapsed_time:.2f} seconds")
def signal_handler(sig, frame):
logging.info("Shutting down scheduler...")
scheduler.shutdown(wait=True)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
if __name__ == "__main__":
scheduler.add_job(
scrape_schedules, "interval", seconds=60 * 60, id="scrape_schedules"
)
scheduler.add_job(
scrape_videos, "interval", seconds=60 * 60 * 24, id="scrape_videos"
)
scheduler.start()
scrape_schedules()
scrape_videos()
signal.pause()