mirror of
https://github.com/skylanix/MamieHenriette.git
synced 2026-02-06 06:40:35 +01:00
Merge pull request #27 from skylanix/humble-bundle-github
Humble bundle GitHub
This commit is contained in:
@@ -1,16 +1,60 @@
|
||||
import logging
|
||||
import json
|
||||
import os
|
||||
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from sqlite3 import Cursor, Connection
|
||||
from webapp import webapp
|
||||
|
||||
webapp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
|
||||
|
||||
basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
|
||||
webapp.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{os.path.join(basedir, "instance", "database.db")}'
|
||||
db = SQLAlchemy(webapp)
|
||||
|
||||
def _tableHaveColumn(table_name:str, column_name:str, cursor:Cursor) -> bool:
|
||||
cursor.execute(f'PRAGMA table_info({table_name})')
|
||||
columns = cursor.fetchall()
|
||||
return any(col[1] == column_name for col in columns)
|
||||
|
||||
def _tableEmpty(table:str, cursor:Cursor) -> bool:
|
||||
return cursor.execute(f'SELECT COUNT(*) FROM {table}').fetchone()[0] == 0
|
||||
|
||||
def _renameTable(old_name:str, new_name:str, cursor:Cursor) :
|
||||
cursor.execute(f'ALTER TABLE {old_name} RENAME TO {new_name}')
|
||||
|
||||
def _dropTable(table_name:str, cursor:Cursor) :
|
||||
cursor.execute(f'DROP TABLE {table_name}')
|
||||
|
||||
def _doPreImportMigration(cursor:Cursor):
|
||||
if _tableHaveColumn('game_bundle', 'id', cursor) :
|
||||
logging.info("Table game_bundle détécté, rennomage en game_bundle_old")
|
||||
_renameTable('game_bundle', 'game_bundle_old', cursor)
|
||||
|
||||
def _doPostImportMigration(cursor:Cursor):
|
||||
if _tableEmpty('game_bundle', cursor) :
|
||||
logging.info("remplir game_bundle avec game_bundle_old")
|
||||
bundles = cursor.execute(f'SELECT * FROM game_bundle_old').fetchall()
|
||||
for bundle in bundles :
|
||||
name = bundle[1]
|
||||
json_data = json.loads(bundle[2])
|
||||
url = json_data['url']
|
||||
logging.info(f'import du bundle {name}, {url}')
|
||||
cursor.execute('INSERT INTO game_bundle(url, name, json) VALUES (?, ?, ?)', (url, name, json.dumps(json_data)))
|
||||
logging.info("suppression de la table temporaire game_bundle_old")
|
||||
_dropTable('game_bundle_old', cursor)
|
||||
|
||||
with webapp.app_context():
|
||||
with open('database/schema.sql', 'r') as f:
|
||||
sql = f.read()
|
||||
connection = db.session.connection().connection
|
||||
connection : Connection = db.session.connection().connection
|
||||
try:
|
||||
cursor = connection.cursor()
|
||||
cursor : Cursor = connection.cursor()
|
||||
_doPreImportMigration(cursor)
|
||||
cursor.executescript(sql)
|
||||
_doPostImportMigration(cursor)
|
||||
connection.commit()
|
||||
cursor.close()
|
||||
except Exception as e:
|
||||
logging.error(f"lors de l'import de la bdd : {e}")
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
);
|
||||
|
||||
|
||||
@@ -11,18 +11,24 @@ from discord import Client
|
||||
|
||||
def _isEnable():
|
||||
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
|
||||
|
||||
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}")
|
||||
|
||||
@@ -53,8 +53,6 @@
|
||||
{{channel.name}}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<label for="humble_bundle_api_key">Clé API</label>
|
||||
<input name="humble_bundle_api_key" type="text" value="{{ configuration.getValue('humble_bundle_api_key') }}" />
|
||||
<input type="Submit" value="Définir">
|
||||
</form>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user