-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic4.py
300 lines (245 loc) · 7.66 KB
/
magic4.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import csv
import requests
from bs4 import BeautifulSoup as Soup
import sys
import os
'''
TODO
separa deck do side
usar diretorios feito
fazer uma saida geral e uma saida para cada
any(key.startswith("Brazen") for key in col)
next( (key for key in col if key.startswith("Brazen")), None )
'''
col={}
deck={}
deck2={}
want=dict()
have=dict()
needs=[]
custos=[]
arqCol='hungria.csv'
arqDeck='deck.txt'
arqdeck='deck2.txt'
lands=["Island",'Swamp','Plains','Mountain','Forest','Planície','Montanha','Ilha','Floresta','Pântano']
#soup=''
def main():
'''
destino=open("decklist2.txt",'w')
sys.stdout=destino
'''
ler_dados_colecao(arqCol)
for txt in os.listdir():
if txt.endswith(".txt"):
nome=txt[:-4]
print(nome)
ler_dados_deck_arena(txt)
calcular_cartas()
limpar()
'''
print("Novo")
ler_dados_deck_arena('simic food.txt')
calcular_cartas()
limpar()
'''
'''
for i in range(1,5):
print("Burn",i)
ler_dados_deck_arena('burn'+str(i)+'.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
for i in range(1,31):
print("Novo",i)
ler_dados_deck_arena('novo'+str(i)+'.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
print('deck1')
ler_dados_deck('deck.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
print('deck2')
ler_dados_deck_arena('deck2.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
for i in range(1,7):
print("Simic",i)
if i==5:
ler_dados_deck('simic'+str(i)+'.txt')
else:
ler_dados_deck_arena('simic'+str(i)+'.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
for i in range(1,9):
print("Dimir",i)
ler_dados_deck_arena('dimir'+str(i)+'.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
for i in range(1,8):
print("Amass",i)
ler_dados_deck_arena('amass'+str(i)+'.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
for i in range(1,8):
print("Orzhov",i)
ler_dados_deck_arena('orzhov'+str(i)+'.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
for i in range(1,4):
print("Gates",i)
ler_dados_deck_arena('gates'+str(i)+'.txt')
calcular_cartas()
limpar()
print('------------------------------------------------\n')
'''
print("\nForam analizados %3.3d decks\no minimo de cartas para montar um deles é %3.3d cartas"%(len(needs),min(needs)))
print("O mais barato custa %6.2f reais para ser completado"%min(custos))
print('------------------------------------------------\n')
#sys.stdout = sys.__stdout__
#destino.close()
print('acabou')
def limpar():
deck.clear()
deck2.clear()
want.clear()
have.clear()
def emArquivo():
pass
def procura(nome):
ler_dados_deck_arena(nome+'.txt')
calcular_cartas()
def ler_dados_colecao(arq):
with open(arq) as csvfile:
colecao=csv.reader(csvfile,quotechar="'")
itercol=iter(colecao)
next(itercol)
for carta in itercol:
cartaEN=''.join(carta[5].split("'"))
if carta[4] not in col or cartaEN not in col:
col[carta[4]]=int(carta[6])
col[cartaEN]=int(carta[6])
else:
col[carta[4]]+=int(carta[6])
col[cartaEN]+=int(carta[6])
def ler_dados_deck(arq1):
with open(arq1) as arq:
col1=arq.readlines()
aux=1
len1=len(col1)
for carta in col1:
if aux==len1:
if carta[2:] not in deck:
deck[carta[2:]]=int(carta[0])
else:
deck[carta[2:]]+=int(carta[0])
else:
if carta[0]=='\n':
aux+=1
continue
if carta[2:-1] not in deck:
deck[carta[2:-1]]=int(carta[0])
else:
deck[carta[2:-1]]+=int(carta[0])
aux+=1
def ler_dados_deck_arena(arq1):
with open(arq1) as arq:
col1=arq.readlines()
for carta in col1:
if carta[0]=='\n':
continue
carta2=" ".join(carta.split(' ')[1:]).split(" (")[0]
#print(carta2)
# print(" ".join(carta.split(' ')[1:]).split("(")[0])
if carta2 not in deck:
deck[carta2]=int(carta[0])
else:
deck[carta2]+=int(carta[0])
def calcular_cartas():
for carta in deck :
if carta in lands:
continue
cartaEN=''.join(carta.split("'"))
if cartaEN in col:
deck2[carta]=deck[carta]-col[cartaEN]
else:
deck2[carta]=deck[carta]
for carta in deck2:
cartaEN=''.join(carta.split("'"))
try:
have[carta]=min(deck[carta],col[cartaEN])
except:
pass
finally:
if deck2[carta]>0:
want[carta]=deck2[carta]
need=0
custo=0
for card in want:
need+=want[card]
custo+=(procurarPreco(card)*want[card])
#print(custo)
needs.append(need)
custos.append(custo)
print("\nPara esse deck faltam %3.3d cartas com o custo de %6.2f\n"%(needs[-1],custos[-1]))
imprimirTudo(need)
def imprimirTudo(need):
#print("\nprecisa de %3.3d"%need)
print("\nO deck:\n")
imprimir(deck)
print("\n\nPreciso:\n")
imprimir(want)
print("\n\nTenho:\n")
imprimir(have)
print()
def imprimir(dic):
for el in dic:
print(el,":",dic[el])
def procurarPreco(carta):
#global soup
ps=[]
pagina="https://www.ligamagic.com.br/?view=cards/card&card="
link="".join((pagina,"%20".join(carta.split(" "))))
print(link)
r=myRequest(link,"erro na carta "+carta)
soup=Soup(r.text,"html.parser")
r.close()
for pr in soup.find_all("font",{'class':'bigger'}):
ps.append(float(pr.text.replace(".","").replace(",",".")))
#print(ps)
if len(ps)==0:
for pr in soup.find_all("td",{'class':'col-4 preMen'}):
ps.append(float(pr.text.split('\xa0')[0][3:].replace(".","").replace(",",".")))
#print(ps)
try:
#print("aqui")
preco=min(ps)
except:
a=sys.stdout
sys.stdout = sys.__stdout__
print(carta)
sys.stdout = a
#preco=float(soup.find("div",{'id':'mobile-precos-menor'}).text[3:].strip(".").replace(",","."))
print(preco)
return preco
def myRequest(url,erro):
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',}
while True:
try:
r=requests.get(url,headers=headers)
except Exception:
a=sys.stdout
sys.stdout = sys.__stdout__
print("\a\n\n\n\n",erro,"\n\n\n\n\a")
sys.stdout = a
else:
break
return r
main()