-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathimagetransfer.py
executable file
·415 lines (361 loc) · 15.4 KB
/
imagetransfer.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python3
"""Script to copy images to Wikimedia Commons, or to another wiki.
Syntax:
python pwb.py imagetransfer {<pagename>|<generator>} [<options>]
The following parameters are supported:
-interwiki Look for images in pages found through interwiki
links.
-keepname Keep the filename and do not verify description while
replacing.
-tolang:x [str] Copy the image to the wiki in code x.
-tofamily:y [str] Copy the image to a wiki in the family y.
-tosite:s [str] Copy the image to the given site like
wikipedia:test.
-force_if_shared Upload the file to the target, even if it exists on
that wiki's shared repo
-asynchronous Upload to stash.
-chunk_size:n [int] Upload in chunks of n bytes.
-file:z [str] Upload many files from textfile z
like:
.. code:: wikitext
[[Image:x]]
[[Image:y]]
If pagename is an image description page, offers to copy the image to
the target site. If it is a normal page, it will offer to copy any of
the images used on that page, or if the -interwiki argument is used, any
of the images used on a page reachable via interwiki links.
¶ms;
"""
#
# (C) Pywikibot team, 2004-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import re
import sys
import pywikibot
from pywikibot import config, i18n, pagegenerators, textlib
from pywikibot.bot import ExistingPageBot, SingleSiteBot
from pywikibot.exceptions import IsRedirectPageError, NoPageError
from pywikibot.specialbots import UploadRobot
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp
}
nowCommonsTemplate = {
'ar': '{{الآن كومنز|%s}}',
'ary': '{{Now Commons|%s}}',
'arz': '{{Now Commons|%s}}',
'ckb': '{{لە کۆمنز بەردەستە|%s}}',
'de': '{{NowCommons|%s}}',
'fr': '{{Désormais sur Commons|%s}}',
'en': '{{subst:ncd|Image:%s}}',
'fa': '{{موجود در انبار|%s}}',
'he': '{{גם בוויקישיתוף|%s}}',
'hu': '{{azonnali-commons|Kép:%s}}',
'ia': '{{OraInCommons|Imagine:%s}}',
'it': '{{NowCommons unlink|%s}}',
'ja': '{{NowCommons|Image:%s}}',
'kk': '{{NowCommons|Image:%s}}',
'li': '{{NowCommons|%s}}',
'lt': '{{NowCommons|Image:%s}}',
'nds-nl': '{{NoenCommons|File:%s}}',
'nl': '{{NuCommons|Image:%s}}',
'pl': '{{NowCommons|%s}}',
'pt': '{{NowCommons|%s}}',
'sr': '{{NowCommons|%s}}',
'zh': '{{NowCommons|Image:%s}}',
}
# Translations for license templates.
# Must only be given when they are in fact different.
licenseTemplates = {
('wikipedia:ar', 'commons:commons'): {
'رخصة جنو للوثائق الحرة': 'GFDL',
'رخصة جنو للوثائق الحرة - شخصي': 'GFDL-self',
'ملكية عامة': 'PD',
'ملكية عامة - شخصي': 'PD-self',
'ملكية عامة - فن': 'PD-Art',
'ملكية عامة - الحكومة الأمريكية': 'PD-USGov',
},
('wikipedia:ckb', 'commons:commons'): {
'PD-because': 'PD',
'پاوانی گشتی-خۆ': 'PD-self',
'مۆڵەتنامەی گنو بۆ بەڵگە ئازادەکان': 'GFDL',
'مۆڵەتنامەی گنو بۆ بەڵگە ئازادەکان-خۆ': 'GFDL-self',
'پاوانی گشتی-ئێران': 'PD-Iran',
'PD-textlogo': 'PD-textlogo',
'پاوانی گشتی-عێراق': 'PD-Iraq',
'PD-UK': 'PD-UK',
'پاوانی گشتی-ھابڵ': 'PD-Hubble',
'پاوانی گشتی-ویلایەتە یەکگرتووەکان': 'PD-US',
'Cc-by-sa-2.5': 'Cc-by-sa-2.5',
'Cc-by-sa-3.0': 'Cc-by-3.0',
},
('wikipedia:de', 'commons:commons'): {
'Bild-GFDL': 'GFDL',
'Bild-GFDL-OpenGeoDB': 'GFDL-OpenGeoDB',
'Bild-Innweb-Lizenz': 'Map-Austria-GNU',
'Bild-PD': 'PD',
'Bild-PD-alt': 'PD-old',
'Bild-PD-Kunst': 'PD-Art',
'Bild-PD-US': 'PD-USGov',
},
('wikipedia:fa', 'commons:commons'): {
'مالکیت عمومی': 'PD',
'مالکیت عمومی-خود': 'PD-self',
'مجوز گنو': 'GFDL',
'مجوز گنو-خود': 'GFDL-self',
'نگاره قدیمی': 'PD-Iran',
'نگاره نوشتاری': 'PD-textlogo',
'نگاره عراقی': 'PD-Iraq',
'نگاره بریتانیا': 'PD-UK',
'نگاره هابل': 'PD-Hubble',
'نگاره آمریکا': 'PD-US',
'نگاره دولت آمریکا': 'PD-USGov',
'کک-یاد-دو': 'Cc-by-2.0',
'کک-یاد-حفظ-دونیم': 'Cc-by-sa-2.5',
'کک-یاد-سه': 'Cc-by-3.0',
},
('wikipedia:fr', 'commons:commons'): {
'Domaine public': 'PD'
},
('wikipedia:he', 'commons:commons'): {
'שימוש חופשי': 'PD-self',
'שימוש חופשי מוגן': 'Copyrighted free use',
'שימוש חופשי מוגן בתנאי': 'Copyrighted free use provided that',
'תמונה ישנה': 'PD-Israel',
'ייחוס': 'Attribution',
'לוגו ויקימדיה': 'Copyright by Wikimedia',
},
('wikipedia:hu', 'commons:commons'): {
'Közkincs': 'PD',
'Közkincs-régi': 'PD-old',
},
('wikipedia:pt', 'commons:commons'): {
'Domínio público': 'PD',
},
}
class ImageTransferBot(SingleSiteBot, ExistingPageBot):
"""Image transfer bot."""
update_options = {
'ignore_warning': False, # not implemented yet
'interwiki': False,
'keepname': False,
'target': None,
'force_if_shared': False,
'asynchronous': False,
'chunk_size': 0,
}
def __init__(self, **kwargs) -> None:
"""Initializer.
:keyword generator: the pages to work on
:type generator: iterable
:keyword target_site: Site to send image to, default none
:type target_site: pywikibot.site.APISite
:keyword interwiki: Look for images in interwiki links, default false
:type interwiki: boolean
:keyword keepname: Keep the filename and do not verify description
while replacing, default false
:type keepname: boolean
:keyword force_if_shared: Upload the file even if it's currently
shared to the target site (e.g. when moving from Commons to another
wiki)
:type force_if_shared: boolean
:keyword asynchronous: Upload to stash.
:type asynchronous: boolean
:keyword chunk_size: Upload in chunks of this size bytes.
:type chunk_size: integer
"""
super().__init__(**kwargs)
if self.opt.target is None:
self.opt.target = self.site.image_repository()
else:
self.opt.target = pywikibot.Site(self.opt.target)
def transfer_image(self, sourceImagePage) -> None:
"""Download image and its description, and upload it to another site.
:return: the filename which was used to upload the image
"""
def delete_source(old_filename, target_filename):
"""Delete source image or tag nowCommons template to it.
This function is called when upload to Commons was
successful.
"""
if not target_filename \
or self.opt.target.sitename != 'commons:commons':
return
reason = i18n.twtranslate(sourceSite,
'imagetransfer-nowcommons_notice')
# try to delete the original image if we have a sysop account
if sourceSite.has_right('delete') \
and sourceImagePage.delete(reason):
return
if sourceSite.lang in nowCommonsTemplate \
and sourceSite.family.name in config.usernames \
and sourceSite.lang in config.usernames[sourceSite.family.name]:
# add the nowCommons template.
pywikibot.info('Adding nowCommons template to '
+ sourceImagePage.title())
sourceImagePage.put(sourceImagePage.get() + '\n\n'
+ nowCommonsTemplate[sourceSite.code]
% target_filename,
summary=reason)
sourceSite = sourceImagePage.site
pywikibot.info(
f'\n>>> Transfer {sourceImagePage} from {sourceImagePage.site} '
f'to {self.opt.target}\n'
)
url = sourceImagePage.get_file_url()
pywikibot.info('URL should be: ' + url)
# localize the text that should be printed on image description page
try:
description = sourceImagePage.get()
# try to translate license templates
if (sourceSite.sitename,
self.opt.target.sitename) in licenseTemplates:
for old, new in licenseTemplates[
(sourceSite.sitename,
self.opt.target.sitename)].items():
new = '{{%s}}' % new
old = re.compile('{{%s}}' % old)
description = textlib.replaceExcept(description, old, new,
['comment', 'math',
'nowiki', 'pre'])
description = i18n.twtranslate(self.opt.target,
'imagetransfer-file_page_message',
{'site': sourceSite,
'description': description})
description += '\n\n'
description += sourceImagePage.getFileVersionHistoryTable()
# add interwiki link
if sourceSite.family == self.opt.target.family:
description += f'\n\n{sourceImagePage}'
except NoPageError:
pywikibot.info(
'Image does not exist or description page is empty.')
except IsRedirectPageError:
pywikibot.info('Image description page is redirect.')
else:
bot = UploadRobot(url=url, description=description,
target_site=self.opt.target,
url_encoding=sourceSite.encoding(),
keep_filename=self.opt.keepname,
verify_description=not self.opt.keepname,
ignore_warning=self.opt.ignore_warning,
force_if_shared=self.opt.force_if_shared,
asynchronous=self.opt.asynchronous,
chunk_size=self.opt.chunk_size)
bot.post_processor = delete_source
bot.run()
def show_image_list(self, imagelist) -> None:
"""Print image list."""
pywikibot.info('-' * 60)
for i, image in enumerate(imagelist):
pywikibot.info(f'{i}. Found image: {image}')
try:
# Show the image description page's contents
pywikibot.info(image.get())
except NoPageError:
pass
else:
# look if page already exists with this name.
# TODO: consider removing this: a different image of the same
# name may exist on the target wiki, and the bot user may want
# to upload anyway, using another name.
try:
# Maybe the image is on the target site already
targetTitle = 'File:' + image.title().split(':', 1)[1]
targetImage = pywikibot.Page(self.opt.target, targetTitle)
targetImage.get()
pywikibot.info(f'Image with this name is already on '
f'{self.opt.target}.')
pywikibot.info('-' * 60)
pywikibot.info(targetImage.get())
sys.exit()
except NoPageError:
# That's the normal case
pass
except IsRedirectPageError:
pywikibot.info(
'Description page on target wiki is redirect?!')
pywikibot.info('=' * 60)
def treat(self, page) -> None:
"""Treat a single page."""
if self.opt.interwiki:
imagelist = []
for linkedPage in page.interwiki():
linkedPage = pywikibot.Page(linkedPage)
imagelist.extend(linkedPage.imagelinks())
elif page.is_filepage():
imagePage = pywikibot.FilePage(page.site, page.title())
imagelist = [imagePage]
else:
imagelist = list(page.imagelinks())
while imagelist:
self.show_image_list(imagelist)
if len(imagelist) == 1:
# no need to query the user, only one possibility
todo = 0
else:
pywikibot.info('Give the number of the image to transfer.')
todo = pywikibot.input('To end uploading, press enter:')
if not todo:
break
todo = int(todo)
if 0 <= todo < len(imagelist):
if self.transfer_allowed(imagelist[todo]):
self.transfer_image(imagelist[todo])
# remove the selected image from the list
imagelist.pop(todo)
else:
pywikibot.info('<<yellow>>No such image number.')
def transfer_allowed(self, image) -> bool:
"""Check whether transfer is allowed."""
target_repo = self.opt.target.image_repository()
if not self.opt.force_if_shared \
and image.file_is_shared() \
and image.site.image_repository() == target_repo:
pywikibot.info(
f'<<yellow>>The image is already shared on {target_repo}.')
return False
return True
def main(*args: str) -> None:
"""Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
:param args: command line arguments
"""
target_code = None
target_family = None
options = {}
local_args = pywikibot.handle_args(args)
generator_factory = pagegenerators.GeneratorFactory(
positional_arg_name='page')
for arg in local_args:
opt, _, value = arg.partition(':')
if opt in ('-ignore_warning', '-interwiki', '-keepname',
'-force_if_shared', '-asynchronous'):
options[opt[1:]] = True
elif opt == '-tolang':
target_code = value
elif opt == '-tofamily':
target_family = value
elif opt == '-tosite':
options['target'] = value
elif opt == '-chunk_size':
options['chunk_size'] = value
else:
generator_factory.handle_arg(arg)
gen = generator_factory.getCombinedGenerator()
if not gen:
pywikibot.bot.suggest_help(
missing_parameters=['page'],
additional_text='and no other generator was defined.')
return
if target_code or target_family:
site = pywikibot.Site()
options.setdefault(
'target',
f'{target_family or site.family}:{target_code or site.lang}')
bot = ImageTransferBot(generator=gen, **options)
bot.run()
if __name__ == '__main__':
main()