-
Notifications
You must be signed in to change notification settings - Fork 8
/
commonscat_move_from_P1754.py
169 lines (151 loc) · 5.87 KB
/
commonscat_move_from_P1754.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Move commons category sitelinks to category items where needed
# Mike Peel 10-Jun-2018 v1
# Mike Peel 13-Jul-2019 v2 - use P1754/P1753 rather than P910/P301
from __future__ import unicode_literals
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
import urllib
import time
from pibot_functions import *
maxnum = 1000
nummodified = 0
stepsize = 10000
maximum = 2000000
numsteps = int(maximum / stepsize)
wikidata_site = pywikibot.Site("wikidata", "wikidata")
repo = wikidata_site.data_repository() # this is a DataSite object
commons = pywikibot.Site('commons', 'commons')
debug = 1
# query = 'SELECT ?item ?categoryitem ?commonscategory WHERE { ?item wdt:P1754 ?categoryitem . ?commonscategory schema:about ?item . ?commonscategory schema:isPartOf <https://commons.wikimedia.org/> . FILTER REGEX(STR(?commonscategory), "https://commons.wikimedia.org/wiki/Category:") . }'
# if debug:
# query = query + " LIMIT 1000"
for i in range(0,numsteps):
print('Starting at ' + str(i*stepsize))
query = 'SELECT ?item ?categoryitem ?commonscategory\n'\
'WITH { \n'\
' SELECT ?item ?categoryitem WHERE {\n'\
' ?item wdt:P1754 ?categoryitem . \n'\
' } LIMIT '+str(stepsize)+' OFFSET '+str(i*stepsize)+'\n'\
'} AS %items\n'\
'WHERE {\n'\
' INCLUDE %items .\n'\
' ?commonscategory schema:about ?item . \n'\
' ?commonscategory schema:isPartOf <https://commons.wikimedia.org/> . \n'\
' FILTER (STRSTARTS(STR(?commonscategory), "https://commons.wikimedia.org/wiki/Category:")) . \n'\
'}'
print(query)
generator = pagegenerators.WikidataSPARQLPageGenerator(query, site=wikidata_site)
bad_commonscat_count = 0
bad_sitelink_count = 0
interwiki_conflicts = []
for page in generator:
# Get the page
try:
item_dict = page.get()
except:
continue
qid = page.title()
print("\n" + qid)
try:
sitelink = get_sitelink_title(item_dict['sitelinks']['commonswiki'])
print(sitelink)
except:
print('No sitelink found in main item! Skipping!')
continue
# Get the value for P1754
try:
P1754 = item_dict['claims']['P1754']
except:
print('No P1754 value found!')
continue
P1754_check = 0
for clm in P1754:
P1754_check += 1
# Only attempt to do this if there is only one value for P1754
if P1754_check != 1:
print('More than one P1754 value found! Skipping...')
continue
for clm in P1754:
try:
val = clm.getTarget()
wd_id = val.title()
target_dict = val.get()
except:
print('Unable to get target!')
continue
print(wd_id)
try:
p31 = target_dict['claims']['P31']
print(p31)
except:
print('No P31 in target - skipping!')
continue
test_p31 = 0
for clm in p31:
if 'Q4167836' in clm.getTarget().title():
test_p31 = 1
if test_p31 != 1:
print('Target is not a category item - skipping!')
continue
try:
sitelink = get_sitelink_title(target_dict['sitelinks']['commonswiki'])
print(sitelink)
print('We have a sitelink in the target! Skipping...')
continue
except:
null = 1
# Do we have the correct value for P1753?
try:
P1753 = target_dict['claims']['P1753']
except:
print('No P1753 value found!')
continue
P1753_check = 0
retarget = 0
for clm2 in P1753:
P1753_check += 1
# Only attempt to do this if there is only one value for P1754
if P1753_check != 1:
print('More than one P1753 value found! Skipping...')
for clm2 in P1753:
retarget = clm2.getTarget().title()
print(retarget)
if retarget != qid:
print("P1754 and P1753 don't match! Skipping!")
continue
# Remove it from the current entry and add it to the new entry
data = {'sitelinks': [{'site': 'commonswiki', 'title': sitelink}]}
try:
print(data)
text = 'y'
# text = input("Save? ")
if text == 'y':
print('Saving!')
page.removeSitelink(site='commonswiki', summary=u'Moving commons category sitelink to category item ([[' + str(wd_id) + ']])')
time.sleep(5)
val.editEntity(data, summary=u'Moving commons category sitelink from list item ([[' + str(qid) + ']])')
nummodified += 1
except:
print('Edit failed!')
# Bonus: if we don't have an English language label, add it.
try:
label = val.labels['en']
print(label)
except:
text = 'y'
# text = input("Save? ")
if text == 'y':
try:
val.editLabels(labels={'en': sitelink}, summary=u'Add en label to match Commons sitelink')
except:
print('Unable to save label edit on Wikidata!')
if nummodified >= maxnum:
print('Reached the maximum of ' + str(maxnum) + ' entries modified, quitting!')
exit()
print('Done! Edited ' + str(nummodified) + ' entries')
# EOF