-
Notifications
You must be signed in to change notification settings - Fork 0
/
SEOValidator.py
507 lines (373 loc) · 21.9 KB
/
SEOValidator.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import urllib
from bs4 import BeautifulSoup
import urllib.request
from urllib.request import Request, urlopen, urlcleanup
from urllib.error import URLError
import json
class SEOValidator:
def __init__(self):
self.file = 'options.json'
self.options = {}
self.loadOptions()
self.loop = 0
def loadOptions(self):
try:
with open(self.file, 'r') as f:
s = f.read()
self.options = json.loads(s)
except Exception:
print(self.file+' not found')
return
def seoAnalysis(self, charClass, charSpec):
# Retrieves all guides that it should analyze
guideTypes = self.options['guidesTypes']
guides = guideTypes.split(',')
result = ''
issues = []
# For each guide, call its own analysis method
for guide in guides:
analysis = getattr(self, 'seoGuideAnalysis_' + guide.replace('-','_') )
res, iss = analysis(charClass, charSpec)
result += res + '\t'
issues += iss
return result, issues
def seoGuideAnalysis_guide(self, charClass, charSpec):
expansion = self.options['expansion']
patch = self.options['patch']
issues = []
# Fetch the guide from Wowhead
title, content = self.dataFetch(charClass, charSpec, 'Guide')
# Verifies if it was found
if title is None:
return 'x', ['{0} {1} Overview Guide wasn\'t found.'.format(charClass, charSpec)]
content = content.lower()
# Checks if the Guide title is well formatted
expectedTitle = '{0} {1} Guide – {2} {3}'.format(charSpec, charClass, expansion, patch)
if title != expectedTitle and title != expectedTitle2:
issues.append('{0} {1} Overview Title has the wrong format. "<{2}>" instead of "<{3}>" '.format(charClass, charSpec, title, expectedTitle))
# Checks the usage of aliases
issues += self.aliasesEvaluation(charClass, charSpec, 'Overview Guide', content)
# Checks if the body is using expressions as often as it should
guideFormat = '{0} {1} guide'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Overview Guide', 3 )
# Returns the amount of issues for the summary and the list of them for the detail
return str(len(issues)), issues
def seoGuideAnalysis_talent_guide(self, charClass, charSpec):
expansion = self.options['expansion']
patch = self.options['patch']
issues = []
# Fetch the guide from Wowhead
title, content = self.dataFetch(charClass, charSpec, 'Talent Guide')
# Verifies if it was found
if title is None:
return 'x', ['{0} {1} Talent Guide wasn\'t found.'.format(charClass, charSpec)]
content = content.lower()
# Checks if the Guide title is well formatted
expectedTitle = '{0} {1} Talents & Build Guide – {2} {3}'.format(charSpec, charClass, expansion, patch)
expectedTitle2 = '{0} {1} Talents & Build Guide -{2} {3}'.format(charSpec, charClass, expansion, patch)
if title != expectedTitle and title != expectedTitle2:
issues.append('{0} {1} Talent Title has the wrong format. "<{2}>" instead of "<{3}>" '.format(charClass, charSpec, title, expectedTitle))
# Checks the usage of aliases
issues += self.aliasesEvaluation(charClass, charSpec, 'Talent Guide', content)
# Checks if the body is using expressions as often as it should
guideFormat = '{0} {1} talent'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Talent Guide', 2 )
guideFormat = '{0} {1} build'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Talent Guide', 2 )
# Returns the amount of issues for the summary and the list of them for the detail
return str(len(issues)), issues
def seoGuideAnalysis_rotation_guide(self, charClass, charSpec):
expansion = self.options['expansion']
patch = self.options['patch']
issues = []
# Fetch the guide from Wowhead
title, content = self.dataFetch(charClass, charSpec, 'Rotation Guide')
# Verifies if it was found
if title is None:
return 'x', ['{0} {1} Rotation Guide wasn\'t found.'.format(charClass, charSpec)]
content = content.lower()
# Checks if the Guide title is well formatted
expectedTitle = '{0} {1} Rotation Guide – {2} {3}'.format(charSpec, charClass, expansion, patch)
expectedTitle2 = '{0} {1} Rotation Guide - {2} {3}'.format(charSpec, charClass, expansion, patch)
if title != expectedTitle and title != expectedTitle2:
issues.append('{0} {1} Rotation Title has the wrong format. "<{2}>" instead of "<{3}>" '.format(charClass, charSpec, title, expectedTitle))
# Checks the usage of aliases
issues += self.aliasesEvaluation(charClass, charSpec, 'Rotation Guide', content)
# Checks if the body is using expressions as often as it should
guideFormat = '{0} {1} rotation'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Rotation Guide', 4 )
# Returns the amount of issues for the summary and the list of them for the detail
return str(len(issues)), issues
def seoGuideAnalysis_artifact_guide(self, charClass, charSpec):
expansion = self.options['expansion']
patch = self.options['patch']
issues = []
# Fetch the guide from Wowhead
title, content = self.dataFetch(charClass, charSpec, 'Artifact Guide')
# Verifies if it was found
if title is None:
return 'x', ['{0} {1} Artifact Guide wasn\'t found.'.format(charClass, charSpec)]
content = content.lower()
artifact = self.options['artifacts']['-'.join(charSpec.lower().split(' ')) + '-' + '-'.join(charClass.lower().split(' '))]
# Checks if the Guide title is well formatted
expectedTitle = '{0} {1} Artifact Weapon: {4} – {2} {3}'.format(charSpec, charClass, expansion, patch, artifact)
expectedTitle2 = '{0} {1} Artifact Weapon: {4} - {2} {3}'.format(charSpec, charClass, expansion, patch, artifact)
if title != expectedTitle and title != expectedTitle2:
issues.append('{0} {1} Artifact Title has the wrong format. "<{2}>" instead of "<{3}>" '.format(charClass, charSpec, title, expectedTitle))
# Checks the usage of aliases
issues += self.aliasesEvaluation(charClass, charSpec, 'Artifact Guide', content)
# Checks if the body is using expressions as often as it should
guideFormat = '{0} {1} artifact'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Artifact Guide', 3 )
# Returns the amount of issues for the summary and the list of them for the detail
return str(len(issues)), issues
def seoGuideAnalysis_gear_guide(self, charClass, charSpec):
expansion = self.options['expansion']
patch = self.options['patch']
issues = []
# Fetch the guide from Wowhead
title, content = self.dataFetch(charClass, charSpec, 'Gear Guide')
# Verifies if it was found
if title is None:
return 'x', ['{0} {1} Gear Guide wasn\'t found.'.format(charClass, charSpec)]
content = content.lower()
# Checks if the Guide title is well formatted
expectedTitle = '{0} {1} Gear, Tier Sets & BiS – {2} {3}'.format(charSpec, charClass, expansion, patch)
expectedTitle2 = '{0} {1} Gear, Tier Sets & BiS - {2} {3}'.format(charSpec, charClass, expansion, patch)
if title != expectedTitle and title != expectedTitle2:
issues.append('{0} {1} Gear Title has the wrong format. "<{2}>" instead of "<{3}>" '.format(charClass, charSpec, title, expectedTitle))
# Checks the usage of aliases
issues += self.aliasesEvaluation(charClass, charSpec, 'Gear Guide', content)
# Checks if the body is using expressions as often as it should
guideFormat = 'bis'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Gear Guide', 3 )
guideFormat = 'tier set'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Gear Guide', 3 )
guideFormat = 'armor'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Gear Guide', 3 )
guideFormat = 'gear'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Gear Guide', 3 )
guideFormat = '{0} {1} gear'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Gear Guide', 1 )
guideFormat = '{0} {1} tier set'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Gear Guide', 1 )
# Returns the amount of issues for the summary and the list of them for the detail
return str(len(issues)), issues
def seoGuideAnalysis_stat_priority_guide(self, charClass, charSpec):
expansion = self.options['expansion']
patch = self.options['patch']
issues = []
# Fetch the guide from Wowhead
title, content = self.dataFetch(charClass, charSpec, 'Stat Priority Guide')
# Verifies if it was found
if title is None:
return 'x', ['{0} {1} Stat Guide wasn\'t found.'.format(charClass, charSpec)]
content = content.lower()
# Checks if the Guide title is well formatted
expectedTitle = '{0} {1} Stat Priority – {2} {3}'.format(charSpec, charClass, expansion, patch)
expectedTitle2 = '{0} {1} Stat Priority - {2} {3}'.format(charSpec, charClass, expansion, patch)
if title != expectedTitle and title != expectedTitle2:
issues.append('{0} {1} Stat Title has the wrong format. "<{2}>" instead of "<{3}>" '.format(charClass, charSpec, title, expectedTitle))
# Checks the usage of aliases
issues += self.aliasesEvaluation(charClass, charSpec, 'Stat Guide', content)
# Checks if the body is using expressions as often as it should
guideFormat = '{0} {1} stat priority'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Stat Guide', 3 )
# Returns the amount of issues for the summary and the list of them for the detail
return str(len(issues)), issues
def seoGuideAnalysis_enhancements_guide(self, charClass, charSpec):
expansion = self.options['expansion']
patch = self.options['patch']
issues = []
# Fetch the guide from Wowhead
title, content = self.dataFetch(charClass, charSpec, 'Enhancements Guide')
content = content.lower()
# Verifies if it was found
if title is None:
return 'x', ['{0} {1} Enhancements Guide wasn\'t found.'.format(charClass, charSpec)]
# Checks if the Guide title is well formatted
expectedTitle = '{0} {1} Enchants, Gems & Enhancements – {2} {3}'.format(charSpec, charClass, expansion, patch)
expectedTitle2 = '{0} {1} Enchants, Gems & Enhancements - {2} {3}'.format(charSpec, charClass, expansion, patch)
if title != expectedTitle and title != expectedTitle2:
issues.append('{0} {1} Enhancements Title has the wrong format. "<{2}>" instead of "<{3}>" '.format(charClass, charSpec, title, expectedTitle))
# Checks the usage of aliases
issues += self.aliasesEvaluation(charClass, charSpec, 'Enhancements Guide', content)
# Checks if the body is using expressions as often as it should
guideFormat = '{0} {1} enchants'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Enhancements Guide', 2 )
guideFormat = '{0} {1} gems'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Enhancements Guide', 2 )
# Returns the amount of issues for the summary and the list of them for the detail
return str(len(issues)), issues
def seoGuideAnalysis_macro_guide(self, charClass, charSpec):
expansion = self.options['expansion']
patch = self.options['patch']
issues = []
# Fetch the guide from Wowhead
title, content = self.dataFetch(charClass, charSpec, 'Macro Guide')
# Verifies if it was found
if title is None:
return 'x', ['{0} {1} Macro Guide wasn\'t found.'.format(charClass, charSpec)]
content = content.lower()
# Checks if the Guide title is well formatted
expectedTitle = '{0} {1} Macros & Addons – {2} {3}'.format(charSpec, charClass, expansion, patch)
expectedTitle2 = '{0} {1} Macros & Addons - {2} {3}'.format(charSpec, charClass, expansion, patch)
if title != expectedTitle and title != expectedTitle2:
issues.append('{0} {1} Macro Title has the wrong format. "<{2}>" instead of "<{3}>" '.format(charClass, charSpec, title, expectedTitle))
# Checks the usage of aliases
issues += self.aliasesEvaluation(charClass, charSpec, 'Macro Guide', content)
# Checks if the body is using expressions as often as it should
guideFormat = '{0} {1} macro'
issues += self.expressionEvaluation(charClass, charSpec, content, guideFormat, 'Macro Guide', 3 )
# Returns the amount of issues for the summary and the list of them for the detail
return str(len(issues)), issues
def expressionEvaluation(self, charClass, charSpec, content, guideFormat, guide, expectedCount):
classAliases = []
specAliases = []
issues = []
# Adds all aliases to the list, if there are any
if charSpec in self.options['validations']:
specAliases += self.options['validations'][charSpec].keys()
# Otherwise, adds only the spec to it
else:
specAliases.append(charSpec)
# Adds all aliases to the list, if there are any
if charClass in self.options['validations']:
classAliases += self.options['validations'][charClass].keys()
# Otherwise, adds only the spec to it
else:
classAliases.append(charClass)
# Build all terms based on aliases and count how many times they show up
terms = []
for classAlias in classAliases:
for specAlias in specAliases:
terms.append( guideFormat.format( specAlias, classAlias ).lower() )
occurenceCount = 0
for term in terms:
occurenceCount += content.lower().count(term)
# If the body doesn't contain enough occurences, add issue
if occurenceCount < expectedCount:
issues.append('{0} {1} {2} needs more expressions <{3}>. Found {4} instead of {5} '.format(charClass, charSpec, guide, terms[0], occurenceCount, expectedCount))
return issues
def aliasesEvaluation(self, charClass, charSpec, guide, content):
issues = []
# Checks if either spec or class has an alias
if charClass in self.options['validations']:
issues += self.termFrequencyEvaluation(charClass, content, '{0} {1} {2}'.format(charSpec, charClass, guide))
if charSpec in self.options['validations']:
issues += self.termFrequencyEvaluation(charSpec, content, '{0} {1} {2}'.format(charSpec, charClass, guide))
return issues
def termFrequencyEvaluation(self, term, content, context):
issues = []
validations = self.options['validations'][term]
totalWeight = sum(validations.values())
aliases = validations.keys()
content = content.lower()
# Count the occurence of each term
termsCount = []
for alias in aliases:
lowAlias = alias.lower()
termsCount.append([alias, content.count('{0} '.format(lowAlias))])
# Total sum of occurences
termsSum = sum([x[1] for x in termsCount])
for key, value in validations.items():
# Checks if key shows up at least twice
if content.count(' {0} '.format(key.lower())) < 2:
issues.append('{0} doesn\'t show up 2 times on {1}'.format(key, context) )
# If the ratio of a given term is 50% or higher checks its frequency
if ( 1.0 * value ) / totalWeight >= 0.5:
dic = dict(termsCount)
ratio = ( 1.0 * dic[key] ) / termsSum
# Gives a the ratio a lee way
precision = self.options['precision']
# If ratio found is too far from the ratio expected
if ratio < ( 1 - precision ) * ( ( 1.0 * value ) / totalWeight ):
issues.append('{0} doesn\'t show as often as it should on {1} - Only {2} appearances in {3} aliases'.format(key, context, dic[key],termsSum) )
return issues
def dataFetch(self, charClass, charSpec, guide):
print('>Fetching {1} {0} {2}'.format(charClass, charSpec, guide))
charClass = '-'.join(charClass.lower().split(' '))
charSpec = '-'.join(charSpec.lower().split(' '))
guide = '-'.join(guide.lower().split(' '))
url = 'https://www.wowhead.com/{1}-{0}-{2}'.format(charClass, charSpec, guide)
req = Request(url)
try:
urlcleanup()
response = urlopen(req)
except URLError as e:
if hasattr(e, 'reason'):
print(' We failed to reach a server.')
print(' Reason: ', e.reason)
elif hasattr(e, 'code'):
print(' The server couldn\'t fulfill the request.')
print(' Error code: ', e.code)
return None, None
else:
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
text = soup.get_text()
# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
lines = text.split('\n')
# Finds the title in the text
title = lines[0].replace(' - Guides - Wowhead','')
# Finds the Context - It is the line after "ReportLinks"
content = ''
nextIsContent = False
for line in lines:
if nextIsContent:
content += line
if 'ReportLinks' in line:
nextIsContent = True
if 'Share your comments about ' in line:
break
return title, content
#return title, '\n'.join(lines)
def classSpecCombos(self):
combos = []
combos.append(['Blood', 'Death Knight'])
combos.append(['Frost', 'Death Knight'])
combos.append(['Unholy', 'Death Knight'])
combos.append(['Havoc', 'Demon Hunter'])
combos.append(['Vengeance', 'Demon Hunter'])
combos.append(['Balance', 'Druid'])
combos.append(['Guardian', 'Druid'])
combos.append(['Feral', 'Druid'])
combos.append(['Restoration', 'Druid'])
combos.append(['Beast Mastery', 'Hunter'])
combos.append(['Marksmanship', 'Hunter'])
combos.append(['Survival', 'Hunter'])
combos.append(['Arcane', 'Mage'])
#return combos
combos.append(['Fire', 'Mage'])
combos.append(['Frost', 'Mage'])
combos.append(['Brewmaster', 'Monk'])
combos.append(['Mistweaver', 'Monk'])
combos.append(['Windwalker', 'Monk'])
combos.append(['Holy', 'Paladin'])
combos.append(['Protection', 'Paladin'])
combos.append(['Retribution', 'Paladin'])
combos.append(['Discipline', 'Priest'])
combos.append(['Holy', 'Priest'])
combos.append(['Shadow', 'Priest'])
combos.append(['Assassination', 'Rogue'])
combos.append(['Outlaw', 'Rogue'])
combos.append(['Subtlety', 'Rogue'])
combos.append(['Elemental', 'Shaman'])
combos.append(['Enhancement', 'Shaman'])
combos.append(['Restoration', 'Shaman'])
combos.append(['Affliction', 'Warlock'])
combos.append(['Destruction', 'Warlock'])
combos.append(['Demonology', 'Warlock'])
combos.append(['Arms', 'Warrior'])
combos.append(['Fury', 'Warrior'])
combos.append(['Protection', 'Warrior'])
return combos