mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-12-17 23:24:20 +00:00
get_or_create -> create
This commit is contained in:
parent
e3c56137c2
commit
468693cec0
5 changed files with 60 additions and 42 deletions
30
db.py
30
db.py
|
@ -4,7 +4,7 @@ from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker, relationship
|
from sqlalchemy.orm import sessionmaker, relationship
|
||||||
from sqlalchemy import Column, BigInteger, Integer, String, Numeric, DateTime, ForeignKey, Float, Enum, create_engine, UniqueConstraint
|
from sqlalchemy import Column, BigInteger, Integer, String, Numeric, DateTime, ForeignKey, Float, Enum, create_engine, UniqueConstraint
|
||||||
import requests
|
import requests
|
||||||
from errors import RequestError, NotFoundError
|
from errors import RequestError, NotFoundError, AlreadyExistingError
|
||||||
import re
|
import re
|
||||||
import enum
|
import enum
|
||||||
|
|
||||||
|
@ -28,10 +28,10 @@ class Royal(Base):
|
||||||
username = Column(String, unique=True, nullable=False)
|
username = Column(String, unique=True, nullable=False)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_or_create(username):
|
def create(username):
|
||||||
r = session.query(Royal).filter_by(username=username).first()
|
r = session.query(Royal).filter_by(username=username).first()
|
||||||
if r is not None:
|
if r is not None:
|
||||||
return r
|
raise AlreadyExistingError(repr(r))
|
||||||
return Royal(username=username)
|
return Royal(username=username)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -85,10 +85,10 @@ class Steam(Base):
|
||||||
return f"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/{self.avatar_hex[0:2]}/{self.avatar_hex}.jpg"
|
return f"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/{self.avatar_hex[0:2]}/{self.avatar_hex}.jpg"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_or_create(royal_id, steam_id):
|
def create(royal_id, steam_id):
|
||||||
s = session.query(Steam).get(steam_id)
|
s = session.query(Steam).get(steam_id)
|
||||||
if s is not None:
|
if s is not None:
|
||||||
return s
|
raise AlreadyExistingError(repr(s))
|
||||||
r = requests.get(f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={config['Steam']['api_key']}&steamids={steam_id}")
|
r = requests.get(f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={config['Steam']['api_key']}&steamids={steam_id}")
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise RequestError(f"Steam returned {r.status_code}")
|
raise RequestError(f"Steam returned {r.status_code}")
|
||||||
|
@ -159,10 +159,10 @@ class RocketLeague(Base):
|
||||||
return f"<RocketLeague {self.steam_id}>"
|
return f"<RocketLeague {self.steam_id}>"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_or_create(steam_id):
|
def create(steam_id):
|
||||||
rl = session.query(RocketLeague).get(steam_id)
|
rl = session.query(RocketLeague).get(steam_id)
|
||||||
if rl is not None:
|
if rl is not None:
|
||||||
return rl
|
raise AlreadyExistingError(repr(rl))
|
||||||
r = requests.get(f"https://api.rocketleaguestats.com/v1/player?apikey={config['Rocket League']['rlstats_api_key']}&unique_id={str(steam_id)}&platform_id=1")
|
r = requests.get(f"https://api.rocketleaguestats.com/v1/player?apikey={config['Rocket League']['rlstats_api_key']}&unique_id={str(steam_id)}&platform_id=1")
|
||||||
if r.status_code == 404:
|
if r.status_code == 404:
|
||||||
raise NotFoundError("The specified user has never played Rocket League")
|
raise NotFoundError("The specified user has never played Rocket League")
|
||||||
|
@ -241,10 +241,10 @@ class Dota(Base):
|
||||||
losses = Column(Integer, nullable=False)
|
losses = Column(Integer, nullable=False)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_or_create(steam_id):
|
def create(steam_id):
|
||||||
d = session.query(Dota).get(steam_id)
|
d = session.query(Dota).get(steam_id)
|
||||||
if d is not None:
|
if d is not None:
|
||||||
return d
|
raise AlreadyExistingError(repr(d))
|
||||||
r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(steam_id)}")
|
r = requests.get(f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(steam_id)}")
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise RequestError("OpenDota returned {r.status_code}")
|
raise RequestError("OpenDota returned {r.status_code}")
|
||||||
|
@ -313,7 +313,7 @@ class LeagueOfLegends(Base):
|
||||||
twtr_rank = Column(Enum(RomanNumerals))
|
twtr_rank = Column(Enum(RomanNumerals))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_or_create(royal_id, summoner_name=None, summoner_id=None):
|
def create(royal_id, summoner_name=None, summoner_id=None):
|
||||||
if summoner_name:
|
if summoner_name:
|
||||||
lol = session.query(LeagueOfLegends).filter(LeagueOfLegends.summoner_name == summoner_name).first()
|
lol = session.query(LeagueOfLegends).filter(LeagueOfLegends.summoner_name == summoner_name).first()
|
||||||
elif summoner_id:
|
elif summoner_id:
|
||||||
|
@ -321,7 +321,7 @@ class LeagueOfLegends(Base):
|
||||||
else:
|
else:
|
||||||
raise SyntaxError("Neither summoner_name or summoner_id are specified")
|
raise SyntaxError("Neither summoner_name or summoner_id are specified")
|
||||||
if lol is not None:
|
if lol is not None:
|
||||||
return lol
|
raise AlreadyExistingError(repr(lol))
|
||||||
# Get the summoner_id
|
# Get the summoner_id
|
||||||
if summoner_name:
|
if summoner_name:
|
||||||
r = requests.get(f"https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/{summoner_name}?api_key={config['League of Legends']['riot_api_key']}")
|
r = requests.get(f"https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/{summoner_name}?api_key={config['League of Legends']['riot_api_key']}")
|
||||||
|
@ -394,10 +394,10 @@ class Osu(Base):
|
||||||
mania_pp = Column(Float)
|
mania_pp = Column(Float)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_or_create(royal_id, osu_name):
|
def create(royal_id, osu_name):
|
||||||
o = session.query(Osu).filter(Osu.osu_name == osu_name).first()
|
o = session.query(Osu).filter(Osu.osu_name == osu_name).first()
|
||||||
if o is not None:
|
if o is not None:
|
||||||
return o
|
raise AlreadyExistingError(repr(o))
|
||||||
r0 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={osu_name}&m=0")
|
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")
|
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")
|
r2 = requests.get(f"https://osu.ppy.sh/api/get_user?k={config['Osu!']['ppy_api_key']}&u={osu_name}&m=2")
|
||||||
|
@ -483,12 +483,12 @@ class Overwatch(Base):
|
||||||
return f"<Overwatch {self}>"
|
return f"<Overwatch {self}>"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_or_create(royal_id, battletag, discriminator=None):
|
def create(royal_id, battletag, discriminator=None):
|
||||||
if discriminator is None:
|
if discriminator is None:
|
||||||
battletag, discriminator = battletag.split("#", 1)
|
battletag, discriminator = battletag.split("#", 1)
|
||||||
o = session.query(Overwatch).filter_by(battletag=battletag, discriminator=discriminator).first()
|
o = session.query(Overwatch).filter_by(battletag=battletag, discriminator=discriminator).first()
|
||||||
if o is not None:
|
if o is not None:
|
||||||
return o
|
raise AlreadyExistingError(repr(o))
|
||||||
r = requests.get(f"https://owapi.net/api/v3/u/{battletag}-{discriminator}/stats", headers={
|
r = requests.get(f"https://owapi.net/api/v3/u/{battletag}-{discriminator}/stats", headers={
|
||||||
"User-Agent": "Royal-Bot/4.0",
|
"User-Agent": "Royal-Bot/4.0",
|
||||||
"From": "ste.pigozzi@gmail.com"
|
"From": "ste.pigozzi@gmail.com"
|
||||||
|
|
|
@ -3,3 +3,6 @@ class RequestError(Exception):
|
||||||
|
|
||||||
class NotFoundError(Exception):
|
class NotFoundError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class AlreadyExistingError(Exception):
|
||||||
|
pass
|
38
newuser.py
38
newuser.py
|
@ -1,42 +1,42 @@
|
||||||
import db
|
import db
|
||||||
|
|
||||||
user = db.Royal.get_or_create(input("Nome account: "))
|
user = db.Royal.create(input("Nome account: "))
|
||||||
db.session.add(user)
|
db.session.add(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
try:
|
try:
|
||||||
steam = db.Steam.get_or_create(user.id, input("Steam ID 1: "))
|
steam = db.Steam.create(user.id, input("Steam ID 1: "))
|
||||||
except KeyboardInterrupt:
|
except Exception as e:
|
||||||
pass
|
print(e)
|
||||||
else:
|
else:
|
||||||
db.session.add(steam)
|
db.session.add(steam)
|
||||||
try:
|
try:
|
||||||
dota = db.Dota.get_or_create(steam.steam_id)
|
dota = db.Dota.create(steam.steam_id)
|
||||||
except:
|
except Exception as e:
|
||||||
pass
|
print(e)
|
||||||
else:
|
else:
|
||||||
db.session.add(dota)
|
db.session.add(dota)
|
||||||
try:
|
try:
|
||||||
rl = db.RocketLeague.get_or_create(steam.steam_id)
|
rl = db.RocketLeague.create(steam.steam_id)
|
||||||
except:
|
except Exception as e:
|
||||||
pass
|
print(e)
|
||||||
else:
|
else:
|
||||||
db.session.add(rl)
|
db.session.add(rl)
|
||||||
try:
|
try:
|
||||||
osu = db.Osu.get_or_create(user.id, input("Osu! username: "))
|
osu = db.Osu.create(user.id, input("Osu! username: "))
|
||||||
except KeyboardInterrupt:
|
except Exception as e:
|
||||||
pass
|
print(e)
|
||||||
else:
|
else:
|
||||||
db.session.add(osu)
|
db.session.add(osu)
|
||||||
try:
|
try:
|
||||||
overwatch = db.Overwatch.get_or_create(user.id, input("Battle.net battletag: "))
|
overwatch = db.Overwatch.create(user.id, input("Battle.net battletag: "))
|
||||||
except KeyboardInterrupt:
|
except Exception as e:
|
||||||
pass
|
print(e)
|
||||||
else:
|
else:
|
||||||
db.session.add(overwatch)
|
db.session.add(overwatch)
|
||||||
try:
|
try:
|
||||||
lol = db.LeagueOfLegends.get_or_create(user.id, input("League summoner name: "))
|
lol = db.LeagueOfLegends.create(user.id, input("League summoner name: "))
|
||||||
except KeyboardInterrupt:
|
except Exception as e:
|
||||||
pass
|
print(e)
|
||||||
else:
|
else:
|
||||||
db.session.add(lol)
|
db.session.add(lol)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
24
update.py
24
update.py
|
@ -7,6 +7,7 @@ try:
|
||||||
# Update Steam
|
# Update Steam
|
||||||
print("STEAM")
|
print("STEAM")
|
||||||
for user in db.session.query(db.Steam).all():
|
for user in db.session.query(db.Steam).all():
|
||||||
|
t = time.clock()
|
||||||
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
|
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
|
||||||
try:
|
try:
|
||||||
user.update()
|
user.update()
|
||||||
|
@ -17,10 +18,12 @@ try:
|
||||||
else:
|
else:
|
||||||
print("OK")
|
print("OK")
|
||||||
finally:
|
finally:
|
||||||
time.sleep(0.5)
|
sleep_time = 1 - time.clock() + t
|
||||||
|
time.sleep(sleep_time if sleep_time > 0 else 0)
|
||||||
# Update Rocket League
|
# Update Rocket League
|
||||||
print("ROCKET LEAGUE")
|
print("ROCKET LEAGUE")
|
||||||
for user in db.session.query(db.RocketLeague).all():
|
for user in db.session.query(db.RocketLeague).all():
|
||||||
|
t = time.clock()
|
||||||
print(f"Updating {user.steam.royal.username}", end="\t\t", flush=True)
|
print(f"Updating {user.steam.royal.username}", end="\t\t", flush=True)
|
||||||
try:
|
try:
|
||||||
user.update()
|
user.update()
|
||||||
|
@ -31,10 +34,12 @@ try:
|
||||||
else:
|
else:
|
||||||
print("OK")
|
print("OK")
|
||||||
finally:
|
finally:
|
||||||
time.sleep(0.5)
|
sleep_time = 1 - time.clock() + t
|
||||||
|
time.sleep(sleep_time if sleep_time > 0 else 0)
|
||||||
# Update Dota 2
|
# Update Dota 2
|
||||||
print("DOTA 2")
|
print("DOTA 2")
|
||||||
for user in db.session.query(db.Dota).all():
|
for user in db.session.query(db.Dota).all():
|
||||||
|
t = time.clock()
|
||||||
print(f"Updating {user.steam.royal.username}", end="\t\t", flush=True)
|
print(f"Updating {user.steam.royal.username}", end="\t\t", flush=True)
|
||||||
try:
|
try:
|
||||||
user.update()
|
user.update()
|
||||||
|
@ -45,10 +50,12 @@ try:
|
||||||
else:
|
else:
|
||||||
print("OK")
|
print("OK")
|
||||||
finally:
|
finally:
|
||||||
time.sleep(0.5)
|
sleep_time = 1 - time.clock() + t
|
||||||
|
time.sleep(sleep_time if sleep_time > 0 else 0)
|
||||||
# Update League of Legends
|
# Update League of Legends
|
||||||
print("LEAGUE OF LEGENDS")
|
print("LEAGUE OF LEGENDS")
|
||||||
for user in db.session.query(db.LeagueOfLegends).all():
|
for user in db.session.query(db.LeagueOfLegends).all():
|
||||||
|
t = time.clock()
|
||||||
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
|
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
|
||||||
try:
|
try:
|
||||||
user.update()
|
user.update()
|
||||||
|
@ -59,10 +66,12 @@ try:
|
||||||
else:
|
else:
|
||||||
print("OK")
|
print("OK")
|
||||||
finally:
|
finally:
|
||||||
time.sleep(0.5)
|
sleep_time = 1 - time.clock() + t
|
||||||
|
time.sleep(sleep_time if sleep_time > 0 else 0)
|
||||||
# Update Osu!
|
# Update Osu!
|
||||||
print("OSU!")
|
print("OSU!")
|
||||||
for user in db.session.query(db.Osu).all():
|
for user in db.session.query(db.Osu).all():
|
||||||
|
t = time.clock()
|
||||||
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
|
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
|
||||||
try:
|
try:
|
||||||
user.update()
|
user.update()
|
||||||
|
@ -73,10 +82,12 @@ try:
|
||||||
else:
|
else:
|
||||||
print("OK")
|
print("OK")
|
||||||
finally:
|
finally:
|
||||||
time.sleep(0.5)
|
sleep_time = 1 - time.clock() + t
|
||||||
|
time.sleep(sleep_time if sleep_time > 0 else 0)
|
||||||
# Update Overwatch
|
# Update Overwatch
|
||||||
print("OVERWATCH")
|
print("OVERWATCH")
|
||||||
for user in db.session.query(db.Overwatch).all():
|
for user in db.session.query(db.Overwatch).all():
|
||||||
|
t = time.clock()
|
||||||
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
|
print(f"Updating {user.royal.username}", end="\t\t", flush=True)
|
||||||
try:
|
try:
|
||||||
user.update()
|
user.update()
|
||||||
|
@ -87,7 +98,8 @@ try:
|
||||||
else:
|
else:
|
||||||
print("OK")
|
print("OK")
|
||||||
finally:
|
finally:
|
||||||
time.sleep(0.5)
|
sleep_time = 1 - time.clock() + t
|
||||||
|
time.sleep(sleep_time if sleep_time > 0 else 0)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from flask import Flask, render_template
|
from flask import Flask, render_template
|
||||||
from db import session, Session, Royal, Steam, RocketLeague, Dota, Osu, Overwatch, LeagueOfLegends
|
from db import session, Royal, Steam, RocketLeague, Dota, Osu, Overwatch, LeagueOfLegends
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@ -16,5 +16,8 @@ def page_leaderboards():
|
||||||
return render_template("leaderboards.html", dota_data=dota_data, rl_data=rl_data, ow_data=ow_data, osu_data=osu_data, lol_data=lol_data)
|
return render_template("leaderboards.html", dota_data=dota_data, rl_data=rl_data, ow_data=ow_data, osu_data=osu_data, lol_data=lol_data)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
app.run(host="0.0.0.0", port=1234, debug=True)
|
app.run(host="0.0.0.0", port=1234, debug=True)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
session.close()
|
session.close()
|
Loading…
Reference in a new issue