This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
forked from underbent/scrape_poe_info
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrape_poe_maps.py
284 lines (213 loc) · 7.86 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
281
282
283
284
#! python3
"""
# scrape_poe_maps.py - scrapes poe map data via the API and all the individual map articles, see http://pathofexile.gamepedia.com/Map for example.
"""
import requests, bs4, re, datetime, time, json, os
from bs4 import NavigableString
from multiprocessing.dummy import Pool as ThreadPool
SCRIPTDIR = os.path.dirname(os.path.abspath(__file__))
base_url = 'http://pathofexile.gamepedia.com'
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)
league_suffix_regex = re.compile(r' \([a-z]+\)$', 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 http://pathofexile.gamepedia.com')
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"] := "Map not recognised or not supported"\n')
data.append('uniqueMapList := Object()')
data.append('uniqueMapList["Unknown Map"] := "Map not recognised or not supported"\n')
return data
def clean_up_api_results(api_results):
"""
Takes the API result and turns it into a list of json objects.
"""
map_list = []
for result in api_results:
itemdata = result['title']
map_info = {}
map_info['name'] = league_suffix_regex.sub('', itemdata['name'])
map_info['url'] = base_url + '/' + map_info['name'].replace(' ', '_')
if ' Map (' in itemdata['name']:
map_info['unique'] = False # Unique maps don't have the "Map" before the league suffix in the query data
else:
map_info['unique'] = True
map_list.append(map_info)
return map_list
def get_api_results():
"""
This function gets the map names, it uses the wiki's API and requests json format.
See this HTML version to get a better idea how the API response is structured:
https://pathofexile.gamepedia.com/api.php?action=cargoquery&format=json&limit=500&tables=atlas_maps&fields=_pageName=name&formatversion=1
"""
print('Getting data for maps')
r = requests.get('https://pathofexile.gamepedia.com/api.php?action=cargoquery&format=json&limit=500&tables=atlas_maps&fields=_pageName=name&formatversion=1')
rj = r.json()
api_results = rj['cargoquery']
return clean_up_api_results(api_results)
def get_wiki_data():
map_list = []
#for category in item_categories:
# map_list.extend(get_api_results(category))
map_list.extend(get_api_results())
print('')
return map_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 find_divcards(div):
for h2 in div.find_all('h2'):
if h2.find('span', id='Divination_cards'):
return h2
return None
def find_vendor_recipe(div):
try:
for yields in div.find_all(text=vendor_regex):
return yields.next_sibling.findNext('a').findNext('a').text
except:
return None
return None
def find_setting(div):
try:
for maptype in div.find_all(text=maptype_regex):
return maptype.parent.next_sibling.replace(':', '').strip()
except:
return None
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']))
map_data['divcards'] = []
heading = data.find(id='Items_found_in_this_area')
if heading is not None:
table = heading.find_next('table')
items = table.find_all('span', class_='divicard-header')
for item in items:
map_data['divcards'].append(item.text)
# find the map setting (indoors/outdoors)
#map_data['setting'] = find_setting(div)
map_data['producedby'] = []
heading = data.find(id='Upgrade_paths')
if heading is not None:
table = heading.find_next('table')
items = table.find_all('span', class_='header -single')
for item in items:
if item.text not in map_data['producedby']:
map_data['producedby'].append(item.text)
map_data['upgradesto'] = []
heading = data.find(id='Usage_in_upgrade_paths')
if heading is not None:
table = heading.find_next('table')
items = table.find_all('span', class_='header -single')
for item in items:
if item.text != map_data['name']:
if item.text not in map_data['upgradesto']:
map_data['upgradesto'].append(item.text)
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(SCRIPTDIR + '\\MapDescriptions.json', 'r') as f:
map_descriptions = json.load(f)
uniqueMapNameFromBase = open(SCRIPTDIR + '\\MapNameFromBase.txt', 'r').read()
new_data = []
unique_map = {}
matchList = []
for mymap in all_data:
if mymap['unique'] is False:
matchList.append(mymap['name'])
# lists sorted by descending name length to avoid mismatching ("Spider Lair Map" before "Lair Map" etc.)
matchList.sort(key=len, reverse=True)
new_data.append('mapMatchList := ["' + '","'.join(matchList) + '"]\n')
new_data.append('\n' + uniqueMapNameFromBase + '\n')
for mymap in all_data:
line = ''
#line = 'Tier: ' + mymap['tier'] + ', Level: ' + mymap['level']
#line += '`nTileset: ' + mymap['tileset']
#if mymap['setting'] is not None:
# line += ' (' + mymap['setting'] + ')'
# Add on vendor recipes and connected maps
if mymap['unique'] is False:
vendor_lines = '3 to 1 vendor recipe:'
if mymap['producedby']:
vendor_lines += '`n Produced by: ' + ', '.join(mymap['producedby'])
else:
vendor_lines += '`n Produced by: none'
if mymap['upgradesto']:
vendor_lines += '`n Upgrades to: ' + ', '.join(mymap['upgradesto'])
else:
vendor_lines += '`n Upgrades to: none'
line += vendor_lines
# Add line when map is shaped
#if mymap['shaped'] == 'yes':
# line += '`n`nInfos from ' + mymap['base'] + ':'
# Add on unique version if one exists
#if mymap['base'] in unique_map and mymap['unique'] == 'no':
# line += '`n`nUnique version of map: ' + unique_map[mymap['base']]
# Add on divination cards
if len(mymap['divcards']) > 0:
line += '`n`nDivination cards:'
for divcard in mymap['divcards']:
line += '`n ' + divcard
# Here we insert the prewritten text descriptions for the maps that have them
if mymap['unique']:
if mymap['name'] in map_descriptions['uniqueMaps']:
line += '`n`n' + map_descriptions['uniqueMaps'][mymap['name']]
else:
if mymap['name'] in map_descriptions['maps']:
line += '`n`n' + map_descriptions['maps'][mymap['name']]
line = line.lstrip('`n')
if mymap['unique']:
entry = 'uniqueMapList["' + mymap['name'] + '"] := "' + line + '"\n'
else:
entry = 'mapList["' + mymap['name'] + '"] := "' + line + '"\n'
new_data.append(entry)
return new_data
def write(new_data):
file = open(SCRIPTDIR + '\\MapList.txt', 'a+b') # opens file for writing
for row in new_data:
file.write(row.encode('cp1252'))
file.write(b'\n')
file.close()
def main():
map_list = get_wiki_data()
open(SCRIPTDIR + '\\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['name'])
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))