Closed poppu-mtg closed 7 years ago
It has been pointed out to me that there is another website that generates a database for magic cards: https://mtgjson.com/sets.html which uses JSON language. So far, no luck in opening it via python though...
Ok, so as far as mtgjson goes. I have code that can handle it over in PennyDreadfulMtg/Penny-Dreadful-Tools, but that feels like overkill for what we're doing. What I'd recommend doing is using magicthegathering.io (an API that allows you to query individual cards from it)
I'd recommend something like
import json, requests
blob = json.loads(requests.get('https://api.magicthegathering.io/v1/cards?name={cardname}'.format(cardname=cardname)))
cost = blob[0].manaCost
(Note that this is an eyeballed pseudocode - It might not compile or work as intended)
Also of note: They do have a python library (https://github.com/MagicTheGathering/mtg-sdk-python/), which would be easier than using requests and json parsing. But it's only compatible with python3, so we're stuck with doing it the hard way unless we port StackIt to 3
The lines you wrote are a great start, I'll work on that for a new CMC search method.
As for the scan search and the different code names, I think a dictionary would suffice, from official set name to magiccards.info set name.
Using python 2.7, I cannot get the request on https to work apparently:
File "newparser-default3.py", line 13, in
blob = json.loads(requests.get('https://api.magicthegathering.io/v1/cards?name={cardname}'.format(cardname=cardname))) File "/Library/Python/2.7/site-packages/requests/api.py", line 70, in get return request('get', url, params=params, kwargs) File "/Library/Python/2.7/site-packages/requests/api.py", line 56, in request return session.request(method=method, url=url, kwargs) File "/Library/Python/2.7/site-packages/requests/sessions.py", line 488, in request resp = self.send(prep, send_kwargs) File "/Library/Python/2.7/site-packages/requests/sessions.py", line 609, in send r = adapter.send(request, kwargs) File "/Library/Python/2.7/site-packages/requests/adapters.py", line 497, in send raise SSLError(e, request=request) requests.exceptions.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)
So, I got the SSL issues working for me, but now I'm having problems with Unicode characters in the ForeignNames key.
Ok, so:
mpskld
. I manually hardcoded a fix for it.Note that switching to mtgjson itself, rather than magicthegathering.io would be better for responsiveness and caching, but that's an issue for another day, and would require additional effort, and a longer initial wait time, as it downloads the entire 6.29MB blob.