Browse Source

Oracle extracted to the separate module

master
LeoTheHuman 8 years ago
parent
commit
2d6aef060f
2 changed files with 73 additions and 51 deletions
  1. 30
    51
      magic-judge-telegram-bot.py
  2. 43
    0
      oracle.py

+ 30
- 51
magic-judge-telegram-bot.py View File

import logging import logging
import json import json
import oracle
from telegram.ext import Updater, CommandHandler, InlineQueryHandler, CallbackQueryHandler, MessageHandler, Filters from telegram.ext import Updater, CommandHandler, InlineQueryHandler, CallbackQueryHandler, MessageHandler, Filters
from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup from telegram import InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardButton, InlineKeyboardMarkup


#logging.basicConfig(level=logging.DEBUG, #logging.basicConfig(level=logging.DEBUG,
# format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') # format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


names = {}
with open('data/names.json') as file:
names = json.load(file)
namesToSearch = names.keys()
oracleData = {}
with open('data/oracle.json') as file:
oracleData = json.load(file)
print('Registered {} card names and {} oracle entries'.format(len(namesToSearch), len(oracleData)))

crData = {} crData = {}
with open('data/cr.json') as file: with open('data/cr.json') as file:
crData = json.load(file) crData = json.load(file)
mana, mana,
card['type']) card['type'])


def start(bot, update):
def start_command(bot, update):
commands = [ commands = [
'/o <card name or search strings> - oracle text for a card', '/o <card name or search strings> - oracle text for a card',
'/q <question> - oracle text for cards mentioned in the question', '/q <question> - oracle text for cards mentioned in the question',
] ]
update.message.reply_text('How can I help?\n{}'.format('\n'.join(commands)), quote = False) update.message.reply_text('How can I help?\n{}'.format('\n'.join(commands)), quote = False)


def search_names(words):
nameCandidates = [name for name in namesToSearch if all(word in name.casefold() for word in words)]

term = ' '.join(words)

if len(words) > 1:
goodCandidates = [name for name in nameCandidates if term in name.casefold()]
if goodCandidates:
nameCandidates = goodCandidates

bestCandidates = [name for name in nameCandidates if term == name.casefold()]
if bestCandidates:
return bestCandidates

return nameCandidates


def oracle(bot, update, args):
def oracle_command(bot, update, args):
if not args: if not args:
update.message.reply_text('I need some clues to search for, my master!', quote=False) update.message.reply_text('I need some clues to search for, my master!', quote=False)
return return
words = [word.casefold() for word in args] words = [word.casefold() for word in args]


nameCandidates = search_names(words)
nameCandidates = oracle.get_matching_names(words)


if not nameCandidates: if not nameCandidates:
update.message.reply_text('I searched very thoroughly, but returned empty-handed, my master!', quote=False) update.message.reply_text('I searched very thoroughly, but returned empty-handed, my master!', quote=False)


reply = [] reply = []
for name in nameCandidates: for name in nameCandidates:
for uniqueName in names[name]:
reply.append(format_card(oracleData[uniqueName]))
for oracleName in oracle.get_oracle_names(name):
reply.append(format_card(oracle.get_card(oracleName)))
update.message.reply_text('\n'.join(reply), parse_mode='HTML', quote = False) update.message.reply_text('\n'.join(reply), parse_mode='HTML', quote = False)


def question(bot, update, args):
def question_command(bot, update, args):
text = ' '.join(args).casefold() text = ' '.join(args).casefold()

names = oracle.get_names_in_text(text)

reply = [] reply = []
for name in namesToSearch:
if name.casefold() in text:
reply.append('"' + name + '":\n' + '\n'.join([format_card(oracleData[uniqueName]) for uniqueName in names[name]]))
for name in names:
reply.append('"' + name + '":\n' + '\n'.join([format_card(oracle.get_card(oracleName)) for oracleName in oracle.get_oracle_names(name)]))
if reply: if reply:
update.message.reply_text('\n\n'.join(reply), parse_mode='HTML', quote = False) update.message.reply_text('\n\n'.join(reply), parse_mode='HTML', quote = False)


return return


words = query.split() words = query.split()
nameCandidates = search_names(words)
nameCandidates = oracle.get_matching_names(words)
if not nameCandidates: if not nameCandidates:
return return


results = list() results = list()
for word in nameCandidates[:3]: for word in nameCandidates[:3]:
for uniqueName in names[word]:
for oracleName in oracle.get_oracle_names(word):
card = oracle.get_card(oracleName)
results.append( results.append(
InlineQueryResultArticle( InlineQueryResultArticle(
id=oracleData[uniqueName]['name'],
id=card['name'],
title=word, title=word,
description=preview_card(oracleData[uniqueName]),
input_message_content=InputTextMessageContent(format_card(oracleData[uniqueName]), parse_mode='HTML')
description=preview_card(card),
input_message_content=InputTextMessageContent(format_card(card), parse_mode='HTML')
) )
) )
bot.answerInlineQuery(update.inline_query.id, results) bot.answerInlineQuery(update.inline_query.id, results)
chat_id = update.callback_query.message.chat.id chat_id = update.callback_query.message.chat.id
name = update.callback_query.data name = update.callback_query.data


if not name in names:
names = oracle.get_oracle_names(name)
if not names:
bot.answerCallbackQuery(update.callback_query.id) bot.answerCallbackQuery(update.callback_query.id)
return return


chat_id = chat_id, chat_id = chat_id,
message_id = message_id, message_id = message_id,
parse_mode = 'HTML', parse_mode = 'HTML',
text = '\n'.join([format_card(oracleData[uniqueName]) for uniqueName in names[name]])
text = '\n'.join([format_card(oracle.get_card(oracleName)) for oracleName in names])
) )
bot.answerCallbackQuery(update.callback_query.id) bot.answerCallbackQuery(update.callback_query.id)


return return
text = update.message.text text = update.message.text
if len(text) < 30: if len(text) < 30:
oracle(bot, update, text.split())
oracle_command(bot, update, text.split())
else: else:
question(bot, update, text) question(bot, update, text)


def comp_rules(bot, update, args):
def comp_rules_command(bot, update, args):
if not args: if not args:
update.message.reply_text('I need some clues to search for, my master!', quote=False) update.message.reply_text('I need some clues to search for, my master!', quote=False)
return return
text = '\n'.join(['<b>{}</b> {}'.format(name, crData['glossary'][name]) for name in sorted(nameCandidates)]) text = '\n'.join(['<b>{}</b> {}'.format(name, crData['glossary'][name]) for name in sorted(nameCandidates)])
update.message.reply_text(text, parse_mode='HTML', quote = False) update.message.reply_text(text, parse_mode='HTML', quote = False)


def ask(bot, update, args):
def ask_command(bot, update, args):
pass pass


def dispatcher_setup(dispatcher): def dispatcher_setup(dispatcher):
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('help', start))
dispatcher.add_handler(CommandHandler('o', oracle, pass_args=True))
dispatcher.add_handler(CommandHandler('q', question, pass_args=True))
dispatcher.add_handler(CommandHandler('cr', comp_rules, pass_args=True))
dispatcher.add_handler(CommandHandler('ask', ask, pass_args=True))
dispatcher.add_handler(CommandHandler('start', start_command))
dispatcher.add_handler(CommandHandler('help', start_command))
dispatcher.add_handler(CommandHandler('o', oracle_command, pass_args=True))
dispatcher.add_handler(CommandHandler('q', question_command, pass_args=True))
dispatcher.add_handler(CommandHandler('cr', comp_rules_command, pass_args=True))
dispatcher.add_handler(CommandHandler('ask', ask_command, pass_args=True))
dispatcher.add_handler(InlineQueryHandler(inline_oracle)) dispatcher.add_handler(InlineQueryHandler(inline_oracle))
dispatcher.add_handler(CallbackQueryHandler(callback_name)) dispatcher.add_handler(CallbackQueryHandler(callback_name))
dispatcher.add_handler(MessageHandler(Filters.text, text)) dispatcher.add_handler(MessageHandler(Filters.text, text))

+ 43
- 0
oracle.py View File

import json

with open('data/names.json') as file:
names = json.load(file)
namesToSearch = names.keys()
with open('data/oracle.json') as file:
oracleData = json.load(file)
print('Registered {} card names and {} oracle entries'.format(len(namesToSearch), len(oracleData)))

def get_matching_names(words):
nameCandidates = [name for name in namesToSearch if all(word in name.casefold() for word in words)]

term = ' '.join(words)

if len(words) > 1:
goodCandidates = [name for name in nameCandidates if term in name.casefold()]
if goodCandidates:
nameCandidates = goodCandidates

bestCandidates = [name for name in nameCandidates if term == name.casefold()]
if bestCandidates:
return bestCandidates

return nameCandidates

def get_card(name):
if name in oracleData:
return oracleData[name]
else:
return None

def get_oracle_names(name):
if name in names:
return names[name]
else:
return None

def get_names_in_text(text):
result = []
for name in namesToSearch:
if name.casefold() in text:
result.append(name)
return result

Loading…
Cancel
Save