1- import logging , schedule , time
1+ import logging , schedule
22from flask import Flask , render_template
3+ from flask_apscheduler import APScheduler
34from flask_graphql import GraphQLView
45from graphene import Schema
56from graphql .utils import schema_printer
67from src .database import db_session , init_db
7- from src .models .capacity import Capacity
8- from src .models .openhours import OpenHours
98from src .schema import Query
109from src .scrapers .capacities_scraper import fetch_capacities
1110from src .scrapers .reg_hours_scraper import fetch_reg_building , fetch_reg_facility
1514
1615app = Flask (__name__ )
1716app .debug = True
18-
1917schema = Schema (query = Query )
2018
19+ # Scheduler
20+ scheduler = APScheduler ()
21+ scheduler .init_app (app )
22+ scheduler .start ()
23+
2124
2225@app .route ("/" )
2326def index ():
@@ -32,6 +35,8 @@ def shutdown_session(exception=None):
3235 db_session .remove ()
3336
3437
38+ # Scrape every 15 minutes
39+ @scheduler .task ("interval" , id = "scrape_sheets" , seconds = 900 )
3540def scrape_sheets ():
3641 logging .info ("Scraping from sheets..." )
3742
@@ -55,10 +60,4 @@ def scrape_sheets():
5560 schema_file .close ()
5661
5762if __name__ == "__main__" :
58- app .run (host = "127.0.0.1" , port = 5000 )
59-
60- # Schedule the scraping to run every 10 minutes
61- schedule .every (10 ).minutes .do (scrape_sheets )
62- while True :
63- schedule .run_pending ()
64- time .sleep (60 )
63+ app .run (host = "0.0.0.0" , port = 5000 )
0 commit comments