File size: 521 Bytes
512aebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import threading
from fastapi import FastAPI
from datetime import datetime
from scraper import main  # our scraping entrypoint

app = FastAPI()
_started = False

@app.on_event("startup")
def kick_off_scraper():
    global _started
    if not _started:
        _started = True
        thread = threading.Thread(target=main, daemon=True)
        thread.start()

@app.get("/")
def read_root():
    return {
        "status": "scraper is running in background",
        "timestamp": datetime.utcnow().isoformat() + "Z"
    }