-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
202 lines (178 loc) · 7.41 KB
/
image.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
# -*- coding: utf-8 -*-
"""
This script can be used to change one image to another or remove an image
entirely.
Syntax: python image.py image_name [new_image_name]
If only one command-line parameter is provided then that image will be removed;
if two are provided, then the first image will be replaced by the second one on
all pages.
Command line options:
-summary: Provide a custom edit summary. If the summary includes spaces,
surround it with single quotes, such as:
-summary:'My edit summary'
-always Don't prompt to make changes, just do them.
-loose Do loose replacements. This will replace all occurences of the name
of the image (and not just explicit image syntax). This should work
to catch all instances of the image, including where it is used as a
template parameter or in image galleries. However, it can also make
more mistakes. This only works with image replacement, not image
removal.
Examples:
The image "FlagrantCopyvio.jpg" is about to be deleted, so let's first remove it
from everything that displays it:
python image.py FlagrantCopyvio.jpg
The image "Flag.svg" has been uploaded, making the old "Flag.jpg" obselete:
python image.py Flag.jpg Flag.svg
"""
__version__ = '$Id$'
#
# Distributed under the terms of the MIT license.
#
import wikipedia as pywikibot
import replace, pagegenerators
import re
class ImageRobot:
"""
This robot will load all pages yielded by a file links image page generator and
replace or remove all occurences of the old image.
"""
# Summary messages for replacing images
msg_replace={
'ar': u'روبوت - استبدال الصورة %s مع %s',
'de': u'Bot: Ersetze Bild %s durch %s',
'en': u'Robot: Replacing image %s with %s',
'es': u'Robot - Reemplazando imagen %s por %s',
'fa': u'ربات: جایگزین کردن تصویر %s با %s',
'fr': u'Bot: Remplace image %s par %s',
'he': u'בוט: מחליף את התמונה %s בתמונה %s',
'it': u"Bot: Sostituisco l'immagine %s con %s",
'ja': u'ロボットによる:画像置き換え %s から %s へ',
'ko': u'로봇 - 그림 %s을 %s로 치환',
'lt': u'robotas: vaizdas %s keičiamas į %s',
'nn': u'robot: erstatta biletet %s med %s',
'no': u'robot: erstatter bildet %s med %s',
'nl': u'Bot: afbeelding %s vervangen door %s',
'pl': u'Robot zamienia obraz %s na %s',
'pt': u'Bot: Alterando imagem %s para %s',
'ru': u'Бот: Замена файла %s на %s',
'zh': u'機器人:取代圖像 %s 至 %s',
}
# Summary messages for removing images
msg_remove={
'ar': u'روبوت - إزالة الصورة %s',
'de': u'Bot: Entferne Bild %s',
'en': u'Robot: Removing image %s',
'es': u'Robot - Retirando imagen %s',
'fa': u'ربات: برداشتن تصویر %s',
'fr': u'Bot: Enleve image %s',
'he': u'בוט: מסיר את התמונה %s',
'it': u"Bot: Rimuovo l'immagine %s",
'ja': u'ロボットによる:画像削除 %s',
'ko': u'로봇 - %s 그림을 제거',
'lt': u'robotas: Šalinamas vaizdas %s',
'nl': u'Bot: afbeelding %s verwijderd',
'no': u'robot: fjerner bildet %s',
'nn': u'robot: fjerna biletet %s',
'pl': u'Robot usuwa obraz %s',
'pt': u'Bot: Alterando imagem %s',
'ru': u'Бот: удалил файл %s',
'zh': u'機器人:移除圖像 %s',
}
def __init__(self, generator, oldImage, newImage=None, summary='',
always=False, loose=False):
"""
Arguments:
* generator - A page generator.
* oldImage - The title of the old image (without namespace)
* newImage - The title of the new image (without namespace), or
None if you want to remove the image.
"""
self.generator = generator
self.oldImage = oldImage
self.newImage = newImage
self.editSummary = summary
self.summary = summary
self.always = always
self.loose = loose
# get edit summary message
mysite = pywikibot.getSite()
if summary:
self.editSummary = summary
elif self.newImage:
self.editSummary = pywikibot.translate(mysite, self.msg_replace) \
% (self.oldImage, self.newImage)
else:
self.editSummary = pywikibot.translate(mysite, self.msg_remove) \
% self.oldImage
def run(self):
"""
Starts the robot's action.
"""
# regular expression to find the original template.
# {{vfd}} does the same thing as {{Vfd}}, so both will be found.
# The old syntax, {{msg:vfd}}, will also be found.
# The group 'parameters' will either match the parameters, or an
# empty string if there are none.
replacements = []
site = pywikibot.getSite()
if not site.nocapitalize:
case = re.escape(self.oldImage[0].upper() + \
self.oldImage[0].lower())
escaped = '[' + case + ']' + re.escape(self.oldImage[1:])
else:
escaped = re.escape(self.oldImage)
# Be careful, spaces and _ have been converted to '\ ' and '\_'
escaped = re.sub('\\\\[_ ]', '[_ ]', escaped)
if not self.loose or not self.newImage:
ImageRegex = re.compile(r'\[\[ *(?:' + '|'.join(site.namespace(6, all = True)) + ')\s*:\s*' + escaped + ' *(?P<parameters>\|[^\n]+|) *\]\]')
else:
ImageRegex = re.compile(r'' + escaped)
if self.newImage:
if not self.loose:
replacements.append((ImageRegex, '[[' + site.image_namespace() + ':' + self.newImage + '\g<parameters>]]'))
else:
replacements.append((ImageRegex, self.newImage))
else:
replacements.append((ImageRegex, ''))
replaceBot = replace.ReplaceRobot(self.generator, replacements,
acceptall=self.always,
editSummary=self.editSummary)
replaceBot.run()
def main():
oldImage = None
newImage = None
summary = ''
always = False
loose = False
# read command line parameters
for arg in pywikibot.handleArgs():
if arg == '-always':
always = True
elif arg == '-loose':
loose = True
elif arg.startswith('-summary'):
if len(arg) == len('-summary'):
summary = pywikibot.input(u'Choose an edit summary: ')
else:
summary = arg[len('-summary:'):]
else:
if oldImage:
newImage = arg
else:
oldImage = arg
if not oldImage:
pywikibot.showHelp('image')
else:
mysite = pywikibot.getSite()
ns = mysite.image_namespace()
oldImagePage = pywikibot.ImagePage(mysite, ns + ':' + oldImage)
gen = pagegenerators.FileLinksGenerator(oldImagePage)
preloadingGen = pagegenerators.PreloadingGenerator(gen)
bot = ImageRobot(preloadingGen, oldImage, newImage, summary, always,
loose)
bot.run()
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()