Skip to content

Commit ddbb160

Browse files
authored
Scraper scheduler (#126)
* Added staging env * Update scheduler
1 parent 01a5293 commit ddbb160

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Check the `#uplift-backend` channel for the `service-account-key.json` which sho
5050

5151
To run the app:
5252
````bash
53-
python app.py
53+
flask run
5454
````
5555

5656
To run the test suite:

app.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import logging, schedule, time
1+
import logging, schedule
22
from flask import Flask, render_template
3+
from flask_apscheduler import APScheduler
34
from flask_graphql import GraphQLView
45
from graphene import Schema
56
from graphql.utils import schema_printer
67
from src.database import db_session, init_db
7-
from src.models.capacity import Capacity
8-
from src.models.openhours import OpenHours
98
from src.schema import Query
109
from src.scrapers.capacities_scraper import fetch_capacities
1110
from src.scrapers.reg_hours_scraper import fetch_reg_building, fetch_reg_facility
@@ -15,9 +14,13 @@
1514

1615
app = Flask(__name__)
1716
app.debug = True
18-
1917
schema = Schema(query=Query)
2018

19+
# Scheduler
20+
scheduler = APScheduler()
21+
scheduler.init_app(app)
22+
scheduler.start()
23+
2124

2225
@app.route("/")
2326
def 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)
3540
def scrape_sheets():
3641
logging.info("Scraping from sheets...")
3742

@@ -55,10 +60,4 @@ def scrape_sheets():
5560
schema_file.close()
5661

5762
if __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)

requirements.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ certifi==2018.8.24
1212
cfgv==3.3.1
1313
chardet==3.0.4
1414
charset-normalizer==3.2.0
15-
click==6.7
15+
click==8.0
1616
distlib==0.3.7
1717
fastdiff==0.3.0
1818
filelock==3.12.2
19-
Flask==1.0.2
19+
Flask==2.2.5
20+
Flask-APScheduler==1.13.1
2021
Flask-GraphQL==2.0.0
2122
Flask-Migrate==2.4.0
2223
Flask-Script==2.0.5
@@ -33,12 +34,12 @@ gunicorn==19.9.0
3334
identify==2.5.24
3435
idna==2.6
3536
importlib-metadata==6.7.0
36-
itsdangerous==0.24
37+
itsdangerous==2.0
3738
jedi==0.18.2
38-
Jinja2==2.10.1
39+
Jinja2==3.0
3940
lxml==4.9.2
4041
Mako==1.0.9
41-
MarkupSafe==1.1.1
42+
MarkupSafe==2.1.1
4243
marshmallow==3.0.0rc4
4344
marshmallow-sqlalchemy==0.16.2
4445
nodeenv==1.8.0
@@ -74,5 +75,5 @@ virtualenv==20.24.5
7475
wasmer==1.1.0
7576
wasmer-compiler-cranelift==1.1.0
7677
wcwidth==0.2.6
77-
Werkzeug==0.15.5
78+
Werkzeug==2.2.2
7879
zipp==3.15.0

0 commit comments

Comments
 (0)