LoginSignup
1
1

More than 1 year has passed since last update.

PythonでScryfallからMagic: the Gatheringのカードの情報をゲットした話

Last updated at Posted at 2021-08-13

PyhtonでScryfallの情報を取得するには、Scrythonを使ってください。

Scrython: https://github.com/NandaScott/Scrython

以下、おまけ

前回の記事では、Scrythonが使えない代わりに、requestsとbs4でScryfallの情報を取得しました。
前回の記事: https://qiita.com/Takeshi_Sue/items/753efb7d0c4680c31df1

その後、Scryfallからもっと直接的に情報を取得する方法があることを知りました。

以下のおまけでは、その「Scryfallからもっと直接的に情報を取得する方法」について書いていきます。

今回の目標は、以下の二つです
•Scryfallから特定のカードを検索する。
•その特定のカードの裁定を取得する。

Scryfallとは

Scryfallは、マジック:ザ・ギャザリングのカードを検索できるサイトです。
https://scryfall.com/

公式からも、Gathererというデータベースが提供されています。
https://gatherer.wizards.com/Pages/Default.aspx

一部カードの情報が、公式のGathererからは意図的に消去されているため、個人的にはScryfallの方が好みです。
例:Crusade
Scryfall: https://scryfall.com/card/ddf/27/crusade
Gatherer: https://gatherer.wizards.com/pages/card/Details.aspx?multiverseid=14503

Scryfallから特定のカードを検索する。

基本的に、「https://api.scryfall.com/cards/search?q=」の先に、 https://scryfall.com/docs/syntax に記載されている通りに検索したい文字列を入れれば、検索されます。

例1: 名前「asmor」で検索

import requests
import pprint

search_query = 'asmor'

cards = requests.get(f"https://api.scryfall.com/cards/search?q={search_query}")

cards_json = cards.json()

print(type(cards_json))
pprint.pprint(cards_json)

結果:
asmor.png

例2: オラクルに「draw」を含み、タイプがクリーチャーである。


import requests
import pprint

search_query = 'o:draw t:creature'

cards = requests.get(f"https://api.scryfall.com/cards/search?q={search_query}")

cards_json = cards.json()

print(type(cards_json))
pprint.pprint(cards_json.keys())
pprint.pprint(cards_json['total_cards'])

結果:
draw_creature.png

このようにすれば、特定のカードを探し当てることができます。

その特定のカードの裁定を取得する。

問題なのは、その特定のカードの裁定はURIとしてリンク先のみ提供されていることです。
例えば、以下のような形でそれがわかります。


import requests

search_query = 'asmor'

cards = requests.get(f"https://api.scryfall.com/cards/search?q={search_query}")

cards_json = cards.json()

print('type(cards_json) = ', type(cards_json), end = '\n\n')

print('cards_json.keys() = ', cards_json.keys(), end = '\n\n')

print("cards_json['data'][0].keys() = ", cards_json['data'][0].keys(), end = '\n\n')

print("cards_json['data'][0]['rulings_uri'] =", cards_json['data'][0]['rulings_uri'])

結果:
URI.png

そのため、そのURIから情報を取得します。
以下のような形で、実際の裁定(comment)は、かなり奥深くにあることがわかります。


import requests

search_query = 'asmor'

rulings = requests.get("https://api.scryfall.com/cards/d99a9a7d-d9ca-4c11-80ab-e39d5943a315/rulings")

rulings_json = rulings.json()

print('type(rulings_json) = ', type(rulings_json), end = '\n\n')

print('rulings_json.keys() = ', rulings_json.keys(), end = '\n\n')

print('type(rulings_json["data"]) = ', type(rulings_json["data"]), end = '\n\n')

print('len(rulings_json["data"]) = ', len(rulings_json["data"]), end = '\n\n')

print('rulings_json["data"][0].keys() = ', rulings_json["data"][0].keys(), end = '\n\n')

for key in rulings_json["data"][0]:
  print(f'rulings_json["data"][0][{key}] = {rulings_json["data"][0][key]}', end = '\n\n')

for i in range(len(rulings_json["data"])):
  print(f'rulings_json["data"][{i}]["comment"] = {rulings_json["data"][i]["comment"]}', end = '\n\n')

結果:
comments.png

感想

素直にScrythonを使おう!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1