From f39b0778445760bb8fbaa83b98173ba71c82f253 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 8 Oct 2017 00:43:41 +0200 Subject: [PATCH] let's try this again --- db.py | 7 +++++-- rocket_league.py | 21 ------------------- steam.py | 48 -------------------------------------------- telebot.py | 52 ------------------------------------------------ 4 files changed, 5 insertions(+), 123 deletions(-) delete mode 100644 rocket_league.py delete mode 100644 steam.py delete mode 100644 telebot.py diff --git a/db.py b/db.py index 61fa2843..6d2115b5 100644 --- a/db.py +++ b/db.py @@ -61,6 +61,7 @@ class Steam(Base): steam_id = Column(String, primary_key=True) persona_name = Column(String) avatar_hex = Column(String) + trade_token = Column(String) def __repr__(self): return f"" @@ -114,9 +115,11 @@ class RocketLeague(Base): rl = session.query(RocketLeague).filter(RocketLeague.steam_id == steam_id).first() if rl is not None: return None - r = requests.get(f"https://api.rocketleaguestats.com/v1/player?apikey={config['Rocket League']['rlstats_api_key']}&unique_id={steam_id}&platform_id=1") - if r.status_code != 200: + 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: return None + elif r.status_code == 500: + raise RequestError("Rocket League Stats returned {r.status_code}") new_record = RocketLeague(steam_id=steam_id) new_record.update() return new_record diff --git a/rocket_league.py b/rocket_league.py deleted file mode 100644 index b57979f0..00000000 --- a/rocket_league.py +++ /dev/null @@ -1,21 +0,0 @@ -from db import session, Steam, RocketLeague -from time import sleep - -def check_for_new_players(): - steam_users = session.query(Steam).all() - for user in steam_users: - print(user) - rl = RocketLeague.check_and_create(user.steam_id) - if rl is not None: - session.add(rl) - sleep(1) - session.commit() - -def update_existing_players(): - rocket_league_players = session.query(RocketLeague).all() - for player in rocket_league_players: - player.update() - session.commit() - -if __name__ == "__main__": - check_for_new_players() diff --git a/steam.py b/steam.py deleted file mode 100644 index bc851c54..00000000 --- a/steam.py +++ /dev/null @@ -1,48 +0,0 @@ -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 -import configparser -config = configparser.ConfigParser() -config.read("config.ini") - -# Init Flask and Flask_OpenID -app = Flask(__name__) -app.secret_key = config["Steam"]["secret_key"] -oid = OpenID(app) - -@app.route("/login/") -@oid.loginhandler -def page_steam_login(royal_id): - flask_session["royal_id"] = royal_id - return oid.try_login("http://steamcommunity.com/openid") - - -@oid.after_login -def page_after_login(response): - steam_id = re.search("https?://steamcommunity\.com/openid/id/(.+)", response.identity_url).group(1) - db_steam = session.query(Steam).filter(Steam.steam_id == steam_id).first() - if db_steam is None: - 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="1234") \ No newline at end of file diff --git a/telebot.py b/telebot.py deleted file mode 100644 index 518cdaa2..00000000 --- a/telebot.py +++ /dev/null @@ -1,52 +0,0 @@ -from telegram import Bot, Update -from telegram.ext import Updater, CommandHandler -import logging -from db import session, Royal, Telegram - -# Init the config reader -import configparser -config = configparser.ConfigParser() -config.read("config.ini") - -# Init the logger -logging.basicConfig(level=logging.DEBUG, format="[%(levelname)s] %(asctime)s | %(message)s") - -# Init the Telegram Bot -updater = Updater(token=config["Telegram"]["bot_token"]) - - -def message_sync(bot: Bot, update: Update): - tg_user = update.message.from_user - db_user = session.query(Telegram).filter(Telegram.telegram_id == tg_user.id).first() - if db_user is None: - # Find the royals table record matching the command argument - try: - tg_royal = update.message.text.split(" ", 1)[1] - except IndexError: - bot.send_message(update.message.chat.id, "⚠️ Non hai specificato nessun username!") - return - db_royal = session.query(Royal).filter(Royal.username == tg_royal).first() - if db_royal is None: - bot.send_message(update.message.chat.id, "⚠️ L'username che hai specificato non è valido!") - return - # Create the new user and link it to the royals table - db_user = Telegram(royal_id=db_royal.id, - telegram_id=tg_user.id, - first_name=tg_user.first_name, - last_name=tg_user.last_name, - username=tg_user.username) - session.add(db_user) - session.commit() - else: - # Update user data - db_user.first_name = tg_user.first_name - db_user.last_name = tg_user.last_name - db_user.username = tg_user.username - session.commit() - bot.send_message(update.message.chat.id, f"✅ Sincronizzazione Telegram completata!\n" - f"Sincronizza il tuo account Steam qui:\n" - f"http://79.26.47.209:1234/login/{db_user.royal_id}") - - -updater.dispatcher.add_handler(CommandHandler("sync", message_sync)) -updater.start_polling() \ No newline at end of file