-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrahlada.py
executable file
·71 lines (58 loc) · 2.19 KB
/
Prahlada.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# MIT License
# Copyright (C) 2014 sitic
import pywikibot
import re
import mwparserfromhell as mwparser
import datetime
PurgePage = u'Benutzer:AsuraBot/Purges'
rePurge = re.compile(r"""<!-- AsuraBot purge Liste Start -->
(.*?)<!-- AsuraBot purge Liste Ende -->""", re.DOTALL)
reLink = re.compile(r"""<!-- AsuraBot forcelinkupdate Liste Start -->
(.*?)<!-- AsuraBot forcelinkupdate Liste Ende -->""", re.DOTALL)
reRec = re.compile(r"""<!-- AsuraBot forcerecursivelinkupdate Liste Start -->
(.*?)<!-- AsuraBot forcerecursivelinkupdate Liste Ende -->""", re.DOTALL)
ListRegex = [rePurge, reLink, reRec]
ListOptions = [None,
{'forcelinkupdate': 'true'},
{'forcerecursivelinkupdate': 'true'}]
class Purger():
def __init__(self):
self.site = pywikibot.Site()
self.site.login()
pywikibot.output(u'\n\ninit complete: ' +
(datetime.datetime.now()
.strftime('%d. %B %Y, %H:%M:%S')).decode('utf-8'))
page = pywikibot.Page(self.site, PurgePage)
if not page.exists():
pywikibot.output(u'\nERROR: page not found')
raise pywikibot.NoSuchSite
for regex, option in zip(ListRegex, ListOptions):
self.parse(regex, page.expand_text(includecomments=True), option)
def parse(self, regex, text, option):
result = regex.search(text)
if result:
code = mwparser.parse(result.group())
links = []
for i in code.filter_wikilinks():
links.append(i.title)
if option is None:
pywikibot.output('\npurging: ' + str(links))
else:
pywikibot.output('\n' + option.keys()[0] + u': ' + str(links))
if links:
self.purge(links, option)
def purge(self, pagetitles, option):
pages = []
for t in pagetitles:
pages.append(pywikibot.Page(self.site, t))
if option is None:
self.site.purgepages(pages)
else:
self.site.purgepages(pages, **option)
if __name__ == "__main__":
try:
Purger()
finally:
pywikibot.stopme()