diff --git a/database/models.py b/database/models.py index ed24f51..308f11c 100644 --- a/database/models.py +++ b/database/models.py @@ -15,9 +15,9 @@ class GameAlias(db.Model): name = db.Column(db.String(256)) class GameBundle(db.Model): - id = db.Column(db.Integer, primary_key=True) + url = db.Column(db.String(2048), primary_key=True) name = db.Column(db.String(256)) - json = db.Column(db.String(1024)) + json = db.Column(db.String(2048)) class LiveAlert(db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/database/schema.sql b/database/schema.sql index 2777bba..de38db0 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -11,9 +11,9 @@ CREATE TABLE IF NOT EXISTS `game_alias` ( ); CREATE TABLE IF NOT EXISTS `game_bundle` ( - id INTEGER PRIMARY KEY, + url VARCHAR(2048) PRIMARY KEY, name VARCHAR(256) NOT NULL, - json VARCHAR(1024) NOT NULL + json VARCHAR(2048) NOT NULL ); CREATE TABLE IF NOT EXISTS `humeur` ( @@ -45,4 +45,3 @@ CREATE TABLE IF NOT EXISTS `commande` ( `trigger` VARCHAR(16) UNIQUE NOT NULL, `response` VARCHAR(2000) NOT NULL ); - diff --git a/discordbot/humblebundle.py b/discordbot/humblebundle.py index 7458752..f1887a8 100644 --- a/discordbot/humblebundle.py +++ b/discordbot/humblebundle.py @@ -13,16 +13,22 @@ def _isEnable(): helper = ConfigurationHelper() return helper.getValue('humble_bundle_enable') and helper.getIntValue('humble_bundle_channel') != 0 and helper.getValue('humble_bundle_api_key') -def _callHexas(): - headers = { "Content-Type": "application/json", "api-key":ConfigurationHelper().getValue('humble_bundle_api_key') } - response = requests.get("http://hexas.shionn.org/humble-bundle/json", headers=headers) +def _callGithub(): + response = requests.get("https://raw.githubusercontent.com/shionn/HumbleBundleGamePack/refs/heads/master/data/game-bundles.json") if response.status_code == 200: return response.json() - logging.error(f"Échec de la connexion à l'API Humble Bundle. Code de statut HTTP : {response.status_code}") + logging.error(f"Échec de la connexion à la ressource Humble Bundle. Code de statut HTTP : {response.status_code}") return None def _isNotAlreadyNotified(bundle): - return GameBundle.query.filter_by(id=bundle['id']).first() == None + return GameBundle.query.filter_by(url=bundle['url']).first() == None + +def _findFirstNotNotified(bundles) : + if bundles != None : + for bundle in bundles: + if _isNotAlreadyNotified(bundle) : + return bundle + return None def _formatMessage(bundle): choice = bundle['choices'][0] @@ -36,11 +42,12 @@ def _formatMessage(bundle): async def checkHumbleBundleAndNotify(bot: Client): if _isEnable() : try : - bundle = _callHexas() - if bundle != None and _isNotAlreadyNotified(bundle) : + bundles = _callGithub() + bundle = _findFirstNotNotified(bundles) + if bundle != None : message = _formatMessage(bundle) await bot.get_channel(ConfigurationHelper().getIntValue('humble_bundle_channel')).send(message) - db.session.add(GameBundle(id=bundle['id'], name=bundle['name'], json = json.dumps(bundle))) + db.session.add(GameBundle(url=bundle['url'], name=bundle['name'], json = json.dumps(bundle))) db.session.commit() except Exception as e: logging.error(f"Échec de la vérification des offres Humble Bundle : {e}")