This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathapp.py
42 lines (35 loc) · 1.41 KB
/
app.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
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
import sqlite3
import json
import zlib
import config
import os
app = Flask(__name__)
socketio = SocketIO(app)
socketio.init_app(app, cors_allowed_origins="*")
ad = os.environ.get("AD_CODE")
@app.route("/", methods=["GET"])
def root():
return render_template("index.html", screener=sorted(config.SCREENER.items(), key=lambda x: x[1]), ad=ad)
@socketio.on("search_event")
def handle_search_event(data):
screener = data["screener"]
query = data["query"]
page = data["page"]
q = f"%{query}%"
with sqlite3.connect("tradingview.db") as con:
db = con.cursor()
if screener == "all":
# https://xkcd.com/327/
rows = db.execute("SELECT * FROM tv WHERE exchange LIKE ? OR symbol LIKE ? OR desc LIKE ? LIMIT ? OFFSET ?",
(q, q, q, config.PAGE_SIZE, (page - 1) * config.PAGE_SIZE))
else:
# https://xkcd.com/327/
rows = db.execute("SELECT * FROM tv WHERE screener = ? AND (exchange LIKE ? OR symbol LIKE ? OR desc LIKE ?) LIMIT ? OFFSET ?",
(screener, q, q, q, config.PAGE_SIZE, (page - 1) * config.PAGE_SIZE))
emit('response', {"r": zlib.compress(
bytes(json.dumps(list(rows)), "utf-8"))})
if __name__ == '__main__':
print("Starting server: http://localhost:5000")
socketio.run(app, debug=False)