forked from RZachLamberty/mtg_data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.py
executable file
·105 lines (72 loc) · 2.15 KB
/
cards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module: cards.py
Author: zlamberty
Created: 2016-02-28
Description:
cards class
Usage:
<usage>
"""
import logging
import os
import requests
import pandas as pd
import common
from mtgconstants import BASIC_LANDS
# ----------------------------- #
# Module Constants #
# ----------------------------- #
# data sources
CARD_URL = 'http://mtgjson.com/json/AllSets.json'
# local html caching
HTML_DIR = os.path.join(os.sep, 'tmp', 'local_html_cache')
# ----------------------------- #
# card class #
# ----------------------------- #
class JsonCardParseError(Exception):
pass
class MtgCard(dict):
"""Simple card object; as most of the cards are already coming to as us
JSON objects, this will be pretty trivial
"""
pass
def get_cards(url=CARD_URL):
"""Download all card data from mtgjson.com (see
http://mtgjson.com/documentation.html for more information about the various
fields available to us)
args:
url: base url of mtgjson api (default: scrape.CARD_URL)
returns:
iterable of MtgCard objects
raises:
None
"""
# download the JSON file
cards = requests.get(url).json()
# iterate through the json objects, parsing one at a time into card objects
for setname, setdict in cards.items():
for card in setdict['cards']:
c = MtgCard()
c.update(card)
c.update({'setname': setname})
yield c
def all_land_card_names(url=CARD_URL):
return {
_.get('name') for _ in get_cards(url) if 'Land' in _.get('types', [])
}
def all_card_names(url=CARD_URL, ignore_lands=True):
"""a set of all the card names. ignore basic lands by default"""
cns = {_.get('name') for _ in get_cards(url)}
if ignore_lands:
cns = cns.difference(all_land_card_names(url))
return cns
def cards_df(url=CARD_URL):
"""Same as get_cards, but returns a pandas dataframe
args:
url: base url of mtgjson api (default: scrape.CARD_URL)
returns:
pandas dataframe of mtg cards
"""
return pd.DataFrame(get_cards(url))