forked from Bahnzo/scape_poe_uniques
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrape_poe_maps.py
280 lines (226 loc) · 9.17 KB
/
scrape_poe_maps.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
#! python3
# -*- coding: utf-8 -*-
"""
# scrape_poe_maps.py - scrapes poe maps from http://pathofexile.gamepedia.com/Map
# (modified from scrape_poe_uniques.py)
"""
import requests, bs4, re, datetime, time, json
from bs4 import NavigableString
from multiprocessing.dummy import Pool as ThreadPool
base_url = 'http://pathofexile.gamepedia.com'
main_url = base_url + '/Map'
rx_search = re.compile(r'\+*\(([\d\.]+)\s[a-z]+\s([\d\.]+)[)]|(\+*[\d\.]+\%)|([\d\.]+-[\d\.]+)|(\([\d\.]+-[\d\.]+)\s\w+\s([\d\.]+-[\d\.]+\))|(-?\+?[\d\.]+)')
vendor_regex = re.compile('yields? one|produces? one', re.IGNORECASE)
maptype_regex = re.compile('Map type', re.IGNORECASE)
def write_file_headers():
"""
info headers for MapList.txt
:return: list
"""
data = []
d = datetime.datetime.now()
now_time = d.strftime('%Y-%m-%d at %H:%M:%S')
data.append('; Data from ' + main_url)
data.append('; Comments can be made with ";", blank lines will be ignored.')
data.append(';')
data.append('; This file was auto-generated by scrape_poe_maps.py on {}'.format(now_time) + '\n')
data.append('mapList := Object()')
data.append('mapList["Unknown Map"] := "You have found a new map type, please report it!"\n')
data.append('uniqueMapList := Object()')
data.append('uniqueMapList["Unknown Map"] := "You have found a new unique map type, please report it!"\n')
return data
def get_main_page(url):
"""
Gets the main wiki page for Maps and parses out the links for each map
and returns a list of the urls
:param url: main page url
:return: list of urls
"""
map_urls = []
print('Getting main page...')
page = requests.get(url)
page.raise_for_status()
soup = bs4.BeautifulSoup(page.text, 'html.parser')
map_table = soup.find_all('table', class_='wikitable')
mapcount = 1
for row in map_table[0].find_all('tr'):
tds = row.find_all('td')
if len(tds) == 0: # exclude header row
continue
map_info = {}
map_info['count'] = mapcount
mapcount += 1
map_info['name'] = tds[0].find('a').text.strip()
map_info['level'] = tds[1].text.strip()
map_info['tier'] = tds[2].text.strip()
map_info['unique'] = tds[3].find('img')['alt'].strip()
map_info['zone'] = tds[6].text.strip()
map_info['url'] = base_url + tds[0].find('a')['href']
map_info['base'] = None
if map_info['unique'] == 'yes':
count = 0
for child in tds[0].find('span', class_='header').children:
if child.string is not None:
count += 1
if count == 2:
map_info['base'] = str(child).strip()
break
else:
map_info['base'] = map_info['name']
map_urls.append(map_info)
return map_urls
def get_category_url(data): # extracts links from the category
url_list = []
page_url = 'http://pathofexile.gamepedia.com'
for elem in data.find_all('a'):
url_list.append(page_url + elem.attrs['href'])
return url_list
def parse_map_data(map_info):
"""
fetches the page for a map
:param links:
:return:
"""
page = requests.get(map_info['url'])
page.raise_for_status()
soup = bs4.BeautifulSoup(page.text, 'html.parser')
return build_data(soup, map_info)
def filter_unicode_string(str):
return str.replace(u'\u2212', '-').replace(u'\u2013', '-').strip()
def append_modifier(results, mod, prepend):
modifier = filter_unicode_string(mod)
if len(modifier) > 0 and modifier.lower() != 'corrupted':
results.append(prepend + modifier)
def parse_stats(stats, prepend):
results = []
for stat in stats:
if isinstance(stat, NavigableString):
append_modifier(results, stat, prepend)
elif stat.name is not None and stat.name == 'span':
append_modifier(results, stat.text, prepend)
return results
def find_divcards(div):
for h2 in div.find_all('h2'):
for span in h2.find_all('span'):
if span.text == 'Divination Cards':
return h2
return None
def find_vendor_recipe(div):
for yields in div.find_all(text=vendor_regex):
return yields.next_sibling.find('a').text
return None
def find_setting(div):
for maptype in div.find_all(text=maptype_regex):
return maptype.parent.next_sibling.replace(':', '').strip()
return None
def build_data(data, mapinfo):
"""
parse map data from the page
:param data: BS4 ResultSet
:return: list
"""
map_data = dict(mapinfo)
print('Getting data for {}'.format(map_data['name'].encode('utf8')))
div = data.find('div', id='mw-content-text') # main div containing our data
# find the h2 element containing "Divination Cards" and list items following it
map_data['divcards'] = []
div_h2 = find_divcards(div)
if div_h2 is not None:
divlist = div_h2.find_next_siblings('ul')
if divlist is not None:
divcards = divlist[0].find_all('li')
for divcard in divcards:
map_data['divcards'].append(divcard.find('a').text)
# find the text string "yield|produce one" to determine the vendor recipe for the map
map_data['vendor'] = find_vendor_recipe(div)
# find the map setting (indoors/outdoors)
map_data['setting'] = find_setting(div)
#if map_data['unique'] == 'no' and map_data['tier'] != 'N/A' and int(map_data['tier']) != 15 and map_data['vendor'] is None:
# print('No vendor found for ' + map_data['name'])
#if map_data['setting'] is None:
# print('No setting found for ' + map_data['name'])
return map_data
def convert_data_to_AHK_readable_format(all_data):
"""
This function takes the raw web page data, and converts it into lines that are readable by the
Poe_item_info AHK script.
:return:
"""
# Read the descriptions of various maps
with open('MapDescriptions.json', 'r') as f:
map_descriptions = json.load(f)
new_data = []
vendor_map = {}
unique_map = {}
matchList = []
for mymap in all_data: # scan once to determine the lower tier map(s) that vendor up to each map
if mymap['vendor'] is not None and mymap['unique'] == 'no':
if not mymap['vendor'] in vendor_map:
vendor_map[mymap['vendor']] = []
vendor_map[mymap['vendor']].append(mymap['name'])
if mymap['unique'] == 'no' and mymap['tier'] != 'N/A':
matchList.append(mymap['name'])
if mymap['unique'] == 'yes' and mymap['base'] is not None:
unique_map[mymap['base']] = mymap['name']
new_data.append('matchList := ["' + '","'.join(matchList) + '"]\n')
for mymap in all_data:
if mymap['base'] is None:
print('ERROR: Could not determine base map type for map: ' + mymap['name'])
continue
structureName = 'mapList'
if mymap['unique'] == 'yes':
structureName = 'uniqueMapList'
new_data.append(';' + mymap['name'])
line = structureName + '["' + mymap['base'] + '"] := "`nThis map is based on the following area(s): '
line += mymap['zone']
if mymap['setting'] is not None:
line += '`n`nMap type: ' + mymap['setting']
# Here we insert the prewritten text descriptions for the maps that have them
desc_key = 'maps'
if mymap['unique'] == 'yes':
desc_key = 'uniqueMaps'
if mymap['base'] in map_descriptions[desc_key]:
line += '`n`n' + map_descriptions[desc_key][mymap['base']]
# Add on divination cards
if len(mymap['divcards']) > 0:
line += '`n`nDivination cards:'
for dc in mymap['divcards']:
line += '`n- ' + dc
# Add on vendor recipes
if mymap['unique'] == 'no':
vendor_lines = []
if mymap['base'] in vendor_map:
for vend in vendor_map[mymap['base']]:
vendor_lines.append('`n- Produced by three ' + vend + 's')
if mymap['vendor'] is not None:
vendor_lines.append('`n- Three ' + mymap['base'] + 's produce one ' + mymap['vendor'])
if len(vendor_lines) > 0:
line += '`n`nVendor:'
for vl in vendor_lines:
line += vl
# Add on unique version if one exists
if mymap['name'] in unique_map:
line += '`n`nUnique version of map: ' + unique_map[mymap['name']]
line += '"\n'
new_data.append(line)
return new_data
def write(new_data):
file = open('MapList.txt', 'ab') # opens file for writing
for row in new_data:
file.write(row.encode('utf8'))
file.write(b'\n')
file.close()
def main():
map_list = get_main_page(main_url)
open('MapList.txt', 'w').close() # create file (or overwrite it if it exists)
write(write_file_headers())
pool = ThreadPool(4)
data = pool.map(parse_map_data, map_list)
data.sort(key=lambda m: m['count'])
x = convert_data_to_AHK_readable_format(data)
write(x)
pool.close()
pool.join()
startTime = datetime.datetime.now()
main()
print('Program execution time: ',(datetime.datetime.now() - startTime))