-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathillustrate_wikidata.py
executable file
·112 lines (82 loc) · 3.19 KB
/
illustrate_wikidata.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
#!/usr/bin/env python3
"""Bot to add images to Wikidata items.
The image is extracted from the page_props. For this to be available the
PageImages extension
(https://www.mediawiki.org/wiki/Extension:PageImages) needs to be
installed.
The following options are provided:
-always Don't prompt to make changes, just do them.
-property The property to add. Should be of type commonsMedia.
Usage:
python pwb.py illustrate_wikidata <some generator>
¶ms;
"""
#
# (C) Pywikibot team, 2013-2024
#
# Distributed under the terms of MIT license.
#
from __future__ import annotations
import pywikibot
from pywikibot import WikidataBot, pagegenerators
docuReplacements = {'¶ms;': pagegenerators.parameterHelp} # noqa: N816
class IllustrateRobot(WikidataBot):
"""A bot to add Wikidata image claims."""
update_options = {
'property': 'P18',
}
def __init__(self, **kwargs) -> None:
"""Initializer."""
super().__init__(**kwargs)
self.cacheSources()
claim = pywikibot.Claim(self.repo, self.opt.property)
if claim.type != 'commonsMedia':
raise ValueError(f'{self.opt.property} is of type {claim.type},'
' should be commonsMedia')
def treat_page_and_item(self, page, item) -> None:
"""Treat a page / item."""
pywikibot.info('Found ' + item.title())
imagename = page.properties().get('page_image_free')
if not imagename:
return
claims = item.get().get('claims')
if self.opt.property in claims:
pywikibot.info(f'Item {item.title()} already contains image '
f'({self.opt.property})')
return
newclaim = pywikibot.Claim(self.repo, self.opt.property)
commonssite = pywikibot.Site('commons')
imagelink = pywikibot.Link(imagename, source=commonssite,
default_namespace=6)
image = pywikibot.FilePage(imagelink)
if image.isRedirectPage():
image = pywikibot.FilePage(image.getRedirectTarget())
if not image.exists():
pywikibot.info(f"{image} doesn't exist so I can't link to it")
return
newclaim.setTarget(image)
# A generator might yield pages from multiple sites
self.user_add_claim(item, newclaim, page.site)
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
"""
# Process global args and prepare generator args parser
local_args = pywikibot.handle_args(args)
generator_factory = pagegenerators.GeneratorFactory()
options = {}
for arg in local_args:
opt, _, value = arg.partition(':')
if opt == '-property':
options['property'] = value or pywikibot.input(
'Please enter the property you want to add:')
elif opt == '-always':
options[opt[1:]] = True
else:
generator_factory.handle_arg(arg)
options['generator'] = generator_factory.getCombinedGenerator(preload=True)
bot = IllustrateRobot(**options)
bot.run()
if __name__ == '__main__':
main()