| oracleData = {} | oracleData = {} | ||||
| with open('data/oracle.json') as file: | with open('data/oracle.json') as file: | ||||
| oracleData = json.load(file) | oracleData = json.load(file) | ||||
| print('Registered {} card names and {} oracle entries'.format(len(names), len(oracleData))) | |||||
| print('Registered {} card names and {} oracle entries'.format(len(namesToSearch), len(oracleData))) | |||||
| crData = {} | |||||
| with open('data/cr.json') as file: | |||||
| crData = json.load(file) | |||||
| crDataNames = crData['glossary'].keys() | |||||
| crDataNumbers = crData['sections'].keys() | |||||
| print('Registered {} CR glossary terms and {} CR sections'.format(len(crDataNames), len(crDataNumbers))) | |||||
| def format_card(card): | def format_card(card): | ||||
| mana = '' | mana = '' | ||||
| 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(bot, update, args): | ||||
| words = args | |||||
| text = ' '.join(words).casefold() | |||||
| text = ' '.join(args).casefold() | |||||
| reply = [] | reply = [] | ||||
| for name in namesToSearch: | for name in namesToSearch: | ||||
| if name.casefold() in text: | if name.casefold() in text: | ||||
| else: | else: | ||||
| question(bot, update, text) | question(bot, update, text) | ||||
| def comp_rules(bot, update, args): | |||||
| if not args: | |||||
| update.message.reply_text('I need some clues to search for, my master!', quote=False) | |||||
| return | |||||
| words = [word.casefold() for word in args] | |||||
| if words[0][0].isdigit(): | |||||
| lang = 'en' | |||||
| if len(words) > 1 and words[1] == 'ru': | |||||
| lang = 'ru' | |||||
| results = [] | |||||
| section = words[0].casefold() | |||||
| pos = len(section) | |||||
| for name in sorted([name for name in crDataNumbers if name.startswith(section)]): | |||||
| diff = name[pos:].strip('.') | |||||
| if len(diff) < 2 and (len(diff) == 0 or diff.isalpha()): | |||||
| results.append(name) | |||||
| text = '\n'.join(['<b>{}</b> {}'.format(name, crData['sections'][name][lang]) for name in results]) | |||||
| update.message.reply_text(text, parse_mode='HTML', quote = False) | |||||
| return | |||||
| nameCandidates = [name for name in crDataNames 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 name.casefold().startswith(term)] | |||||
| if bestCandidates: | |||||
| nameCandidates = bestCandidates | |||||
| excellentCandidate = [name for name in nameCandidates if name.casefold() == term] | |||||
| if excellentCandidate: | |||||
| nameCandidates = excellentCandidate | |||||
| if not nameCandidates: | |||||
| update.message.reply_text('I searched very thoroughly, but returned empty-handed, my master!', quote=False) | |||||
| return | |||||
| if len(nameCandidates) > 20: | |||||
| update.message.reply_text('I need more specific clues, my master! This would return {} names'.format(len(nameCandidates)), quote=False) | |||||
| return | |||||
| 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) | |||||
| def dispatcher_setup(dispatcher): | def dispatcher_setup(dispatcher): | ||||
| dispatcher.add_handler(CommandHandler('start', start)) | dispatcher.add_handler(CommandHandler('start', start)) | ||||
| dispatcher.add_handler(CommandHandler('help', start)) | dispatcher.add_handler(CommandHandler('help', start)) | ||||
| dispatcher.add_handler(CommandHandler('o', oracle, pass_args=True)) | dispatcher.add_handler(CommandHandler('o', oracle, pass_args=True)) | ||||
| dispatcher.add_handler(CommandHandler('q', question, 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(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)) |