1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
This commit is contained in:
Steffo 2017-10-10 23:30:06 +02:00
parent 0d22177de0
commit 0425721103
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: C27544372FBB445D
4 changed files with 156 additions and 13 deletions

18
db.py
View file

@ -257,7 +257,7 @@ class Dota(Base):
if r.status_code != 200:
raise RequestError("OpenDota returned {r.status_code}")
data = r.json()
r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(steam_id)}/wl")
r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(self.steam_id)}/wl")
if r.status_code != 200:
raise RequestError("OpenDota returned {r.status_code}")
wl = r.json()
@ -408,10 +408,10 @@ class Osu(Base):
return new_record
def update(self):
r0 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={osu_name}&m=0")
r1 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={osu_name}&m=1")
r2 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={osu_name}&m=2")
r3 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={osu_name}&m=3")
r0 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={self.osu_name}&m=0")
r1 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={self.osu_name}&m=1")
r2 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={self.osu_name}&m=2")
r3 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={self.osu_name}&m=3")
if r0.status_code != 200 or r1.status_code != 200 or r2.status_code != 200 or r3.status_code != 200:
raise RequestError(
f"Osu! API returned an error ({r0.status_code} {r1.status_code} {r2.status_code} {r3.status_code})")
@ -429,11 +429,3 @@ class Osu(Base):
# If run as script, create all the tables in the db
if __name__ == "__main__":
Base.metadata.create_all(bind=engine)
for player in session.query(Royal).all():
name = input(f"{player}: ")
if name == "":
continue
o = Osu.get_or_create(player.id, name)
print(o)
session.add(o)
session.commit()

29
flaskserver.py Normal file
View file

@ -0,0 +1,29 @@
import db
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/dota/ladder")
def page_dota_ladder():
session = db.Session()
query = session.execute("SELECT royals.username, dota.solo_mmr, dota.party_mmr, dota.wins FROM royals JOIN steam ON royals.id = steam.royal_id JOIN dota ON steam.steam_id = dota.steam_id ORDER BY dota.solo_mmr DESC;")
return render_template("table.htm", query=query)
@app.route("/rl/ladder")
def page_rl_ladder():
session = db.Session()
query = session.execute("SELECT royals.username, rocketleague.single_mmr, rocketleague.doubles_mmr, rocketleague.standard_mmr, rocketleague.solo_std_mmr FROM royals JOIN steam ON royals.id = steam.royal_id JOIN rocketleague ON steam.steam_id = rocketleague.steam_id ORDER BY rocketleague.doubles_mmr DESC;")
return render_template("table.htm", query=query)
@app.route("/osu/ladder")
def page_osu_ladder():
session = db.Session()
query = session.execute("SELECT royals.username, osu.std_pp, osu.taiko_pp, osu.catch_pp, osu.mania_pp FROM royals JOIN osu ON royals.id = osu.royal_id ORDER BY osu.std_pp DESC;")
return render_template("table.htm", query=query)
if __name__ == "__main__":
app.run()

36
templates/table.htm Normal file
View file

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Ladder</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"
integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>
<table class="table">
<thead>
<tr>
{% for column in query.keys() %}
<th>
{{ column }}
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for record in query %}
<tr>
{% for column in record %}
<td>
{% if column %}
{{ column }}
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

86
update.py Normal file
View file

@ -0,0 +1,86 @@
import db
import errors
import time
# Create a new database session
session = db.Session()
# Stop updating if Ctrl-C is pressed
try:
# Update Steam
print("STEAM")
for user in session.query(db.Steam).all():
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
try:
user.update()
except errors.RequestError:
print("Request Error")
except errors.NotFoundError:
print("Not Found Error (?)")
else:
print("OK")
finally:
time.sleep(1)
# Update Rocket League
print("ROCKET LEAGUE")
for user in session.query(db.RocketLeague).all():
print(f"Updating {user.steam.royal.username}", end="\t\t", flush=True)
try:
user.update()
except errors.RequestError:
print("Request Error")
except errors.NotFoundError:
print("Not Found Error (?)")
else:
print("OK")
finally:
time.sleep(1)
# Update Dota 2
print("DOTA 2")
for user in session.query(db.Dota).all():
print(f"Updating {user.steam.royal.username}", end="\t\t", flush=True)
try:
user.update()
except errors.RequestError:
print("Request Error")
except errors.NotFoundError:
print("Not Found Error (?)")
else:
print("OK")
finally:
time.sleep(1)
# Update League of Legends
print("LEAGUE OF LEGENDS")
for user in session.query(db.LeagueOfLegends).all():
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
try:
user.update()
except errors.RequestError:
print("Request Error")
except errors.NotFoundError:
print("Not Found Error (?)")
else:
print("OK")
finally:
time.sleep(1)
# Update Osu!
print("OSU!")
for user in session.query(db.Osu).all():
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
try:
user.update()
except errors.RequestError:
print("Request Error")
except errors.NotFoundError:
print("Not Found Error (?)")
else:
print("OK")
finally:
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
print("Committing...\t\t")
session.commit()
print("OK")