-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathedit_vg_headers.py
More file actions
196 lines (172 loc) · 6.57 KB
/
Copy pathedit_vg_headers.py
File metadata and controls
196 lines (172 loc) · 6.57 KB
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
# Copyright (c) 2025 Facenapalm
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
Process headers (labels & descriptions) of the video game items: add mul labels,
remove redundant labels, fill in missing descriptions.
Usage:
python edit_vg_headers.py
"""
import pywikibot
from pywikibot import pagegenerators as pg
from common.basis import BaseWikidataBot
from common.data import vg_descriptions_data
def has_early_access_qualifier(claim):
if 'P3831' not in claim.qualifiers:
return False
for qualifier in claim.qualifiers['P3831']:
try:
if qualifier.getTarget().getID() == 'Q17042291':
return True
except:
pass
return False
def get_release_year(item):
result = None
if 'P577' not in item.claims:
return result
for date in item.claims['P577']:
if date.rank == 'deprecated':
continue
if has_early_access_qualifier(date):
continue
try:
year = date.getTarget().year
except:
continue
if result is None:
result = year
elif year < result:
result = year
return result
class HeaderMaintainerBot(BaseWikidataBot):
summary = 'maintain headers: set mul label, clear duplicating labels, add missing descriptions'
query = """
SELECT ?item WHERE {
?item wdt:P31 wd:Q7889 .
MINUS {
?item rdfs:label ?label .
FILTER( LANG(?label) = "mul" )
}
}
"""
supported_instances = {
'Q7397', # program
'Q166142', # application
'Q620615', # mobile app
'Q1121542', # mobile game
'Q56273712', # source-available software
'Q1395577', # fangame
'Q2568454', # reissue
'Q178285', # freeware
'Q116634', # online game
'Q7889', # video game
'Q848991', # browser game
'Q865493', # video game mod
'Q1755420', # game demo
'Q4393107', # video game remake
'Q61475894', # cancelled/unreleased video game
'Q65963104', # video game remaster
'Q21125433', # free or open-source video game
'Q60997816', # video game edition
'Q61456428', # total conversion mod
'Q64170203', # video game project
'Q64170508', # unfinished or abandoned video game project
'Q111223304', # video game reboot
'Q112144412', # esports discipline
}
def check_instance_of(self, item):
"""
Check if the item is an instance of video game.
If not, throw RuntimeError.
"""
if 'P31' not in item.claims:
raise RuntimeError('instance of is not set')
for claim in item.claims['P31']:
instance = claim.getTarget()
if instance is None:
continue
if instance.getID() not in self.supported_instances:
raise RuntimeError(f'unsupported instance of (`{instance.getID()}`)')
def process_labels(self, item):
try:
labels = item.labels
result = {}
# set up mul label
if 'mul' not in labels:
if 'en' not in labels:
raise RuntimeError('neither mul or en label set')
mul_label = labels['en']
for lang in labels:
if labels[lang] != mul_label:
raise RuntimeError(f'mismatch between en and {lang} label')
result['mul'] = mul_label
else:
mul_label = labels['mul']
# clear up duplicating labels
for lang in labels:
if lang in { 'mul', 'en' }:
continue
if labels[lang] == mul_label:
result[lang] = ''
return result
except RuntimeError as error:
print(f'{item.title()}: {error}')
return {}
def process_descriptions(self, item):
try:
year = get_release_year(item)
if year is None:
raise RuntimeError('release year is not specified')
descriptions = item.descriptions
result = {}
for lang, short_description, full_description in vg_descriptions_data:
if lang not in descriptions or descriptions[lang] == short_description:
result[lang] = full_description.format(year)
return result
except RuntimeError as error:
print(f'{item.title()}: {error}')
return {}
def process_item(self, item):
labels = self.process_labels(item)
descriptions = self.process_descriptions(item)
try:
item.editEntity(
{ 'labels': labels, 'descriptions': descriptions },
summary=self.summary
)
return bool(labels) or bool(descriptions)
except pywikibot.exceptions.OtherPageSaveError:
# wikibase-validator-label-with-description-conflict
# Let's try to only edit labels
item.editEntity(
{ 'labels': labels },
summary=self.summary
)
return bool(labels)
def run(self):
for item in pg.WikidataSPARQLPageGenerator(self.query, site=self.repo):
try:
self.check_instance_of(item)
if self.process_item(item):
print(f'{item.title()} processed')
except (RuntimeError, pywikibot.exceptions.OtherPageSaveError) as error:
print(f'{item.title()}: {error}')
if __name__ == '__main__':
HeaderMaintainerBot().run()