-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
171 lines (152 loc) · 5.68 KB
/
scraper.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
import urllib2
import re
import json
bad_feats = ['Armor Proficiency, Light', 'Armor Proficiency, Medium']
class Feat:
def __init__(self,
reference_name='',
display_name='',
feat_prerequisites=[],
non_feat_prerequisites=[],
builds_into=[],
benefits_short=''):
self.reference_name = reference_name
self.display_name = display_name
self.feat_prerequisites = feat_prerequisites
self.non_feat_prerequisites = non_feat_prerequisites
self.builds_into = builds_into
self.benefits_short = benefits_short
def get_page(url):
req = urllib2.Request(url)
response = urllib2.urlopen(req)
page = response.read()
start = page.find('</thead>')
page = page[start:]
print 'Response Status Code for ' + str(url) + ': ' + str(response.getcode())
return page
def get_next_feat(page):
display_name_start = page.find('>', page.find('<td'))+1
display_name_end = page.find('</t', display_name_start)
display_name = page[display_name_start:display_name_end]
# print display_name_start, display_name_end, display_name
display_name = re.sub(r'<a.*?>|</a>|\*','',display_name)
display_name = display_name.lstrip(' ')
# display_name_start > 200 or display_name_end - display_name_start > 100 or
if (display_name_start > 200 or display_name_end == -1 or len(display_name) > 50):
return None
reference_name = ''
reference_name = display_name.replace(",", '').split()
if len(reference_name) == 1:
reference_name = ''.join(reference_name).lower()
else:
first_word = reference_name[0].lower()
other_words = ''.join(reference_name[1:])
reference_name = first_word + other_words
reference_name = reference_name.replace("'", '')
page = page[display_name_end:]
feat_prerequisites = []
non_feat_prerequisites = []
prerequisites_data_start = page.find('>', page.find('<td'))+1
prerequisites_data_end = page.find('</t', prerequisites_data_start)
prerequisites_data_all = page[prerequisites_data_start:prerequisites_data_end]
if prerequisites_data_all == '—':
# no prerequisites
pass
else:
non_feat_prerequisites.append(prerequisites_data_all)
page = page[prerequisites_data_end:]
benefits_short_start = page.find('>',page.find('<td')+3)+1
benefits_short_end = page.find('</td>',benefits_short_start)
benefits_short = page[benefits_short_start:benefits_short_end]
benefits_short = re.sub(r'<a.*?>|</a>|<i>|</i>','',benefits_short)
page = page[benefits_short_end:]
return reference_name, display_name, feat_prerequisites, non_feat_prerequisites, benefits_short, page
def get_some_feats(page, max_iterations=1000):
feats = []
i = 0
while i < max_iterations:
next_feat = get_next_feat(page)
if next_feat == None:
break
reference_name, display_name, feat_prerequisites, non_feat_prerequisites, benefits_short, page = next_feat
new_feat = Feat(reference_name=reference_name,
display_name=display_name,
feat_prerequisites=feat_prerequisites,
non_feat_prerequisites=non_feat_prerequisites,
benefits_short=benefits_short)
feats.append(new_feat)
i += 1
return feats
def get_all_feats(url_list):
feats = []
feat_names = []
for url in url_list:
page = get_page(url)
feats_list = get_some_feats(page)
for feat in feats_list:
feats.append(feat)
for feat in feats:
feat_names.append(feat.display_name)
for feat_index, feat in enumerate(feats):
prerequisites = []
feat_prerequisites = []
non_feat_prerequisites = []
if feat.non_feat_prerequisites:
#removing <a> tags
all_prerequisites = re.sub(r'<a.*?>|</a>|\*','',feat.non_feat_prerequisites[0])
for bad_feat_index, bad_feat in enumerate(bad_feats):
#check if all_prerequisites contains a "bad feat"
if all_prerequisites.find(bad_feats[bad_feat_index]) == -1:
pass
else:
#adds proper "bad feat" to list and removes from all_prereq string
prerequisites.append(bad_feats[bad_feat_index])
all_prerequisites = all_prerequisites.replace(bad_feats[bad_feat_index], '')
if all_prerequisites:
all_prerequisites_list = all_prerequisites.split(', ')
for pre in all_prerequisites_list:
if pre:
prerequisites.append(pre)
for pre in prerequisites:
if pre in feat_names:
feat_prerequisites.append(pre)
else:
non_feat_prerequisites.append(pre)
feats[feat_index].feat_prerequisites = feat_prerequisites
feats[feat_index].non_feat_prerequisites = non_feat_prerequisites
for i, feat in enumerate(feats):
builds_into = []
feat_display_name = feat.display_name
for feat in feats:
if feat_display_name in feat.feat_prerequisites:
builds_into.append(feat.reference_name)
feats[i].builds_into = builds_into
return feats
def print_feats(feats):
for feat in feats:
print feat.reference_name, feat.display_name, feat.feat_prerequisites, feat.non_feat_prerequisites, feat.builds_into, feat.benefits_short
def write_feats(feats):
file = open("js/feats.js", 'w')
file.write("var feats = {\n")
for i, feat in enumerate(feats):
feat_data = dict(
name=feat.display_name,
featPrerequisites=feat.feat_prerequisites,
nonFeatPrerequisites=feat.non_feat_prerequisites,
buildsInto=feat.builds_into,
benefitsShort=feat.benefits_short)
formatted_feat_data = json.dumps(feat_data, sort_keys=True, indent=4, separators=(',', ': '))
if i > 0:
file.write(',\n"' + feat.reference_name + '" : ')
else:
file.write('"' + feat.reference_name + '" : ')
file.write(formatted_feat_data)
file.write("}")
file.close()
core_feats_url = "http://paizo.com/pathfinderRPG/prd/coreRulebook/feats"
apg_feats_url = "http://paizo.com/pathfinderRPG/prd/advancedPlayersGuide/advancedFeats"
acg_feats_url = "http://paizo.com/pathfinderRPG/prd/advancedClassGuide/feats"
urls = [core_feats_url, apg_feats_url, acg_feats_url]
feats = get_all_feats(urls)
# print_feats(feats)
write_feats(feats)