|
1 | | -import logging |
| 1 | +from app_factory import create_app |
2 | 2 | import sentry_sdk |
3 | | -from flask import Flask, render_template |
4 | | -from flask_apscheduler import APScheduler |
5 | | -from flask_graphql import GraphQLView |
6 | | -from graphene import Schema |
7 | | -from graphql.utils import schema_printer |
8 | | -from src.database import db_session, init_db |
9 | | -from src.schema import Query, Mutation |
10 | | -from src.scrapers.capacities_scraper import fetch_capacities |
11 | | -from src.scrapers.reg_hours_scraper import fetch_reg_building, fetch_reg_facility |
12 | | -from src.scrapers.scraper_helpers import clean_past_hours |
13 | | -from src.scrapers.sp_hours_scraper import fetch_sp_facility |
14 | | -from src.scrapers.equipment_scraper import scrape_equipment |
15 | | -from src.scrapers.class_scraper import fetch_classes |
16 | | -from src.scrapers.activities_scraper import fetch_activity |
17 | | -from src.utils.utils import create_gym_table |
18 | | -from src.models.openhours import OpenHours |
19 | | -from flasgger import Swagger |
| 3 | +import os |
20 | 4 |
|
| 5 | +# Initialize Sentry only if not in local |
| 6 | +if os.environ.get('FLASK_ENV') in ["development", "production"]: |
| 7 | + sentry_sdk.init( |
| 8 | + dsn="https://2a96f65cca45d8a7c3ffc3b878d4346b@o4507365244010496.ingest.us.sentry.io/4507850536386560", |
| 9 | + traces_sample_rate=1.0, |
| 10 | + profiles_sample_rate=1.0, |
| 11 | + ) |
21 | 12 |
|
22 | | -sentry_sdk.init( |
23 | | - dsn="https://2a96f65cca45d8a7c3ffc3b878d4346b@o4507365244010496.ingest.us.sentry.io/4507850536386560", |
24 | | - # Set traces_sample_rate to 1.0 to capture 100% |
25 | | - # of transactions for tracing. |
26 | | - traces_sample_rate=1.0, |
27 | | - # Set profiles_sample_rate to 1.0 to profile 100% |
28 | | - # of sampled transactions. |
29 | | - # We recommend adjusting this value in production. |
30 | | - profiles_sample_rate=1.0, |
31 | | -) |
32 | | - |
33 | | -app = Flask(__name__) |
34 | | -app.debug = True |
35 | | -schema = Schema(query=Query, mutation=Mutation) |
36 | | -swagger = Swagger(app) |
37 | | - |
38 | | -# Scheduler |
39 | | -scheduler = APScheduler() |
40 | | -scheduler.init_app(app) |
41 | | -scheduler.start() |
42 | | - |
43 | | -# Logging |
44 | | -logging.basicConfig(format="%(asctime)s %(levelname)-8s %(message)s", level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S") |
45 | | - |
46 | | - |
47 | | -@app.route("/") |
48 | | -def index(): |
49 | | - return render_template("index.html") |
50 | | - |
51 | | - |
52 | | -app.add_url_rule("/graphql", view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True)) |
53 | | - |
54 | | - |
55 | | -@app.teardown_appcontext |
56 | | -def shutdown_session(exception=None): |
57 | | - db_session.remove() |
58 | | - |
59 | | - |
60 | | -# Scrape hours every 15 minutes |
61 | | -@scheduler.task("interval", id="scrape_hours", seconds=900) |
62 | | -def scrape_hours(): |
63 | | - logging.info("Scraping hours from sheets...") |
64 | | - |
65 | | - # Clear hours |
66 | | - db_session.query(OpenHours).delete() |
67 | | - |
68 | | - fetch_reg_facility() |
69 | | - fetch_reg_building() |
70 | | - fetch_sp_facility() |
71 | | - clean_past_hours() |
72 | | - |
73 | | - |
74 | | -# Scrape capacities every 10 minutes |
75 | | -@scheduler.task("interval", id="scrape_capacities", seconds=600) |
76 | | -def scrape_capacities(): |
77 | | - logging.info("Scraping capacities from C2C...") |
78 | | - |
79 | | - fetch_capacities() |
80 | | - |
81 | | - |
82 | | -# Scrape classes every hour |
83 | | -@scheduler.task("interval", id="scrape_classes", seconds=3600) |
84 | | -def scrape_classes(): |
85 | | - logging.info("Scraping classes from group-fitness-classes...") |
86 | | - |
87 | | - |
88 | | - fetch_classes(10) |
89 | | - |
90 | | - |
91 | | -# Create database and fill it with data |
92 | | -init_db() |
93 | | -create_gym_table() |
94 | | -scrape_classes() |
95 | | -scrape_hours() |
96 | | -scrape_capacities() |
97 | | -scrape_equipment() |
98 | | - |
99 | | -logging.info("Scraping activities from sheets...") |
100 | | -fetch_activity() |
101 | | -# Create schema.graphql |
102 | | -with open("schema.graphql", "w+") as schema_file: |
103 | | - schema_file.write(schema_printer.print_schema(schema)) |
104 | | - schema_file.close() |
| 13 | +# Create Flask app with scrapers enabled |
| 14 | +app = create_app(run_migrations=False) |
105 | 15 |
|
106 | 16 | if __name__ == "__main__": |
107 | | - app.run(host="127.0.0.1", port=5000) |
| 17 | + app.run(host="0.0.0.0", port=5000) |
0 commit comments