-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiwm.py
401 lines (340 loc) · 14.9 KB
/
iwm.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
import sys, graphviz, re, json, os
import urllib.request, urllib.parse, urllib.error
from graphviz import Digraph
if sys.version_info[0] > 2:
from urllib.error import HTTPError
import urllib.request as urllib2
raw_input = input
else:
import urllib.request, urllib.error, urllib.parse
from urllib.error import HTTPError
def urlopen(url):
req = urllib.request.Request(url, headers = {'User-agent': 'Interwiki Graph Generator'})
uo = urllib.request.urlopen(req)
try:
if sys.version_info[0] > 2:
uo.charset = uo.headers.get_content_charset()
else:
uo.charset = uo.headers.getfirstmatchingheader('Content-Type')[0].strip().split('charset=')[1]
except IndexError:
uo.charset = 'latin-1'
return uo
class Graph(object):
showGood = 2
showOneWay = 2
showRedir = 2
showBadRedir = 2
showBad = 2
showBroken = 2
def __init__(self, *args, **kwargs):
self.dot = graphviz.Digraph(*args, **kwargs)
self.dot.engine = 'circo'
self.dot.format = 'png'
self.nodes = {}
self.edges = {}
self.name = None
self.depth = None
self.checkall = None
self.dot.attr('node', shape='circle')
def node(self, wiki, **kwargs):
if wiki.id in self.nodes: return
self.nodes[wiki.id] = True
args = {
'URL': 'http://%s/' % wiki.url
}
args.update(kwargs)
if wiki.invalid:
args['color'] = 'red'
self.dot.node(wiki.id, '%s\\n%s' % ('--', wiki.id), comment = 'invalid', **args)
else:
self.dot.node(wiki.id, '%s\\n%s' % (wiki.lang, wiki.id), **args)
def edge(self, w1, w2):
w1, w2 = sorted([w1, w2], key=lambda x: x.id)
if w1.id not in self.edges: self.edges[w1.id] = {}
if w2.id in self.edges[w1.id]: return
self.edges[w1.id][w2.id] = True
w1, w2 = sorted([w1, w2], key=lambda x: x.level)
print(('\nEdge between %s and %s' % (w1, w2)))
if self.invalidEdge(w1, w2): return
self.goodEdges(w1, w2)
self.badEdges(w1, w2)
def goodEdges(self, w1, w2):
a = self._goodEdge(w1, w2)
b = self._goodEdge(w2, w1)
if a and b:
if self.showGood > 0: self.dot.edge(w1.id, w2.id, color='green', dir='both', weight='2', style=[None,'invis','bold'][self.showGood], comment='good')
print('Good both ways')
elif a and not b:
if self.showOneWay > 0: self.dot.edge(w1.id, w2.id, comment='good one-way', style=[None,'invis',''][self.showOneWay])
print(('Only \'%s\' correctly points to %s' % (w1, w2)))
elif not a and b:
if self.showOneWay > 0: self.dot.edge(w2.id, w1.id, comment='good one-way', style=[None,'invis',''][self.showOneWay])
print(('Only \'%s\' correctly points to %s' % (w2, w1)))
def _goodEdge(self, w1, w2):
try:
return Wiki.reURL.match(w1.langs[w2.lang]['url']).group(1) == w2.url # w1 corretly points to w2
except (AttributeError, KeyError):
pass
return False
def badEdges(self, w1, w2):
self._badEdge(w1, w2)
self._badEdge(w2, w1)
def _badEdge(self, w1, w2):
print(('%s -> %s' % (w1, w2)))
badlinks = set()
for lang, data in sorted(w1.langs.items()):
wiki = Wiki(data['url'])
if wiki.id != w2.id: continue
try:
url = Wiki.reURL.match(data['url']).group(1)
except (AttributeError, KeyError):
url = ''
if url != w2.url:
if w2.lang == lang:
print(('\'%s\' points to a redirect (http://%s/) to %s' % (lang, url, w2)))
if self.showRedir > 0: self.dot.edge(w1.id, w2.id, color='darkorange', headURL='http://%s/' % url, comment='redirect', style=[None,'invis',''][self.showRedir])
else:
print(('\'%s\' points to a redirect (http://%s/) to %s under wrong lang prefix' % (lang, url, w2)))
if self.showBadRedir > 0: self.dot.edge(w1.id, w2.id, color='purple', fontcolor='purple', label=lang+':', headURL='http://%s/' % url, comment='wrong redirect', style=[None,'invis',''][self.showBadRedir])
elif w2.lang != lang:
badlinks.add(lang)
print(('\'%s\' points to %s under wrong lang prefix' % (lang, w2)))
if len(badlinks) > 2:
if self.showBad > 0: self.dot.edge(w1.id, w2.id, color='brown', fontcolor='brown', label='%d links' % len(badlinks), comment='wrong', style=[None,'invis','bold'][self.showBad])
self.dumpLinks(w1, w2, badlinks)
else:
for lang in badlinks:
if self.showBad > 0: self.dot.edge(w1.id, w2.id, color='brown', fontcolor='brown', label=lang+':', comment='wrong', style=[None,'invis',''][self.showBad])
def invalidEdge(self, w1, w2):
return self._invalidEdge(w1, w2) or self._invalidEdge(w2, w1)
def _invalidEdge(self, w1, w2):
if w1.invalid:
for lang, data in sorted(w2.langs.items()):
if Wiki(data['url']) != w1: continue
if self.showBroken > 0: self.dot.edge(w2.id, w1.id, color='red', fontcolor='red', label=lang+':', style=[None,'invis',''][self.showBroken])
print(('Invalid \'%s\' link to %s' % (lang, w1)))
return True
return False
@property
def source(self):
return self.dot.source
def dumpLinks(self, w1, w2, links):
try:
os.mkdir(self.name)
except OSError:
pass
f = open('%s/badlinks %s-%s.txt' % (self.name, w1.id, w2.id), 'w')
f.write(' '.join(sorted(links)))
f.close()
def filename(self, ext = 'gv'):
return '%s/%s L%d %s [%s,%s,%s,%s,%s,%s].%s' % (
self.name,
self.name,
self.depth,
['R','A'][int(self.checkall)],
['I','H','S'][self.showGood],
['I','H','S'][self.showOneWay],
['I','H','S'][self.showRedir],
['I','H','S'][self.showBadRedir],
['I','H','S'][self.showBad],
['I','H','S'][self.showBroken],
ext,
)
def save(self, *args, **kwargs):
f = self.filename()
self.dot.save(f, *args, **kwargs)
def render(self, *args, **kwargs):
f = self.filename()
self.dot.render(f, *args, **kwargs)
class GraphGenerator(object):
__instance = None
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = super(GraphGenerator,cls).__new__(cls)
cls.__instance.__initialized = False
return cls.__instance
def __init__(self, url = None, depth = None, checkall = None, showlinks = None):
if(self.__initialized): return
self.__initialized = True
if url is None:
url = input("\nPlease insert URL to wiki: ")
self.url = url
if depth is None:
depth = input("\nHow many levels do you want to check (0 - only ones linked from root): ")
self.depth = int(depth)
if checkall is None:
checkall = input("\nDo you want to check links between [a]ll found wikis or only for [r]oot: ").lower() == 'a'
else:
checkall = checkall == '1' or checkall.lower() == 'y'
self.checkall = checkall
if showlinks is None:
print('How do you want to display link types?')
print('[Show/Hide/Ignore]')
showlinks = []
a = input("Good both ways links? (green): ").lower()
if a == 's': showlinks.append(2)
elif a == 'h': showlinks.append(1)
else: showlinks.append(0)
a = input("Good one way links? (black): ").lower()
if a == 's': showlinks.append(2)
elif a == 'h': showlinks.append(1)
else: showlinks.append(0)
a = input("Good redirects? (orange): ").lower()
if a == 's': showlinks.append(2)
elif a == 'h': showlinks.append(1)
else: showlinks.append(0)
a = input("Bad redirects? (purple): ").lower()
if a == 's': showlinks.append(2)
elif a == 'h': showlinks.append(1)
else: showlinks.append(0)
a = input("Bad links? (brown): ").lower()
if a == 's': showlinks.append(2)
elif a == 'h': showlinks.append(1)
else: showlinks.append(0)
a = input("Broken links? (red): ").lower()
if a == 's': showlinks.append(2)
elif a == 'h': showlinks.append(1)
else: showlinks.append(0)
else:
showlinks = showlinks.lower().split(',')
for i in range(6):
try:
if showlinks[i] == 's': showlinks[i] = 2
elif showlinks[i] == 'h': showlinks[i] = 1
else: showlinks[i] = 0
except IndexError:
showlinks.append(2)
self.showlinks = showlinks
self.root = root = Wiki(url, 0)
self.all = set([root])
self.dot = dot = Graph(comment='Interwiki map for http://%s/' % self.url)
dot.name = re.sub('[\\\/]', '_', root.id)
dot.depth = self.depth
dot.checkall = self.checkall
dot.showGood, dot.showOneWay, dot.showRedir, dot.showBadRedir, dot.showBad, dot.showBroken = (self.showlinks + ['s','s','s','s','s','s'])[:6]
print('\n\n')
def nodes(self, wiki):
print(('Working on http://%s/ -- level: %d' % (wiki.url, wiki.level)))
self.dot.node(self.root, color='lightblue' if wiki.level == 0 else '')
print(('Has %d language links' % len(list(wiki.langs.keys()))))
self.all.add(wiki)
langs = sorted(wiki.langs.items())
for lang, data in langs:
w = Wiki(data['url'])
print(('Found link to http://%s/' % w.url))
self.dot.node(w)
self.all.add(w)
wiki.done = True
if wiki.level+1 > self.depth: return
for lang, data in langs:
w = Wiki(data['url'])
if not w.done:
self.nodes(w)
def run(self):
self.nodes(self.root)
self.edges()
def edges(self):
if self.checkall:
all = sorted(list(self.all), key=lambda x: x.id)
for i, w1 in enumerate(all):
for j, w2 in enumerate(all[i+1:]):
self.dot.edge(w1, w2)
else:
all = sorted(list(self.all), key=lambda x: x.id)
for i, w1 in enumerate(all):
self.dot.edge(self.root, w1)
self.dot.edge(w1, self.root)
def source(self):
return self.dot.source
def log(self):
wikis = set([Wiki.cache[k] for k in Wiki.cache])
with open(self.dot.filename('txt'), 'w') as file:
valid = [wiki.url for wiki in wikis if not wiki.invalid]
if len(valid) > 0:
file.write('Found wikis:\n')
file.write('\n'.join(sorted(valid)))
file.write('\n\n')
invalid = [wiki.url for wiki in wikis if wiki.invalid]
if len(invalid) > 0:
file.write('Invalid wikis:\n')
file.write('\n'.join(sorted(invalid)))
file.write('\n\n')
class Wiki(object):
cache = {}
reURL = re.compile('(?:https?:\/\/)?(?:[^@\n]+@)?((?:www\.)?[^:\/\n]+(?:\/[a-z-][a-z-]+)?)', re.I)
def __new__(cls, *args, **kwargs):
match = Wiki.reURL.match(args[0])
url = match.group(1)
if url.endswith('/wiki'):
url = url[:-5]
if url in Wiki.cache:
return Wiki.cache[url]
obj = super(Wiki,cls).__new__(cls)
obj.__initialized = False
return obj
def __init__(self, url, level = 1):
if self.__initialized:
self.level = min(level, self.level)
return
self.__initialized = True
self.id = None
self.lang = None
self.setUrl(url)
self.api = 'http://%s/api.php' % self.url
self.level = level
self.invalid = False
self.done = False
self.info()
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.id == other.id
return False
def __hash__(self):
return hash(self.id)
def setUrl(self, url):
match = Wiki.reURL.match(url)
url = match.group(1)
if url.endswith('/wiki'):
url = url[:-5]
self.url = url
self.id = url.replace('.wikia.com', '')
Wiki.cache[url] = self
def info(self):
#print('Fetching info about http://%s/' % self.url)
try:
data = urlopen(self.api + "?action=query&meta=siteinfo&siprop=general|interwikimap&sifilteriw=local&format=json")
res = json.loads(data.read().decode(data.charset))
if 'error' in res:
raise RuntimeError('%s - %s' % (res['error']['code'], res['error']['info']))
self.setUrl(res['query']['general']['server'] + res['query']['general']['scriptpath'])
self.lang = res['query']['general']['lang']
self.langs = {wiki['prefix']: wiki for wiki in res['query']['interwikimap']
if 'language' in wiki}
except (HTTPError, ValueError) as e:
self.langs = {}
self.invalid = True
def __repr__(self):
if hasattr(self, 'url'):
return 'Wiki(%s, %s)' % (self.id, self.lang or '-')
return 'new Wiki()'
if __name__ == "__main__":
print("This script is lazy and most likely won't work for non-Wikia wikis\n")
if len(sys.argv) != 5:
print(("Usage: %s <url> <depth> <checkall> <showlinks>" % sys.argv[0]))
print("url: url of the wiki")
print("depth: how many levels to check <0|1|2|...>")
print("checkall: check links between all wikis or only between root and target <1|0>")
print("showlinks: what types of links should be shown, hidden or ignored")
print(" comma separated list of <s|h|i>")
print(" order: good both ways, good one-way, good redirs, bad redirs, bad, broken")
print(" ommited entries will be 's'")
gen = GraphGenerator(*sys.argv[1:])
gen.run()
print('\n\n')
#os.system('pause')
print('\n\n')
#print(gen.source())
gen.dot.save()
gen.log()
if input('Render? ').lower() == 'y':
gen.dot.render(view=True)