-
Notifications
You must be signed in to change notification settings - Fork 8
/
enwp_commonscat_import.py
245 lines (225 loc) · 7.42 KB
/
enwp_commonscat_import.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
# !/usr/bin/python
# -*- coding: utf-8 -*-
# Check for consistency in commons category usage
# Mike Peel 01-Mar-2018 v1 - start
from __future__ import unicode_literals
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
import urllib
from pibot_functions import *
import time
maxnum = 10000
nummodified = 0
wikidata_site = pywikibot.Site("wikidata", "wikidata")
repo = wikidata_site.data_repository() # this is a DataSite object
commons = pywikibot.Site('commons', 'commons')
langcode = 'en'
enwp = pywikibot.Site(langcode, 'wikipedia')
debug = 0
trip = 1
templates = ['commonscat', 'Commonscat', 'commonscategory', 'Commonscategory', 'commons category', 'Commons category', 'commons cat', 'Commons cat', 'commons_category', 'Commons_category', 'commons_cat', 'Commons_cat', 'Commons category-inline', 'commons category-inline', 'Commons cat-inline', 'commons cat-inline', 'commonscat-inline', 'Commonscat-inline', 'Commons category inline', 'commons category inline', 'commons-cat-inline', 'Commons-cat-inline', 'Commons cat inline', 'commons cat inline', 'commonscat inline', 'Commonscat inline', 'Commons Category', 'commons Category','commonscatinline', 'Commonscatinline','Autres projets','Ccat','ccat','Cc','cc']
targetcats = ['Category:Commons category link is the pagename', 'Category:Commons category link is defined as the pagename']
include_cat = True
for targetcat in targetcats:
for category in np.arange(0,2):
cat = pywikibot.Category(enwp, targetcat)
if category == 0:
pages = pagegenerators.SubCategoriesPageGenerator(cat, recurse=False);
else:
pages = pagegenerators.CategorizedPageGenerator(cat, recurse=False);
for page in pages:
if trip == 0:
if "Varanasi" in page.title():
trip = 1
else:
print(page.title())
continue
try:
wd_item = pywikibot.ItemPage.fromPage(page)
item_dict = wd_item.get()
qid = wd_item.title()
except:
print('Huh - no page found')
continue
print("\n" + qid)
print('https://'+langcode+'.wikipedia.org/wiki/'+page.title().replace(' ','_'))
# Get the candidate page
target_text = page.get()
id_val = 0
abort = 0
null = 0
for i in range(0,len(templates)):
if id_val == 0:
try:
value = (target_text.split("{{"+templates[i]+"|"))[1].split("}}")[0].strip()
values = (value.split("|"))
if 'position' in values[0]:
value = values[1]
else:
value = values[0]
print('0')
null = 0
print(value)
if value and id_val == 0:
id_val = value
except:
null = 1
try:
value = (target_text.split("{{"+templates[i]+" |1="))[1].split("}}")[0].strip()
print('1')
null = 1
print(value)
if value and id_val == 0:
id_val = value
elif id_val != 0:
print('Found multiple IDs')
except:
null = 2
try:
value = (target_text.split("{{"+templates[i])[1]).strip()
# print(value)
value = value.split("}}")[0].strip()
print(value)
values = (value.split("|"))
print(values)
if 'position' in values[1] or 'wiktionary' in values[1]:
value = values[2]
else:
value = values[1]
null = 2
print('2')
print(value)
if value and id_val == 0:
id_val = value
except:
null = 3
print(null)
# exit()
if id_val == 0:
try:
p373 = item_dict['claims']['P373']
print('P373 exists, following using.')
for clm in p373:
id_val = clm.getTarget()
except:
id_val = page.title().replace('Category:','')
if id_val == 0:
id_val = page.title().replace('Category:','')
# Do some tidying of the link
if id_val != 0:
if "|" in id_val:
if 'position' in id_val.split("|")[0] or 'bullet' in id_val.split("|")[0]:
if 'position' in id_val.split("|")[1] or 'bullet' in id_val.split("|")[1]:
id_val = id_val.split("|")[2]
else:
id_val = id_val.split("|")[1]
else:
id_val = id_val.split("|")[0]
try:
id_val = id_val.strip()
except:
null = 1
if "{{PAGENAME" in id_val:
id_val = page.title().replace('Category:','')
try:
id_val = id_val.strip()
except:
null = 1
try:
id_val = id_val.replace('Commons=', '').replace('Commons =','').replace('commons=', '').replace('commons =','').replace('Category:','').replace('category:','')
if '|' in id_val:
id_val = id_val.split('|')[0]
except:
null = 1
try:
id_val = id_val.strip()
except:
null = 1
# Check for bad characters
if "{" in id_val or "<" in id_val or "]" in id_val or "[" in id_val:
continue
else:
id_val = page.title()
id_val = id_val.replace('::',':')
print(id_val)
# If we have a P910 value, switch to using that item
if include_cat:
have_followed_p910 = False
try:
existing_id = item_dict['claims']['P910']
print('P910 exists, following that.')
for clm2 in existing_id:
wd_item = clm2.getTarget()
item_dict = wd_item.get()
print(wd_item.title())
have_followed_p910 = True
except:
null = 0
# Double-check that we don't already have a sitelink
try:
sitelink = get_sitelink_title(item_dict['sitelinks']['commonswiki'])
sitelink_check = 1
except:
sitelink_check = 0
# Only attempt to do this if there is only one value for P373 and no existing sitelink
if id_val != 0 and sitelink_check == 0:
if include_cat:
commonscat = u"Category:" + id_val
else:
commonscat = id_val
commonscat = commonscat.replace('::',':')
if commonscat[0] == ':':
commonscat = commonscat[1:]
# The commons category must already exist
test = 'n'
check = 0
try:
sitelink_page = pywikibot.Page(commons, commonscat)
check = 1
except:
print('Found a bad sitelink')
if check == 1:
# Check the category to see if it already has a Wikidata item
commonscat_sitelink_exists = 0
try:
commonscat_page = pywikibot.Page(commons, commonscat)
commonscat_item = pywikibot.ItemPage.fromPage(commonscat_page)
commonscat_item_dict = commonscat_item.get()
commonscat_sitelink_exists = 1
except:
try:
text = commonscat_page.get()
except:
print('Commons category does not exist - fix that?')
# last_check = check_if_category_has_contents(id_val,site=commons)
# if last_check:
continue
if '{{Disambig' not in text and '{{disambig' not in text and '{{Category redirect' not in text and '{{category redirect' not in text:
# That didn't work, add it to the Wikidata entry
data = {'sitelinks': [{'site': 'commonswiki', 'title': commonscat}]}
print('http://www.wikidata.org/wiki/'+qid)
print('http://commons.wikimedia.org/wiki/'+commonscat.replace(' ','_'))
if debug:
text = input("Save? ")
else:
text = 'y'
if text != 'n':
try:
wd_item.editEntity(data, summary=u'Add commons sitelink')
except:
time.sleep(1)
wd_item.editEntity(data, summary=u'Add commons sitelink')
nummodified += 1
try:
page.touch()
except:
continue
print(nummodified)
if nummodified >= maxnum:
print('Reached the maximum of ' + str(maxnum) + ' entries modified, quitting!')
exit()
print('Done! Edited ' + str(nummodified) + ' entries')
# EOF