-
Notifications
You must be signed in to change notification settings - Fork 0
/
commons_link.py
222 lines (200 loc) · 9.6 KB
/
commons_link.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
#!/usr/bin/python
# coding: utf-8
"""
Include commons template in home wiki.
This bot functions mainly in the en.wikipedia, because it
compares the names of articles and category in English
language (standard language in Commons). If the name of
an article in Commons will not be in English but with
redirect, this also functions.
Run:
Syntax: python commons_link.py action [-option]
where action can be one of these:
* pages : Run over articles, include {{commons}}
* categories : Run over categories, include {{commonscat}}
and option can be one of these:
* -cat : Work on all pages which are in a specific category.
* -ref : Work on all pages that link to a certain page.
* -link : Work on all pages that are linked from a certain page.
* -start : Work on all pages on the home wiki, starting at the named page.
* -page : Work on one page.
"""
#
# (C) Leonardo Gregianin, 2006
# (C) Pywikipedia team, 2007-2011
#
# Distributed under the terms of the MIT license.
#
__version__='$Id$'
import re
import wikipedia as pywikibot
import pagegenerators, catlib
comment1 = {
'ar':u'روبوت: تضمين قالب كومنز',
'cs':u'Robot přidal šablonu commons',
'en':u'Robot: Include commons template',
'fa':u'ربات: افزودن الگوی ویکیانبار',
'he':u'בוט: מוסיף תבנית Commons',
'ja':u'ロボットによる: テンプレcommons追加',
'nl':u'Bot: sjabloon commons toegevoegd',
'zh':u'機器人: 增加commons模板',
}
comment2 = {
'ar':u'روبوت: تضمين قالب تصنيف كومنز',
'cs':u'Robot přidal šablonu commonscat',
'en':u'Robot: Include commonscat template',
'fa':u'ربات: افزودن الگوی ردهبندی ویکیانبار',
'he':u'בוט: מוסיף תבנית Commonscat',
'ja':u'ロボットによる: テンプレcommonscat追加',
'nl':u'Bot: sjabloon commonscat toegevoegd',
'zh':u'機器人: 增加commonscat模板',
}
class CommonsLinkBot:
def __init__(self, generator, acceptall = False):
self.generator = generator
self.acceptall = acceptall
def pages(self):
for page in self.generator:
try:
pywikibot.output(u'\n>>>> %s <<<<' % page.title())
commons = pywikibot.getSite().image_repository()
commonspage = pywikibot.Page(commons, page.title())
try:
getcommons = commonspage.get(get_redirect=True)
if page.title() == commonspage.title():
oldText = page.get()
text = oldText
# for commons template
findTemplate=re.compile(ur'\{\{[Cc]ommonscat')
s = findTemplate.search(text)
findTemplate2=re.compile(ur'\{\{[Ss]isterlinks')
s2 = findTemplate2.search(text)
if s or s2:
pywikibot.output(u'** Already done.')
else:
text = pywikibot.replaceCategoryLinks(
text + u'{{commons|%s}}' % commonspage.title(),
page.categories())
if oldText != text:
pywikibot.showDiff(oldText, text)
if not self.acceptall:
choice = pywikibot.inputChoice(
u'Do you want to accept these changes?',
['Yes', 'No', 'All'], ['y', 'N', 'a'],
'N')
if choice == 'a':
self.acceptall = True
if self.acceptall or choice == 'y':
try:
msg = pywikibot.translate(
pywikibot.getSite(), comment1)
page.put(text, msg)
except pywikibot.EditConflict:
pywikibot.output(
u'Skipping %s because of edit conflict'
% (page.title()))
except pywikibot.NoPage:
pywikibot.output(u'Page does not exist in Commons!')
except pywikibot.NoPage:
pywikibot.output(u'Page %s does not exist?!' % page.title())
except pywikibot.IsRedirectPage:
pywikibot.output(u'Page %s is a redirect; skipping.'
% page.title())
except pywikibot.LockedPage:
pywikibot.output(u'Page %s is locked?!' % page.title())
def categories(self):
for page in self.generator:
try:
pywikibot.output(u'\n>>>> %s <<<<' % page.title())
commons = pywikibot.getSite().image_repository()
commonsCategory = catlib.Category(commons,
'Category:%s' % page.title())
try:
getcommonscat = commonsCategory.get(get_redirect=True)
commonsCategoryTitle = commonsCategory.title()
categoryname = commonsCategoryTitle.split('Category:',1)[1]
if page.title() == categoryname:
oldText = page.get()
text = oldText
# for commonscat template
findTemplate=re.compile(ur'\{\{[Cc]ommons')
s = findTemplate.search(text)
findTemplate2=re.compile(ur'\{\{[Ss]isterlinks')
s2 = findTemplate2.search(text)
if s or s2:
pywikibot.output(u'** Already done.')
else:
text = pywikibot.replaceCategoryLinks(
text + u'{{commonscat|%s}}' % categoryname,
page.categories())
if oldText != text:
pywikibot.showDiff(oldText, text)
if not self.acceptall:
choice = pywikibot.inputChoice(
u'Do you want to accept these changes?',
['Yes', 'No', 'All'], ['y', 'N', 'a'],
'N')
if choice == 'a':
self.acceptall = True
if self.acceptall or choice == 'y':
try:
msg = pywikibot.translate(
pywikibot.getSite(), comment2)
page.put(text, msg)
except pywikibot.EditConflict:
pywikibot.output(
u'Skipping %s because of edit conflict'
% (page.title()))
except pywikibot.NoPage:
pywikibot.output(u'Category does not exist in Commons!')
except pywikibot.NoPage:
pywikibot.output(u'Page %s does not exist?!' % page.title())
except pywikibot.IsRedirectPage:
pywikibot.output(u'Page %s is a redirect; skipping.'
% page.title())
except pywikibot.LockedPage:
pywikibot.output(u'Page %s is locked?!' % page.title())
if __name__ == "__main__":
singlepage = []
gen = None
start = None
try:
action = None
for arg in pywikibot.handleArgs():
if arg == ('pages'):
action = 'pages'
elif arg == ('categories'):
action = 'categories'
elif arg.startswith('-start:'):
start = pywikibot.Page(pywikibot.getSite(), arg[7:])
gen = pagegenerators.AllpagesPageGenerator(
start.title(withNamespace=False),
namespace=start.namespace(),
includeredirects = False)
elif arg.startswith('-cat:'):
cat = catlib.Category(pywikibot.getSite(),
'Category:%s' % arg[5:])
gen = pagegenerators.CategorizedPageGenerator(cat)
elif arg.startswith('-ref:'):
ref = pywikibot.Page(pywikibot.getSite(), arg[5:])
gen = pagegenerators.ReferringPageGenerator(ref)
elif arg.startswith('-link:'):
link = pywikibot.Page(pywikibot.getSite(), arg[6:])
gen = pagegenerators.LinkedPageGenerator(link)
elif arg.startswith('-page:'):
singlepage = pywikibot.Page(pywikibot.getSite(), arg[6:])
gen = iter([singlepage])
#else:
#bug
if action == 'pages':
preloadingGen = pagegenerators.PreloadingGenerator(gen)
bot = CommonsLinkBot(preloadingGen, acceptall=False)
bot.pages()
elif action == 'categories':
preloadingGen = pagegenerators.PreloadingGenerator(gen)
bot = CommonsLinkBot(preloadingGen, acceptall=False)
bot.categories()
else:
pywikibot.showHelp(u'commons_link')
finally:
pywikibot.stopme()