mirror of
https://github.com/skylanix/MamieHenriette.git
synced 2026-02-11 18:10:37 +01:00
Merge remote-tracking branch 'origin/main' into heatcheck
This commit is contained in:
@@ -1,16 +1,60 @@
|
|||||||
|
import logging
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from sqlite3 import Cursor, Connection
|
||||||
from webapp import webapp
|
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)
|
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 webapp.app_context():
|
||||||
with open('database/schema.sql', 'r') as f:
|
with open('database/schema.sql', 'r') as f:
|
||||||
sql = f.read()
|
sql = f.read()
|
||||||
connection = db.session.connection().connection
|
connection : Connection = db.session.connection().connection
|
||||||
try:
|
try:
|
||||||
cursor = connection.cursor()
|
cursor : Cursor = connection.cursor()
|
||||||
|
_doPreImportMigration(cursor)
|
||||||
cursor.executescript(sql)
|
cursor.executescript(sql)
|
||||||
|
_doPostImportMigration(cursor)
|
||||||
|
connection.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"lors de l'import de la bdd : {e}")
|
||||||
finally:
|
finally:
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -11,18 +11,24 @@ from discord import Client
|
|||||||
|
|
||||||
def _isEnable():
|
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
|
||||||
|
|
||||||
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}")
|
||||||
|
|||||||
@@ -53,8 +53,6 @@
|
|||||||
{{channel.name}}</option>
|
{{channel.name}}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</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">
|
<input type="Submit" value="Définir">
|
||||||
</form>
|
</form>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user