-
Notifications
You must be signed in to change notification settings - Fork 4
/
CardDataHandler.py
267 lines (215 loc) · 9.53 KB
/
CardDataHandler.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
# -*- coding:utf-8 -*-
from functools import lru_cache
import requests
from urllib.parse import quote_plus
from Util import process_string, timing
from pyquery import PyQuery as pq
from DatabaseHandler import getClosestTCGCardname
import traceback
import re
import pprint
import difflib
WIKI_URL = 'https://yugipedia.com/wiki/'
TCG_BASE_URL = 'http://yugiohprices.com/api'
OCG_BASE_URL = 'https://yugipedia.com/api.php'
BREAK_TOKEN = '__BREAK__'
def sanitiseCardname(cardname):
return cardname.replace('/', '%2F')
@lru_cache(maxsize=128)
def getOCGCardURL(searchText):
try:
searchResults = requests.get(OCG_BASE_URL + '?action=query&list=search&srsearch=' + searchText + '&srlimit=50&format=json')
except:
traceback.print_exc()
return None
data = searchResults.json()['query']['search']
titles = [item['title'].lower() for item in data]
results = difflib.get_close_matches(searchText.lower(), titles, 1, 0.85)
if results:
for item in data:
if item['title'].lower() == results[0]:
return getWikiaURL(item['title'])
return None
@lru_cache(maxsize=128)
def getTCGCardImage(cardName):
endPoint = '/card_image/'
try:
response = requests.get(TCG_BASE_URL + endPoint + quote_plus(cardName))
except:
return None
else:
response.connection.close()
if response.ok:
return response.url
@lru_cache(maxsize=128)
def getTCGCardData(cardName):
endPoint = '/card_data/'
try:
response = requests.get(TCG_BASE_URL + endPoint + quote_plus(cardName))
except:
traceback.print_exc()
return None
else:
response.connection.close()
if response.ok:
json = response.json()
if json.get('status', '') == 'success':
json['data']['image'] = getTCGCardImage(cardName);
return json['data']
def getOCGCardData(url):
try:
html = requests.get(url)
ocg = pq(html.text)
card = ocg('.cardtable')
statuses = ocg('.cardtablestatuses')
data = {
'image': (card.find('td.cardtable-cardimage').eq(0)
.find('img').eq(0).attr('data-src')),
'name': (card.find('th.cardtable-header').eq(0).text()),
'type': ('trap' if card.find('a[title="Trap Card"]') else
('spell' if card.find('a[title="Spell Card"]') else
('monster' if card.find('th a[title="Type"]') else
'other')))
}
if (data['type'] == 'monster'):
data['monster_attribute'] = (card.find('th a[title="Attribute"]')
.eq(0).parents('tr').eq(0)
.find('td a').eq(0).text())
data['monster_types'] = [monster_type.strip() for monster_type in (process_string(
card.find('th a[title="Type"]').eq(0).parents('tr').eq(0)
.find('td').eq(0).text())).split('/')]
if 'Link' in data['monster_types']:
data['monster_level'] = ' / '.join(str(process_string(
card.find('th a[title="Link Arrow"]').eq(0).parents('tr').eq(0)
.find('td a').text())).split(' '))
elif 'Xyz' in data['monster_types']:
data['monster_level'] = int(process_string(
card.find('th a[title="Rank"]').eq(0).parents('tr').eq(0)
.find('td a').eq(0).text()))
else:
data['monster_level'] = int(process_string(
card.find('th a[title="Level"]').eq(0).parents('tr').eq(0)
.find('td a').eq(0).text()))
if 'Pendulum' in data['monster_types']:
data['pendulum_scale'] = int(process_string(
card.find('th a[title="Pendulum Scale"]').eq(0).parents('tr').eq(0)
.find('td a').eq(1).text()))
else:
data['pendulum_scale'] = None
atk_def = (card.find('th a[title="ATK"]').eq(0)
.parents('tr').eq(0).find('td').eq(0).text()).split('/')
data['monster_attack'] = process_string(atk_def[0])
data['monster_defense'] = process_string(atk_def[1])
elif (data['type'] == 'spell' or data['type'] == 'trap'):
data['spell_trap_property'] = (
card.find('th a[title="Property"]').eq(0).parents('tr').eq(0)
.find('td a').eq(0).text())
if (data['type'] == 'monster'):
for i, m_type in enumerate(data['monster_types']):
data['monster_types'][i] = data['monster_types'][i].strip()
description_element = card.find('.cardtablespanrow').html()
description_element = re.sub(r'</dt>', ': </dt>' + BREAK_TOKEN, description_element)
description_element = re.sub(r'</dd>', '</dd>' + BREAK_TOKEN, description_element)
description_element = re.sub(r'<br ?/?>', BREAK_TOKEN, description_element)
description_element = re.sub(r'<a href=[^>]+>', '', description_element)
description_element = re.sub(r'</a>', '', description_element)
description_element = pq(description_element).text()
data['description'] = process_string(description_element)
data['description'] = data['description'].replace(BREAK_TOKEN, '\n')
data['description'] = re.sub(r':(?=\w)', ': ', data['description'])
data['description'] = re.sub(r'\.(?=\w)', '. ', data['description'])
return data
except:
traceback.print_exc()
return None
def getPricesURL(cardName):
return "http://yugiohprices.com/card_price?name=" + cardName.replace(" ", "+")
def getWikiaURL(cardName):
return WIKI_URL + cardName.replace(" ", "_")
def formatTCGData(data):
try:
formatted = {}
formatted['name'] = data['name']
formatted['wikia'] = getWikiaURL(data['name'])
formatted['pricedata'] = getPricesURL(data['name'])
formatted['image'] = data['image']
formatted['text'] = re.sub('<!--(.*?)-->', '', data['text'].replace('\n\n', ' \n'))
formatted['cardtype'] = data['card_type']
if formatted['cardtype'].lower() == 'monster':
formatted['attribute'] = data['family'].upper()
formatted['types'] = data['type'].split('/')
formatted['level'] = data['level']
formatted['att'] = data['atk']
formatted['def'] = data['def']
if 'link' in ' '.join(str(i[1]).lower() for i in enumerate(formatted['types'])):
formatted['leveltype'] = None
formatted['level'] = None
formatted['def'] = None
elif 'xyz' in ' '.join(str(i[1]).lower() for i in enumerate(formatted['types'])):
formatted['leveltype'] = 'Rank'
else:
formatted['leveltype'] = 'Level'
else:
formatted['property'] = data['property']
return formatted
except:
traceback.print_exc()
return None
def formatOCGData(data):
try:
formatted = {}
formatted['name'] = data['name']
formatted['wikia'] = getWikiaURL(data['name'])
formatted['pricedata'] = None
formatted['image'] = data['image']
formatted['text'] = data['description'].replace('\n', ' \n')
formatted['cardtype'] = data['type']
if formatted['cardtype'].lower() == 'monster':
formatted['attribute'] = data['monster_attribute'].upper()
formatted['types'] = data['monster_types']
formatted['level'] = data['monster_level']
formatted['att'] = data['monster_attack']
formatted['defn_type'] = 'DEF'
formatted['def'] = data['monster_defense']
formatted['pendulum_scale'] = data['pendulum_scale']
if 'link' in ' '.join(str(i[1]).lower() for i in enumerate(formatted['types'])):
formatted['leveltype'] = 'Link Arrows'
formatted['defn_type'] = 'LINK'
elif 'xyz' in ' '.join(str(i[1]).lower() for i in enumerate(formatted['types'])):
formatted['leveltype'] = 'Rank'
else:
formatted['leveltype'] = 'Level'
else:
formatted['property'] = data['spell_trap_property']
return formatted
except:
traceback.print_exc()
return None
def getCardData(searchText):
try:
cardName = getClosestTCGCardname(searchText)
if (cardName): #TCG
print('Searching YGOPrices for: ' + searchText)
tcgData = getTCGCardData(sanitiseCardname(cardName))
formattedData = formatTCGData(tcgData)
if formattedData:
print("(TCG) Found: " + tcgData['name'])
else:
print ("Card not found.")
return formattedData
else: #OCG
print('Searching Yugipedia for: ' + searchText)
wikiURL = getOCGCardURL(searchText)
if not wikiURL:
wikiURL = getWikiaURL(searchText)
if (wikiURL):
ocgData = getOCGCardData(wikiURL)
formattedData = formatOCGData(ocgData)
if formattedData:
print("(OCG) Found: " + ocgData['name'])
return formattedData
else:
return None
except:
traceback.print_exc()
return None