-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtg_cli.py
218 lines (181 loc) · 6.24 KB
/
mtg_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
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
217
218
from InquirerPy import inquirer
from InquirerPy.base.control import Choice
from InquirerPy.separator import Separator
import pyfiglet
import requests
from clint.textui import puts, indent, colored
from time import sleep
from random import random
from clint.textui import progress
import zmq
def main():
"""Main driver function"""
hello_text = pyfiglet.figlet_format("Magic CLI", font="colossal", width=110)
bye_text = pyfiglet.figlet_format("Goodbye", font="colossal", width=110)
print(hello_text)
print("** Welcome to the Magic the Gathering CLI Tool by CMoore")
print("** Use this tool to find information about Magic the Gathering cards")
print("** Use your arrow keys to get started")
while True:
print("\n")
action = actions()
if action == "Search card by name":
search_by_name()
if action == "Explore Magic the Gathering":
explore_MTG()
if action == "Generate random card name":
# magic_rng_card()
with indent(4, quote=(" |")):
puts(colored.green(magic_rng_card()))
if action == None:
break
print(bye_text)
quit()
def mill():
"""mill progress bar"""
with indent(2, quote=(" |")):
puts(colored.blue("Fetching..."))
for i in progress.mill(range(10)):
sleep(random() * 0.2)
def actions():
"""Default actions in main menu"""
action = inquirer.select(
message="Select an action:",
choices=[
"Search card by name",
"Explore Magic the Gathering",
"Generate random card name",
Choice(value=None, name="Exit"),
],
default="Search by card name",
).execute()
return action
# PARTNER'S MICROSERVICE
def magic_rng_card():
context = zmq.Context()
# Socket to talk to server
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
socket.send_string("Generating random card name...")
# Get the reply.
message = socket.recv()
return message.decode("utf-8")
def search_by_name():
"""Search card by name with Scryfall API fuzzy name search"""
c_name = inquirer.text(
message="Enter card name:",
).execute()
mill()
response = requests.get("https://api.scryfall.com/cards/named?fuzzy=" + c_name)
result = response.json()
if result["object"] == "error":
with indent(4, quote=(" |")):
puts(colored.green("Card not found, please try another."))
else:
rarity = result["rarity"]
# print card info from request
with indent(2, quote=(" |")):
puts(colored.green(result["name"]))
with indent(4, quote=(" |")):
puts(colored.green(result["type_line"]))
puts(colored.green(result["oracle_text"]))
puts(colored.green(result["mana_cost"]))
puts(colored.green(result["set_name"]))
puts(colored.green(rarity.capitalize()))
puts(colored.green(result["released_at"]))
price = inquirer.confirm(message="Fetch card price?", default=True).execute()
if price:
prices = result["prices"]
# Make sure price is listed and valid
check_type = isinstance(prices["usd"], str)
if check_type == True:
with indent(4, quote=(" |")):
puts(colored.green("$" + prices["usd"]))
else:
with indent(4, quote=(" |")):
puts(colored.green("Price not listed"))
def explore_MTG():
action = inquirer.select(
message="Choose category",
choices=[
"Land types",
"Creature types",
"Mana symbols",
"Other symbols",
"Unfinity symbols",
],
default="Land types",
).execute()
if action == "Land types":
land()
if action == "Creature types":
creatures()
if action == "Mana symbols":
symbols(action)
if action == "Other symbols":
symbols(action)
if action == "Unfinity symbols":
symbols(action)
def land():
"""Search land types"""
mill()
response = requests.get("https://api.scryfall.com/catalog/land-types")
result = response.json()
for i in result["data"]:
with indent(4, quote=(" |")):
puts(colored.green(i))
return
def creatures():
"""Search creature types"""
mill()
response = requests.get("https://api.scryfall.com/catalog/creature-types")
result = response.json()
items = result["total_values"]
start = 0
end = 13
while end <= items:
for i in range(start, end):
if i != items:
with indent(4, quote=(" |")):
puts(colored.green(result["data"][i]))
else:
break
if end + 13 == items:
break
else:
next_page = inquirer.confirm(message="Load more?", default=True).execute()
if next_page == True:
start += 13
end += 13
else:
break
return
def symbols(action):
"""Search MTG symbols"""
mill()
response = requests.get("https://api.scryfall.com/symbology")
result = response.json()
if action == "Mana symbols":
for i in result["data"]:
if i["represents_mana"] == True and i["funny"] == False:
with indent(4, quote=(" |")):
symbol = i["symbol"]
english = i["english"]
puts(colored.green("%s - %s" % (symbol, english)))
if action == "Other symbols":
for i in result["data"]:
if i["represents_mana"] == False and i["funny"] == False:
with indent(4, quote=(" |")):
symbol = i["symbol"]
english = i["english"]
puts(colored.green("%s - %s" % (symbol, english)))
if action == "Unfinity symbols":
for i in result["data"]:
if i["funny"] == True:
with indent(4, quote=(" |")):
symbol = i["symbol"]
english = i["english"]
puts(colored.green("%s - %s" % (symbol, english)))
return
if __name__ == "__main__":
main()