Extraction code humble bundle pour une meileur lecture

This commit is contained in:
Kepka Ludovic
2025-08-21 08:03:57 +02:00
committed by skylanix
parent fca1fbd02d
commit 762873f3b5
3 changed files with 52 additions and 27 deletions

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@
**/.venv **/.venv
__pycache__ __pycache__
instance instance
.tio.tokens.json

View File

@@ -1,17 +1,14 @@
import asyncio import asyncio
import datetime
import discord import discord
import json
import logging import logging
import random import random
import requests
from database import db from database import db
from database.helpers import ConfigurationHelper from database.helpers import ConfigurationHelper
from database.models import Configuration, GameBundle, Humeur, Commande from database.models import Configuration, Humeur, Commande
from protondb import searhProtonDb
from discord import Message from discord import Message
from discordbot.humblebundle import checkHumbleBundleAndNotify
from protondb import searhProtonDb
class DiscordBot(discord.Client): class DiscordBot(discord.Client):
async def on_ready(self): async def on_ready(self):
@@ -35,27 +32,7 @@ class DiscordBot(discord.Client):
async def updateHumbleBundle(self): async def updateHumbleBundle(self):
while not self.is_closed(): while not self.is_closed():
if ConfigurationHelper().getValue('humble_bundle_enable') and ConfigurationHelper().getIntValue('humble_bundle_channel') != 0 : await checkHumbleBundleAndNotify(self)
try :
response = requests.get("http://hexas.shionn.org/humble-bundle/json", headers={ "Content-Type": "application/json" })
if response.status_code == 200:
bundle = response.json()
if GameBundle.query.filter_by(id=bundle['id']).first() == None :
choice = bundle['choices'][0]
date = datetime.datetime.fromtimestamp(bundle['endDate']/1000,datetime.UTC).strftime("%d %B %Y")
message = f"@here **Humble Bundle** propose un pack de jeu [{bundle['name']}]({bundle['url']}) contenant :\n"
for game in choice["games"]:
message += f"- {game}\n"
message += f"Pour {choice['price']}€, disponible jusqu'au {date}."
await self.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.commit()
else:
logging.error(f"Erreur de connexion {response.status_code}")
except Exception as e:
logging.error(f"Erreur de connexion {e}")
else:
logging.info('Humble bundle est désactivé')
# toute les 30 minutes # toute les 30 minutes
await asyncio.sleep(30*60) await asyncio.sleep(30*60)

View File

@@ -0,0 +1,47 @@
import datetime
import logging
import json
import requests
from database import db
from database.helpers import ConfigurationHelper
from database.models import GameBundle
from discord import Client
def _isEnable():
return ConfigurationHelper().getValue('humble_bundle_enable') and ConfigurationHelper().getIntValue('humble_bundle_channel') != 0
def _callHexas():
response = requests.get("http://hexas.shionn.org/humble-bundle/json", headers={ "Content-Type": "application/json" })
if response.status_code == 200:
return response.json()
logging.error(f"Erreur de connexion {response.status_code}")
return None
def _isNotAlreadyNotified(bundle):
return GameBundle.query.filter_by(id=bundle['id']).first() == None
def _formatMessage(bundle):
choice = bundle['choices'][0]
date = datetime.datetime.fromtimestamp(bundle['endDate']/1000,datetime.UTC).strftime("%d %B %Y")
message = f"@here **Humble Bundle** propose un pack de jeu [{bundle['name']}]({bundle['url']}) contenant :\n"
for game in choice["games"]:
message += f"- {game}\n"
message += f"Pour {choice['price']}€, disponible jusqu'au {date}."
return message
async def checkHumbleBundleAndNotify(bot: Client):
if _isEnable() :
try :
bundle = _callHexas()
if _isNotAlreadyNotified(bundle) :
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.commit()
except Exception as e:
logging.error(f"Erreur de connexion {e}")
else:
logging.info('Humble bundle est désactivé')