ソースを参照

switch oracle to mtgjson

master
コミット
a4193665bb
3個のファイルの変更86行の追加14行の削除
  1. 4
    2
      make_data.sh
  2. 12
    12
      src/magic-judge-telegram-bot.py
  3. 70
    0
      src/oracle_sql.py

+ 4
- 2
make_data.sh ファイルの表示

@@ -1,5 +1,7 @@
#!/bin/sh
mkdir -p data
python3 scripts/update_cards.py
curl https://media.wizards.com/2019/downloads/MagicCompRules%2020190503.txt --output cr.txt
#python3 scripts/update_cards.py
curl https://media.wizards.com/2020/downloads/MagicCompRules%2020200703.txt --output cr.txt
curl https://mtgjson.com/api/v5/AllPrintings.sqlite --output data/mtg.sqlite
python3 scripts/update_cr.py


+ 12
- 12
src/magic-judge-telegram-bot.py ファイルの表示

@@ -1,6 +1,6 @@
import logging
import json
import oracle
import oracle_sql
import documents
import gc
from telegram.ext import (Updater, CommandHandler, InlineQueryHandler,
@@ -67,7 +67,7 @@ def oracle_command(bot, update, args):
return
words = [word.casefold() for word in args]

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

if not nameCandidates:
update.message.reply_text(
@@ -95,22 +95,22 @@ def oracle_command(bot, update, args):

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


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

names = oracle.get_names_in_text(text)
names = oracle_sql.get_names_in_text(text)

reply = []
for name in names:
reply.append(
'"' + name + '":\n' + '\n'.join(
[format_card(oracle.get_card(oracleName))
for oracleName in oracle.get_oracle_names(name)]))
[format_card(oracle_sql.get_card(oracleName))
for oracleName in oracle_sql.get_oracle_names(name)]))
if reply:
update.message.reply_text(
'\n\n'.join(reply),
@@ -126,14 +126,14 @@ def inline_oracle(bot, update):
return

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

results = list()
for word in nameCandidates[:3]:
for oracleName in oracle.get_oracle_names(word):
card = oracle.get_card(oracleName)
for oracleName in oracle_sql.get_oracle_names(word):
card = oracle_sql.get_card(oracleName)
results.append(
InlineQueryResultArticle(
id=card['name'],
@@ -150,14 +150,14 @@ def callback_name(bot, update):
chat_id = update.callback_query.message.chat.id
name = update.callback_query.data

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

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


+ 70
- 0
src/oracle_sql.py ファイルの表示

@@ -0,0 +1,70 @@
import sqlite3
import codecs

def quote_identifier(s, errors="strict"):
encodable = s.encode("utf-8", errors).decode("utf-8")

nul_index = encodable.find("\x00")

if nul_index >= 0:
error = UnicodeEncodeError("NUL-terminated utf-8", encodable,
nul_index, nul_index + 1, "NUL not allowed")
error_handler = codecs.lookup_error(errors)
replacement, _ = error_handler(error)
encodable = encodable.replace("\x00", replacement)

return encodable.replace("\"", "\"\"")

conn = sqlite3.connect('data/mtg.sqlite', check_same_thread=False)

def get_card(name):
t = ( name,)
cursor = conn.cursor()
cursor.execute("SELECT DISTINCT(name), type, text, manacost, loyalty, power, toughness FROM cards WHERE name LIKE ?", t)
row = cursor.fetchone()
if not row:
return None

result = {
'name': row[0],
'type': row[1]
}

if row[2]:
result['text'] = row[2]

if row[3]:
result['manaCost'] = row[3]

if row[4]:
result['loyalty'] = row[4]

if row[5] and row[6]:
result['power'] = row[5]
result['toughness'] = row[6]

return result


def get_matching_names(words):
condition = ''

for word in words:
if condition:
condition = condition + ' AND '

condition = condition + "name LIKE '%" + quote_identifier(word) + "%'"

query = 'SELECT DISTINCT(name) FROM cards WHERE ' + condition
cursor = conn.cursor()
cursor.execute(query)
return [row[0] for row in cursor.fetchall()]


def get_oracle_names(name):
card = get_card(name)
if not card:
return None

return [card['name']]


読み込み中…
キャンセル
保存