-
Notifications
You must be signed in to change notification settings - Fork 3
/
author_adder.py
113 lines (99 loc) · 3.56 KB
/
author_adder.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
# Author: Amir Sarabadani
# License: MIT
import pywikibot
from pywikibot import Bot, pagegenerators
import codecs
import json
import re
summary = 'Bot: Prova'
site = pywikibot.Site('it', 'wikisource')
def get_authors_list():
authors = []
category = pywikibot.Category(site, 'Categoria:Autori')
gen = pagegenerators.CategorizedPageGenerator(category)
for page in pagegenerators.PreloadingGenerator(gen):
authors.append(page.title(withNamespace=False))
return authors
def load_cache():
with codecs.open('authors.txt', 'r', 'utf-8') as f:
return json.load(f)
def write_cache(data=None):
if not data:
print('Loading names of authors...')
data = get_authors_list()
with codecs.open('authors.txt', 'w', 'utf-8') as f:
json.dump(data, f)
class AuthorBot(Bot):
"""AuthorBot"""
def __init__(self, gen, site, no_cache, last_name, auto):
super(AuthorBot, self).__init__()
self._site = site
self.generator = gen
if no_cache:
write_cache()
self.authors = {}
if not last_name:
for author in load_cache():
self.authors[author] = author
else:
for author in load_cache():
temp = author.split(' (')[0]
temp = re.split(r' I(?:\W|I|V|X|M|$)', temp)[0]
temp = temp.split(' ')[-1]
self.authors[temp] = author
self.auto = auto
def treat(self, page):
self.current_page = page
try:
text = page.get()
for author in self.authors:
if re.search(r'(\b%s\b)' % re.escape(author), text):
if self.authors[author] == author:
text = text.replace(
author, u'{{AutoreCitato|%s}}' % author)
else:
text = text.replace(
author, u'{{AutoreCitato|%s|%s}}'
% (self.authors[author], author))
if page.text != text:
if self.auto:
page.text = text
page.save(summary)
else:
pywikibot.showDiff(page.text, text)
choice = pywikibot.input('Agree? [Y]es, [N]o')
if choice.lower() == 'y':
page.text = text
page.save(summary)
except pywikibot.NoPage:
pywikibot.output(u"Page %s does not exist?!"
% page.title(asLink=True))
except pywikibot.IsRedirectPage:
pywikibot.output(u"Page %s is a redirect; skipping."
% page.title(asLink=True))
except pywikibot.LockedPage:
pywikibot.output(u"Page %s is locked?!" % page.title(asLink=True))
def main(*args):
no_cache = False
last_name = False
auto = False
local_args = pywikibot.handle_args(args)
genFactory = pagegenerators.GeneratorFactory()
for arg in local_args:
if genFactory.handleArg(arg):
continue
elif arg == '--nocache':
no_cache = True
elif arg == '--last':
last_name = True
elif arg == '--auto':
auto = True
gen = genFactory.getCombinedGenerator()
if not gen:
pywikibot.error('You must define something')
preloading_gen = pagegenerators.PreloadingGenerator(gen)
bot = AuthorBot(preloading_gen, site, no_cache, last_name, auto)
site.login()
bot.run()
if __name__ == "__main__":
main()