Nouvelle api pour humble bundle

This commit is contained in:
Kepka Ludovic
2025-09-13 09:08:09 +02:00
parent 991966ebb3
commit 6158eee9b1
3 changed files with 19 additions and 13 deletions

View File

@@ -15,9 +15,9 @@ class GameAlias(db.Model):
name = db.Column(db.String(256)) name = db.Column(db.String(256))
class GameBundle(db.Model): 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)) name = db.Column(db.String(256))
json = db.Column(db.String(1024)) json = db.Column(db.String(2048))
class LiveAlert(db.Model): class LiveAlert(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)

View File

@@ -11,9 +11,9 @@ CREATE TABLE IF NOT EXISTS `game_alias` (
); );
CREATE TABLE IF NOT EXISTS `game_bundle` ( CREATE TABLE IF NOT EXISTS `game_bundle` (
id INTEGER PRIMARY KEY, url VARCHAR(2048) PRIMARY KEY,
name VARCHAR(256) NOT NULL, name VARCHAR(256) NOT NULL,
json VARCHAR(1024) NOT NULL json VARCHAR(2048) NOT NULL
); );
CREATE TABLE IF NOT EXISTS `humeur` ( CREATE TABLE IF NOT EXISTS `humeur` (
@@ -45,4 +45,3 @@ CREATE TABLE IF NOT EXISTS `commande` (
`trigger` VARCHAR(16) UNIQUE NOT NULL, `trigger` VARCHAR(16) UNIQUE NOT NULL,
`response` VARCHAR(2000) NOT NULL `response` VARCHAR(2000) NOT NULL
); );

View File

@@ -13,16 +13,22 @@ def _isEnable():
helper = ConfigurationHelper() helper = ConfigurationHelper()
return helper.getValue('humble_bundle_enable') and helper.getIntValue('humble_bundle_channel') != 0 and helper.getValue('humble_bundle_api_key') return helper.getValue('humble_bundle_enable') and helper.getIntValue('humble_bundle_channel') != 0 and helper.getValue('humble_bundle_api_key')
def _callHexas(): def _callGithub():
headers = { "Content-Type": "application/json", "api-key":ConfigurationHelper().getValue('humble_bundle_api_key') } response = requests.get("https://raw.githubusercontent.com/shionn/HumbleBundleGamePack/refs/heads/master/data/game-bundles.json")
response = requests.get("http://hexas.shionn.org/humble-bundle/json", headers=headers)
if response.status_code == 200: if response.status_code == 200:
return response.json() 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 return None
def _isNotAlreadyNotified(bundle): 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): def _formatMessage(bundle):
choice = bundle['choices'][0] choice = bundle['choices'][0]
@@ -36,11 +42,12 @@ def _formatMessage(bundle):
async def checkHumbleBundleAndNotify(bot: Client): async def checkHumbleBundleAndNotify(bot: Client):
if _isEnable() : if _isEnable() :
try : try :
bundle = _callHexas() bundles = _callGithub()
if bundle != None and _isNotAlreadyNotified(bundle) : bundle = _findFirstNotNotified(bundles)
if bundle != None :
message = _formatMessage(bundle) message = _formatMessage(bundle)
await bot.get_channel(ConfigurationHelper().getIntValue('humble_bundle_channel')).send(message) 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() db.session.commit()
except Exception as e: except Exception as e:
logging.error(f"Échec de la vérification des offres Humble Bundle : {e}") logging.error(f"Échec de la vérification des offres Humble Bundle : {e}")