-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfix_commons_labels.py
63 lines (49 loc) · 2.02 KB
/
fix_commons_labels.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
#!/usr/bin/python
import pywikibot
from pywikibot import pagegenerators
from pywikibot.backports import removeprefix
from query_store import QueryStore
from wikidata import WikidataEntityBot
class LabelsFixingBot(WikidataEntityBot):
use_from_page = False
def __init__(self, generator, **kwargs):
self.available_options.update({
'always': True,
'limit': 50,
})
super().__init__(**kwargs)
self.store = QueryStore()
self._generator = generator or self.custom_generator()
self.summary = 'remove prefix from [en] label'
@property
def generator(self):
return pagegenerators.PreloadingEntityGenerator(self._generator)
def custom_generator(self):
query = self.store.build_query('commons_labels',
limit=self.opt['limit'])
return pagegenerators.WikidataSPARQLPageGenerator(query, site=self.repo)
def treat_page_and_item(self, page, item):
if any(cl.target_equals('Q4167836') for cl in item.claims.get('P31', [])):
return
if item.getSitelink('commonswiki').startswith('Category:'):
if item.labels['en'].startswith('Category:'):
data = {'en': removeprefix(item.labels['en'], 'Category:')}
self.user_edit_entity(item, {'labels': data},
summary=self.summary)
def main(*args):
options = {}
local_args = pywikibot.handle_args(args)
site = pywikibot.Site()
genFactory = pagegenerators.GeneratorFactory(site=site)
for arg in genFactory.handle_args(local_args):
if arg.startswith('-'):
arg, sep, value = arg.partition(':')
if value != '':
options[arg[1:]] = value if not value.isdigit() else int(value)
else:
options[arg[1:]] = True
generator = genFactory.getCombinedGenerator()
bot = LabelsFixingBot(generator=generator, site=site, **options)
bot.run()
if __name__ == '__main__':
main()