Добавлена команда /getGamesPlayer: поиск PGN-партий по FIDE ID через Lichess broadcast API
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 5s
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 5s
This commit is contained in:
parent
7f627beeec
commit
74dd696783
2 changed files with 290 additions and 1 deletions
|
|
@ -8,7 +8,7 @@ import re
|
|||
import sys
|
||||
import asyncio
|
||||
from telegram import Update
|
||||
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
||||
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler
|
||||
from telegram.constants import ParseMode
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
|
@ -386,6 +386,80 @@ def _chunk_output(text: str, size: int) -> list:
|
|||
return chunks
|
||||
|
||||
|
||||
ASK_FIDE_ID = 1
|
||||
|
||||
|
||||
async def get_games_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
user = update.effective_user
|
||||
record_user(user.id, user.username or '', user.first_name or '',
|
||||
user.last_name or '')
|
||||
await update.message.reply_text(
|
||||
'🔍 Пришли FIDE ID или ссылку на профиль ratings.fide.com\n\n'
|
||||
'Пример: `46616543`\n'
|
||||
'Или: `https://ratings.fide.com/profile/46616543`',
|
||||
parse_mode=ParseMode.MARKDOWN_V2)
|
||||
return ASK_FIDE_ID
|
||||
|
||||
|
||||
async def get_games_receive_id(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
user = update.effective_user
|
||||
text = update.message.text.strip()
|
||||
|
||||
from swiss_calc.pgn_fetcher import extract_fide_id, get_player_games, count_games
|
||||
|
||||
fide_id = extract_fide_id(text)
|
||||
if not fide_id:
|
||||
await update.message.reply_text(
|
||||
'❌ Не нашёл FIDE ID в сообщении\\. Пришли 5\\-8 значный номер или ссылку на ratings\\.fide\\.com',
|
||||
parse_mode=ParseMode.MARKDOWN_V2)
|
||||
return ASK_FIDE_ID
|
||||
|
||||
msg = await update.message.reply_text(
|
||||
f'⏳ Ищу партии для FIDE ID {fide_id}\\.\\.\\.',
|
||||
parse_mode=ParseMode.MARKDOWN_V2)
|
||||
|
||||
try:
|
||||
games = get_player_games(fide_id)
|
||||
except Exception as e:
|
||||
await msg.edit_text(f'❌ Ошибка: {str(e)[:200]}')
|
||||
return ConversationHandler.END
|
||||
|
||||
sent = 0
|
||||
for tc in ('standard', 'rapid', 'blitz'):
|
||||
pgn = games.get(tc, '')
|
||||
n = count_games(pgn)
|
||||
if n == 0:
|
||||
continue
|
||||
filename = f'fide_{fide_id}_{tc}.pgn'
|
||||
try:
|
||||
await update.message.reply_document(
|
||||
document=bytes(pgn, 'utf-8'),
|
||||
filename=filename,
|
||||
caption=f'📋 {tc.upper()} — {n} партий'
|
||||
)
|
||||
sent += 1
|
||||
except Exception as e:
|
||||
await update.message.reply_text(
|
||||
f'⚠️ Не удалось отправить {tc}: {str(e)[:100]}')
|
||||
|
||||
if sent == 0:
|
||||
await update.message.reply_text(
|
||||
'🔍 Партий не найдено в Lichess\\-трансляциях за последнее время\\.',
|
||||
parse_mode=ParseMode.MARKDOWN_V2)
|
||||
|
||||
try:
|
||||
await msg.delete()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return ConversationHandler.END
|
||||
|
||||
|
||||
async def cancel_get_games(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
await update.message.reply_text('Отменено\\.', parse_mode=ParseMode.MARKDOWN_V2)
|
||||
return ConversationHandler.END
|
||||
|
||||
|
||||
def main():
|
||||
if not TOKEN:
|
||||
print('CLIENT_BOT_TOKEN not set', file=sys.stderr)
|
||||
|
|
@ -394,6 +468,13 @@ def main():
|
|||
app = Application.builder().token(TOKEN).build()
|
||||
app.add_handler(CommandHandler('start', start))
|
||||
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_url))
|
||||
app.add_handler(ConversationHandler(
|
||||
entry_points=[CommandHandler('getGamesPlayer', get_games_start)],
|
||||
states={
|
||||
ASK_FIDE_ID: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_games_receive_id)],
|
||||
},
|
||||
fallbacks=[CommandHandler('cancel', cancel_get_games)],
|
||||
))
|
||||
|
||||
print('Client bot started', file=sys.stderr)
|
||||
app.run_polling()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue