-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
141 lines (117 loc) · 5.43 KB
/
cli.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
import fire
import json
import urllib.request
from urllib.error import HTTPError
from rich import print as rprint
url = "https://api.scryfall.com/"
# Returns list of cards found using a search string
# Currently only returns first "page" of results (175 cards) sorted by name A -> Z
def cardSearch():
results = {}
searchQuery = input("Please input a search query: ")
searchURL = f"{url}cards/search?q={searchQuery}"
try:
jsonURL = urllib.request.urlopen(searchURL)
except HTTPError as err:
if err.code == 404:
rprint("[b]Card not found, please try again.[/b]\n")
cardSearch()
else:
data = json.loads(jsonURL.read())
cardsReturned = len(data['data'])
if cardsReturned == 1:
print(f"Your search \"{searchQuery}\" returned 1 card:")
setUpPrintout(data['data'][0]['uri'])
else:
print(f"Your search \"{searchQuery}\" returned {int(cardsReturned)} cards:\n")
for i in range(cardsReturned):
print(f"#{i+1}: {data['data'][i]['name']}")
results[i] = f"{data['data'][i]['uri']}"
selection = input("\nChoose a card number to see more info about that card: ")
selectedResultURI = results[int(selection) - 1]
setUpPrintout(selectedResultURI)
# Returns card printout if exact card is matched via the API, otherwise throws an error and restarts.
def exactCard():
rprint("[b]This function only returns a card when the name is exact (case-insensitive and optional punctuation).[/b]")
searchQuery = input("\nPlease input an exact card name: ")
searchURL = f"{url}cards/named?exact={searchQuery}".lower().replace(" ", "+")
try:
jsonURL = urllib.request.urlopen(searchURL)
except HTTPError as err:
if err.code == 404:
rprint("[b]Card not found, please try again.[/b]\n")
exactCard()
else:
data = json.loads(jsonURL.read())
setUpPrintout(data['uri'])
# Returns card printout if server is confident you named one card, otherwise throws an error and restarts
def fuzzyCard():
rprint("[b]This function only returns a card if the Scryfall server is confident that you unambiguously identified a unique name with your input.[/b]")
searchQuery = input("\nPlease input a search query: ")
searchURL = f"{url}cards/named?fuzzy={searchQuery}".lower().replace(" ", "+")
try:
jsonURL = urllib.request.urlopen(searchURL)
except HTTPError as err:
if err.code == 404:
rprint("[b]Either more than 1 one card matched your search, or zero cards matched; please try again.[/b]\n")
fuzzyCard()
else:
data = json.loads(jsonURL.read())
setUpPrintout(data['uri'])
# Returns a random card
def randomCard():
searchURL = f"{url}cards/random"
jsonURL = urllib.request.urlopen(searchURL)
data = json.loads(jsonURL.read())
setUpPrintout(data['uri'])
# Prints out card name, mana cost, type, text, and (if applicable) power and toughness
def setUpPrintout(cardURI):
jsonURL = urllib.request.urlopen(cardURI)
data = json.loads(jsonURL.read())
if bool(data.get('card_faces')):
faces = data.get('card_faces')
for face in faces:
typePrintout(face)
else:
typePrintout(data)
def typeDetermination(cardData):
typeLine = cardData.get('type_line').casefold()
if typeLine.find('creature') != -1:
return 1
elif typeLine.find('tribal') != -1:
return 2
elif typeLine.find('conspiracy') != -1:
return 3
elif typeLine.find('artifact') != -1:
return 4
elif typeLine.find('enchantment') != -1:
return 5
elif typeLine.find('hero') != -1:
return 6
elif typeLine.find('instant') != -1:
return 7
elif typeLine.find('land') != -1:
return 8
elif typeLine.find('planeswalker') != -1:
return 9
elif typeLine.find('sorcery') != -1:
return 10
else : rprint("There was an error with the application. Please open up an issue at [b]https://github.com/warpaltarpers/mtg-cli/issues[/b] and include a log of your console in the description.")
def typePrintout(card):
cardType = typeDetermination(card)
# Creature
if cardType == 1:
if bool(card.get('power')):
rprint(f"[b]{card['name']}[/b] | {card['mana_cost']} | {card['power']}/{card['toughness']}\n[u]{card['type_line']}[/u]\n{card['oracle_text']}\n".replace("{T}", "{Tap}"))
else: rprint(f"[b]{card['name']}[/b] | {card['mana_cost']}\n[u]{card['type_line']}[/u]\n{card['oracle_text']}\n".replace("{T}", "{Tap}"))
# Conspiracy, Hero, and Land
elif cardType == 3 or cardType == 6 or cardType == 8:
rprint(f"[b]{card['name']}[/b]\n[u]{card['type_line']}[/u]\n{card['oracle_text']}\n".replace("{T}", "{Tap}"))
# Artifact, Enchantment, Instant, Sorcery, and Tribal
elif cardType == 2 or cardType == 4 or cardType == 5 or cardType == 7 or cardType == 10:
rprint(f"[b]{card['name']}[/b] | {card['mana_cost']}\n[u]{card['type_line']}[/u]\n{card['oracle_text']}\n".replace("{T}", "{Tap}"))
# Planeswalker
elif cardType == 9:
rprint(f"[b]{card['name']}[/b] | {card['mana_cost']}| {card['loyalty']}\n[u]{card['type_line']}[/u]\n{card['oracle_text']}\n".replace("{T}", "{Tap}"))
if __name__ == '__main__':
fire.Fire()