-
Notifications
You must be signed in to change notification settings - Fork 0
/
capitalize_redirects.py
117 lines (102 loc) · 3.72 KB
/
capitalize_redirects.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Bot to create capitalized redirects where the first character of the first
word is uppercase and the remainig characters and words are lowercase.
Command-line arguments:
¶ms;
-always Don't prompt to make changes, just do them.
-titlecase creates a titlecased redirect version of a given page
where all words of the title start with an uppercase
character and the remaining characters are lowercase.
Example: "python capitalize_redirects.py -start:B -always"
'''
#
# (C) Yrithinnd
# (C) Pywikipedia bot team, 2007-2010
#
# Class licensed under terms of the MIT license
#
__version__ = '$Id$'
#
import time, sys, re
import wikipedia as pywikibot
from pywikibot import i18n
import pagegenerators
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp
}
class CapitalizeBot:
def __init__(self, generator, acceptall, titlecase):
self.generator = generator
self.acceptall = acceptall
self.titlecase = titlecase
self.site = pywikibot.getSite()
self.done = False
def run(self):
for page in self.generator:
if self.done: break
if page.exists():
self.treat(page)
def treat(self, page):
if page.isRedirectPage():
page = page.getRedirectTarget()
page_t = page.title()
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(u"\n>>> \03{lightpurple}%s\03{default} <<<"
% page_t)
if self.titlecase:
page_cap = pywikibot.Page(self.site, page_t.title())
else:
page_cap = pywikibot.Page(self.site, page_t.capitalize())
if page_cap.exists():
pywikibot.output(u'%s already exists, skipping...\n'
% page_cap.title(asLink=True))
else:
pywikibot.output(u'[[%s]] doesn\'t exist' % page_cap.title())
if not self.acceptall:
choice = pywikibot.inputChoice(
u'Do you want to create a redirect?',
['Yes', 'No', 'All', 'Quit'], ['y', 'N', 'a', 'q'], 'N')
if choice == 'a':
self.acceptall = True
elif choice == 'q':
self.done = True
if self.acceptall or choice == 'y':
comment = i18n.twtranslate(self.site,
'capitalize_redirects-create-redirect',
{'to': page_t})
try:
page_cap.put(u"#%s %s" % (self.site.redirect(),
page.title(asLink=True,
textlink=True)),
comment)
except:
pywikibot.output(u"An error occurred, skipping...")
def main():
genFactory = pagegenerators.GeneratorFactory()
acceptall = False
titlecase = False
for arg in pywikibot.handleArgs():
if arg == '-always':
acceptall = True
elif arg == '-titlecase':
titlecase = True
elif genFactory.handleArg(arg):
pass
else:
pywikibot.showHelp(u'capitalize_redirects')
return
gen = genFactory.getCombinedGenerator()
preloadingGen = pagegenerators.PreloadingGenerator(gen)
bot = CapitalizeBot(preloadingGen, acceptall, titlecase)
try:
bot.run()
except KeyboardInterrupt:
pywikibot.output('\nQuitting program...')
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()