ajout commande protondb

This commit is contained in:
Kepka Ludovic
2025-08-15 15:55:07 +02:00
committed by skylanix
parent b614e3238f
commit 488c49e42e
4 changed files with 76 additions and 39 deletions

View File

@@ -9,6 +9,8 @@ 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 from database.models import Configuration, GameBundle, Humeur
from protondb import searhProtonDb
class DiscordBot(discord.Client): class DiscordBot(discord.Client):
async def on_ready(self): async def on_ready(self):
@@ -53,10 +55,6 @@ class DiscordBot(discord.Client):
# toute les 30 minutes # toute les 30 minutes
await asyncio.sleep(30*60) await asyncio.sleep(30*60)
# TODO voir ce que je prend
def event(self, coro):
return super().event(coro)
def begin(self) : def begin(self) :
token = Configuration.query.filter_by(key='discord_token').first() token = Configuration.query.filter_by(key='discord_token').first()
if token : if token :
@@ -65,12 +63,20 @@ class DiscordBot(discord.Client):
logging.error('pas de token on ne lance pas discord') logging.error('pas de token on ne lance pas discord')
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True
bot = DiscordBot(intents=intents) bot = DiscordBot(intents=intents)
# TODO voir ce que je prend
@bot.event @bot.event
async def on_message(message): async def on_message(message):
if message.channel.id == 1234567: content: str = message.content;
@bot.command() if(ConfigurationHelper().getValue('proton_db_enable_enable') and content.find('!protondb')==0) :
async def name(ctx, arg): if (content.find('@')>0) :
await ctx.send(f"hello {arg}") user = content[content.find('@'):]
else :
user = f'@{message.author.name}'
name = message.content.replace('!protondb', '').replace(f'{user}', '').strip();
games = searhProtonDb(name)
msg = f'{user} J\'ai trouvé {len(games)} jeux :\n'
for game in games:
msg += f'- [{game.get('name')}](https://www.protondb.com/app/{game.get('id')}) classé **{game.get('tier')}**\n'
await message.channel.send(msg)

View File

@@ -1,36 +1,52 @@
import logging
import requests import requests
from algoliasearch.search.client import SearchClientSync, SearchConfig, RequestOptions
app_id = "94HE6YATEI" from algoliasearch.search.client import SearchClientSync, SearchConfig
api_key = "9ba0e69fb2974316cdaec8f5f257088f" from database.helpers import ConfigurationHelper
search_name = "call of duty" def _call_algoliasearch(search_name:str):
config = SearchConfig(ConfigurationHelper().getValue('proton_db_api_id'),
ConfigurationHelper().getValue('proton_db_api_key'))
config.set_default_hosts()
client = SearchClientSync(config=config)
return client.search_single_index(index_name="steamdb",
search_params={
"query":search_name,
"facetFilters":[["appType:Game"]],
"hitsPerPage":50},
request_options= {'headers':{'Referer':'https://www.protondb.com/'}})
def _call_summary(id):
response = requests.get(f'http://jazzy-starlight-aeea19.netlify.app/api/v1/reports/summaries/{id}.json')
if (response.status_code == 200) :
return response.json()
logging.error(f'{response.status_code} on {id}')
return None
def _is_name_match(name:str, search_name:str) -> bool:
return name.lower().find(search_name.lower()) >= 0
def searhProtonDb(search_name:str):
results = []
responses = _call_algoliasearch(search_name)
for hit in responses.model_dump().get('hits'):
id = hit.get('object_id')
name:str = hit.get('name')
if (_is_name_match(name, search_name)) :
try:
summmary = _call_summary(id)
if (summmary != None) :
tier = summmary.get('tier')
results.append({
'id':id,
'name' : name,
'tier' : tier
})
logging.info(f'found {name}({id} : {tier}')
except Exception as e:
logging.error(f'error on {name}({id}): {e}')
else:
logging.info(f'{name}({id} ne contient pas {search_name})')
return results
config = SearchConfig(app_id, api_key)
config.set_default_hosts()
client = SearchClientSync(app_id, api_key, config=config)
options = RequestOptions(config=config, headers={'referers':'https://www.protondb.com/'})
responses = client.search_single_index(index_name="steamdb",
search_params={
"query":"call of duty",
"facetFilters":[["appType:Game"]],
"hitsPerPage":50},
request_options= {'headers':{'Referer':'https://www.protondb.com/'}})
for hit in responses.model_dump().get('hits'):
id = hit.get('object_id')
name:str = hit.get('name')
if (name.lower().find(search_name.lower())>=0) :
try:
response = requests.get(f'http://jazzy-starlight-aeea19.netlify.app/api/v1/reports/summaries/{id}.json')
if (response.status_code == 200) :
summmary = response.json()
tier = summmary.get('tier')
print(f'{name} : {tier}')
else :
print(f'{response.status_code} on {name}({id})')
except Exception as e:
print(f'error on {name}({id}): {e}')
else:
print(f'{name}({id} ne contient pas {search_name})')

View File

@@ -20,5 +20,7 @@ def updateConfiguration():
# Je fait ca car html n'envoi pas le parametre de checkbox quand il est décoché # Je fait ca car html n'envoi pas le parametre de checkbox quand il est décoché
if (request.form.get("humble_bundle_channel") != None and request.form.get("humble_bundle_enable") == None) : if (request.form.get("humble_bundle_channel") != None and request.form.get("humble_bundle_enable") == None) :
ConfigurationHelper().createOrUpdate('humble_bundle_enable', False) ConfigurationHelper().createOrUpdate('humble_bundle_enable', False)
if (request.form.get("proton_db_api_id") != None and request.form.get("proton_db_enable_enable") == None) :
ConfigurationHelper().createOrUpdate('proton_db_enable_enable', False)
db.session.commit() db.session.commit()
return redirect(url_for('openConfigurations')) return redirect(url_for('openConfigurations'))

View File

@@ -20,6 +20,19 @@
<input type="Submit" value="Définir"> <input type="Submit" value="Définir">
</form> </form>
<h2>Proton DB</h2>
<form action="{{ url_for('updateConfiguration') }}" method="POST">
<label for="proton_db_enable_enable">Activer</label>
<input type="checkbox" name="proton_db_enable_enable" {% if configuration.getValue('proton_db_enable_enable') %}
checked="checked" {% endif %}>
<label>Activer la commande Proton DB</label>
<label for="proton_db_api_id">Api ID</label>
<input name="proton_db_api_id" type="text" value="{{ configuration.getValue('proton_db_api_id') }}" />
<label for="proton_db_api_key">Api KEY</label>
<input name="proton_db_api_key" type="text" value="{{ configuration.getValue('proton_db_api_key') }}" />
<input type="Submit" value="Définir">
</form>
<h2>Api</h2> <h2>Api</h2>
<form action="{{ url_for('updateConfiguration') }}" method="POST"> <form action="{{ url_for('updateConfiguration') }}" method="POST">
<label for="discord_token">Api Discord (cachée)</label> <label for="discord_token">Api Discord (cachée)</label>