diff --git a/db.py b/db.py index ce940a7d..de9b0f59 100644 --- a/db.py +++ b/db.py @@ -1,6 +1,9 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy import Column, BigInteger, Integer, String, Numeric, DateTime, ForeignKey, Float, create_engine +import requests +from errors import RequestError +import re # Init the config reader import configparser @@ -56,6 +59,8 @@ class Steam(Base): royal = relationship("Royal") steam_id = Column(String, primary_key=True) + persona_name = Column(String) + avatar_hex = Column(String) def __repr__(self): return f"" @@ -66,6 +71,13 @@ class Steam(Base): else: return self.steam_id + def update(self): + r = requests.get(f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={config['Steam']['api_key']}&steamids={self.steam_id}") + if r.status_code != 200: + raise RequestError(f"Steam returned {r.status_code}") + j = r.json() + self.persona_name = j["response"]["players"][0]["personaname"] + self.avatar_hex = re.search("https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/../(.+).jpg", j["response"]["players"][0]["avatar"]).group(1) # If run as script, create all the tables in the db if __name__ == "__main__": diff --git a/errors.py b/errors.py new file mode 100644 index 00000000..f267ff24 --- /dev/null +++ b/errors.py @@ -0,0 +1,2 @@ +class RequestError(Exception): + pass \ No newline at end of file diff --git a/steam.py b/steam.py index 2bc41273..bc851c54 100644 --- a/steam.py +++ b/steam.py @@ -2,6 +2,7 @@ from flask import Flask from flask import session as flask_session from flask_openid import OpenID from db import session, Royal, Steam +import requests import re # Init the config reader @@ -29,11 +30,19 @@ def page_after_login(response): db_steam = Steam(royal_id=flask_session["royal_id"], steam_id=steam_id) session.add(db_steam) + db_steam.update() session.commit() return "Account Steam collegato con successo!" else: return "Il tuo account Steam è già collegato." +@app.route("/update/") +def page_steam_update(royal_id): + db_steam = session.query(Steam).filter(Steam.royal_id == royal_id).first() + db_steam.update() + return "Dati account aggiornati." + + if __name__ == "__main__": - app.run(host="0.0.0.0", port=config["Steam"]["flask_port"]) \ No newline at end of file + app.run(host="0.0.0.0", port="1234") \ No newline at end of file