-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
114 lines (98 loc) · 3.19 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# app.py
import sqlite3
from flask import Flask, jsonify, render_template
from flask_socketio import SocketIO, emit
from wfmapi import update_database, print_item
app = Flask(__name__)
socketio = SocketIO(app)
@app.route('/')
def home():
# update_database()
conn = sqlite3.connect('mod_database.db')
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute("SELECT id, name FROM Faction")
factions = cursor.fetchall()
cursor.execute(r"""
SELECT
Mod.name,
ROUND(MAX(Price48hs,Price90d), 2) AS MaxAvgSold,
ROUND(offerPrice, 2) AS offerPrice,
ROUND(mostRepeatedOffer, 2) AS mostRepeatedOffer,
GROUP_CONCAT(Faction.name, ', ') AS factionNames,
Mod.url_name,
amount48,
amount90,
Faction.id as factionId,
strftime('%Y-%m-%d %H:%M:%S', Mod.lastUpdated) AS lastUpdated
FROM Mod
JOIN Mod_Faction ON Mod.id = Mod_Faction.mod_id
JOIN Faction ON Mod_Faction.faction_id = Faction.id
GROUP BY Mod.name
ORDER BY amount48 DESC
""")
mods = cursor.fetchall()
return render_template('index.html',factions=factions,mods=mods)
@app.route('/mods/<faction_id>/<orderType>')
def get_mods_by_faction(faction_id, orderType="demand"):
faction_filter = "" if faction_id == "0" else "WHERE Mod_Faction.faction_id IN ({})".format(faction_id)
queryString = f"""
SELECT
Mod.name,
ROUND(MAX(Price48hs, Price90d), 2) AS MaxAvgSold,
ROUND(offerPrice, 2) AS offerPrice,
ROUND(mostRepeatedOffer, 2) AS mostRepeatedOffer,
factionNames,
Mod.url_name,
amount48,
amount90,
Faction.id as factionId,
strftime('%Y-%m-%d %H:%M:%S', Mod.lastUpdated) AS lastUpdated
FROM Mod
JOIN
(SELECT
Mod_Faction.mod_id,
GROUP_CONCAT(Faction.name, ', ') as factionNames
FROM
Mod_Faction
JOIN
Faction ON Mod_Faction.faction_id = Faction.id
GROUP BY
Mod_Faction.mod_id) AS factionsSubquery
ON Mod.id = factionsSubquery.mod_id
JOIN Mod_Faction ON Mod.id = Mod_Faction.mod_id
JOIN Faction ON Mod_Faction.faction_id = Faction.id
{faction_filter}
GROUP BY Mod.name
"""
if orderType == "price":
queryString += " ORDER BY offerPrice DESC"
else:
queryString += " ORDER BY amount48 DESC"
conn = sqlite3.connect('mod_database.db')
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute(queryString)
mods = cursor.fetchall()
mod_list = [dict(mod) for mod in mods]
conn.close()
return jsonify(mod_list)
# def updateMods(faction_id,orderType="demand"):
# @app.route('/updateMods')
# def updateMods():
# try:
# update_database()
# return "ok"
# except Exception as e:
# return str(e), 500
@socketio.on('start_updateMods')
def handle_start_task():
try:
update_database()
return "ok"
except Exception as e:
print("error: ",e)
emit('error', e.with_traceback())
return str(e), 500
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0')