1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add steam persona_name and avatar info

This commit is contained in:
Steffo 2017-10-05 09:47:59 +02:00
parent f3338c2fe9
commit f9995666e2
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
3 changed files with 24 additions and 1 deletions

12
db.py
View file

@ -1,6 +1,9 @@
from sqlalchemy.ext.declarative import declarative_base 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, create_engine 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 # Init the config reader
import configparser import configparser
@ -56,6 +59,8 @@ class Steam(Base):
royal = relationship("Royal") royal = relationship("Royal")
steam_id = Column(String, primary_key=True) steam_id = Column(String, primary_key=True)
persona_name = Column(String)
avatar_hex = Column(String)
def __repr__(self): def __repr__(self):
return f"<Steam {self.steam_id}>" return f"<Steam {self.steam_id}>"
@ -66,6 +71,13 @@ class Steam(Base):
else: else:
return self.steam_id 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 run as script, create all the tables in the db
if __name__ == "__main__": if __name__ == "__main__":

2
errors.py Normal file
View file

@ -0,0 +1,2 @@
class RequestError(Exception):
pass

View file

@ -2,6 +2,7 @@ from flask import Flask
from flask import session as flask_session from flask import session as flask_session
from flask_openid import OpenID from flask_openid import OpenID
from db import session, Royal, Steam from db import session, Royal, Steam
import requests
import re import re
# Init the config reader # Init the config reader
@ -29,11 +30,19 @@ def page_after_login(response):
db_steam = Steam(royal_id=flask_session["royal_id"], db_steam = Steam(royal_id=flask_session["royal_id"],
steam_id=steam_id) steam_id=steam_id)
session.add(db_steam) session.add(db_steam)
db_steam.update()
session.commit() session.commit()
return "Account Steam collegato con successo!" return "Account Steam collegato con successo!"
else: else:
return "Il tuo account Steam è già collegato." return "Il tuo account Steam è già collegato."
@app.route("/update/<int:royal_id>")
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__": if __name__ == "__main__":
app.run(host="0.0.0.0", port=config["Steam"]["flask_port"]) app.run(host="0.0.0.0", port="1234")