This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
card.py
216 lines (176 loc) · 6.51 KB
/
card.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'''
card.py - Willie Magic: the Gathering Card Lookup Module
A module for the IRC Bot Willie that allows for one to get the Oracle text
for any Magic: the Gathering card by card name.
Author: John Cleaver
Site: https://github.com/kihashi/mtg-irc
License: BSD 3 Clause.
'''
from card_database import models
import sys
import argparse
import pprint
def find_card(input_card):
'''
Returns the card object that matches the input name.
'''
input_card = sanitize(input_card)
try:
return find_card_by_name(input_card)
except CardNotFoundError:
try:
return find_card_by_search_name(input_card)
except CardNotFoundError:
try:
return find_card_by_nickname(input_card)
except CardNotFoundError:
return find_cards_like(input_card)
def sanitize(input):
'''
Makes sure that the input string is clean.
'''
return input.strip()
def find_card_by_name(input_card):
db_card = models.MagicCard.get_by(name=input_card)
if not db_card:
raise CardNotFoundError(input_card)
else:
return db_card
def find_card_by_search_name(input_card):
db_card = models.MagicCard.get_by(search_name=
input_card.lower()
.replace(u"'", u"")
.replace(u",", u""))
if not db_card:
raise CardNotFoundError(input_card)
else:
return db_card
def find_card_by_nickname(input_card):
db_nick = models.CardNick.get_by(nickname=input_card.title())
if not db_nick:
raise CardNotFoundError(input_card)
else:
return db_nick.card
def find_cards_like(input_card):
db_card = models.MagicCard.query.filter(models.MagicCard.search_name.contains(input_card.lower().replace("'", "").replace(",", ""))).first()
if not db_card:
raise CardNotFoundError(input_card)
else:
return db_card
def find_expansion(exp_abbrev):
q = models.Expansion.get_by(abbreviation=exp_abbrev.upper())
if not q:
q = models.Expansion.get_by(gatherer_code=exp_abbrev.upper())
if not q:
q = models.Expansion.get_by(old_code=exp_abbrev.upper())
if not q:
q = models.Expansion.get_by(mtgo_code=exp_abbrev.upper())
if not q:
raise ExpansionNotFoundError(exp_abbrev.upper())
return q
def find_release_by_name(card_name, expansion_abbreviation):
card = find_card(card_name)
expansion = find_expansion(expansion_abbreviation)
card_release = _find_release(card, expansion)
return card_release
def _find_release(card, expansion):
card_release = models.CardRelease.get_by(card=card,
expansion=expansion)
if card_release is not None:
return card_release
else:
raise ReleaseNotFoundError(card, expansion)
class CardError(Exception):
def __init__(self):
pass
class CardNotFoundError(CardError):
def __init__(self, card_name):
self.card_name = card_name
def __unicode__(self):
return self.card_name
def __str__(self):
return self.__unicode__()
class ExpansionNotFoundError(CardError):
def __init__(self, exp_abbrev):
self.expansion = exp_abbrev
def __unicode__(self):
return self.expansion
def __str__(self):
return self.__unicode__()
class ReleaseNotFoundError(CardError):
def __init__(self, card, expansion):
self.card = card
self.expansion = expansion
def __unicode__(self):
return self.card.name + u" is not in " + self.expansion
def __str__(self):
return self.__unicode__()
def main(argv):
if not argv.card:
print(u"You must specify a card.")
sys.exit()
else:
try:
if argv.rulings:
card_obj = find_card(u" ".join(argv.card))
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(card_obj.get_rulings())
elif argv.flavor:
card_obj = find_card(u" ".join(argv.card))
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(card_obj.get_flavor_text())
elif argv.eprice:
card_obj = find_card(u" ".join(argv.card))
print(card_obj.get_mtgoprice())
elif argv.legality:
card_obj = find_card(u" ".join(argv.card))
print(card_obj.get_legality())
elif argv.printed:
card_obj = find_card(u" ".join(argv.card))
print(card_obj.get_printed_text())
elif argv.set:
models.setup()
set_obj = find_expansion(u" ".join(argv.card))
models.close()
print(set_obj)
else:
card_obj = find_card(u" ".join(argv.card))
print(card_obj.get_card_text())
type(card_obj.name)
except CardNotFoundError as e:
print(u"Could not find the card: " + unicode(e))
if __name__ == "__main__":
models.setup()
parser = argparse.ArgumentParser()
parser.add_argument("card", nargs="+", help="The Card to find.")
parser.add_argument("-r",
"--rulings",
action="store_true",
help="Get the rulings for the specified card.")
parser.add_argument("-e",
"--eprice",
action="store_true",
help="Get the MTGO price for the specified card.")
parser.add_argument("-t",
"--text",
action="store_true",
help="Get the text for a specific card.")
parser.add_argument("-f",
"--flavor",
action="store_true",
help="Get the flavor texts for a specific card")
parser.add_argument("-s",
"--set",
action="store_true",
help="Get the set name for a specific code.")
parser.add_argument("-p",
"--printed",
action="store_true",
help="Get the text originally printed on the card.")
parser.add_argument("-l",
"--legality",
action="store_true",
help="Get the format legality for the specified card.")
args = parser.parse_args()
main(args)
models.close()