-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (37 loc) · 1.22 KB
/
main.py
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
import threading
from flask import Flask, render_template, g
from mechanicalsoup import StatefulBrowser
from database import query_db
from scraper import start_scraper
def create_browser(url, *args, **kwargs) -> StatefulBrowser:
b = StatefulBrowser(*args, **kwargs)
b.open(url)
return b
def create_app() -> Flask:
a = Flask(__name__)
@a.route('/')
def index():
events = []
data = query_db("SELECT * FROM events")
for event in data:
events.append({
"url": event[1],
"title": event[2] if len(event[2]) < 24 else event[2][:21] + "...",
"date": event[3],
"time": event[4],
"price": event[5],
"age": event[6],
})
return render_template('index.html', events=events)
@a.teardown_appcontext
def close_connection(_):
db = getattr(g, '_database', None)
if db is not None:
db.close()
return a
if __name__ == "__main__":
browser = create_browser("https://www.oceanen.com")
app = create_app()
threading.Thread(target=start_scraper, args=(browser, app), daemon=True).start()
app.run(debug=True)
browser.close()