-
Notifications
You must be signed in to change notification settings - Fork 8
/
enwp_find_wikidata.py
361 lines (329 loc) · 11.1 KB
/
enwp_find_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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Add enwp sitelinks based on commons categories
# Mike Peel 11-Jun-2020 v1 - start
from __future__ import unicode_literals
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
from pywikibot.data import api
import urllib
from pibot_functions import *
from database_login import *
from wir_newpages import *
import os
wikidata_site = pywikibot.Site("wikidata", "wikidata")
repo = wikidata_site.data_repository() # this is a DataSite object
commons = pywikibot.Site('commons', 'commons')
enwp = pywikibot.Site('en', 'wikipedia')
enwp_site = 'enwiki'
prefix = 'en'
# enwp = pywikibot.Site('simple', 'wikipedia')
# enwp_site = 'simplewiki'
# prefix = 'simple'
# enwp = pywikibot.Site('pt', 'wikipedia')
# enwp_site = 'ptwiki'
# prefix = 'pt'
# enwp = pywikibot.Site('de', 'wikipedia')
# enwp_site = 'dewiki'
# prefix = 'de'
enwp = pywikibot.Site('dag', 'wikipedia')
enwp_site = 'dagwiki'
prefix = 'dag'
doing_cats = False
templates_to_skip = ['Q4847311','Q6687153','Q21528265','Q26004972','Q6838010','Q14446424','Q7926719','Q5849910','Q6535522','Q12857463','Q14397354','Q18198962','Q13107809','Q6916118','Q15630429','Q6868608','Q6868546','Q5931187','Q26021926','Q21684530','Q20310993','Q25970270','Q57620750','Q4844001','Q97159332','Q17586305','Q17586361','Q17588240','Q13420881','Q17589095','Q17586294','Q13421187','Q97709865','Q17586502','Q5828850','Q15631954','Q5902043', 'Q14456068','Q105097863','Q11032822']
def newitem(category, enwp, items,commonscat_has_item=False):
new_item = pywikibot.ItemPage(repo)
new_item.editLabels(labels={"en":enwp.title()}, summary="Creating item")
candidate_item = pywikibot.ItemPage(repo, new_item.getID())
print(candidate_item)
if commonscat_has_item:
data = {'sitelinks': [{'site': 'commonswiki', 'title': category.title()}, {'site': enwp_site, 'title': enwp.title()}]}
candidate_item.editEntity(data, summary=u'Add commons and '+enwp_site+' sitelink')
else:
data = {'sitelinks': [{'site': enwp_site, 'title': enwp.title()}]}
candidate_item.editEntity(data, summary=u'Added [['+prefix+':'+enwp.title()+']]')
for item in items:
claim = pywikibot.Claim(repo, item[0])
if item[0] == 'P569' or item[0] == 'P570':
claim.setTarget(item[1])
else:
claim.setTarget(pywikibot.ItemPage(repo, item[1]))
try:
candidate_item.addClaim(claim, summary=u'Setting '+item[0]+' value')
claim.addSources([statedin, retrieved], summary=u'Add source.')
except:
print("That didn't work")
return
def parsequarry(quarryfile):
with open(quarryfile, mode='r') as infile:
targets = infile.read()
targets = targets.splitlines()
targets = targets[1:]
return targets
def parseredirects(quarryfile):
with open(quarryfile, mode='r') as infile:
targets = infile.read()
targets = targets.splitlines()
return targets
def search_entities(site, itemtitle,lang='en'):
params = { 'action' :'wbsearchentities',
'format' : 'json',
'language' : lang,
'type' : 'item',
'search': itemtitle}
request = api.Request(site=site, parameters=params)
return request.submit()
maxnum = 100000
nummodified = 0
debug = 1
trip = 1
newitems = 0
targetcats = ['Category:Articles_without_Wikidata_item']
# Also see https://www.wikidata.org/wiki/Wikidata:Metrics
# targetcats = ['Category:Short description with empty Wikidata description']
lang = 'en'
skipping_templates = set()
for item in templates_to_skip:
print(item)
template = enwp.page_from_repository(item)
if template is None:
continue
skipping_templates.add(template)
# also add redirect templates
skipping_templates.update(template.getReferences(follow_redirects=False, with_template_inclusion=False, filter_redirects=True, namespaces=enwp.namespaces.TEMPLATE))
print(template.title())
# for categories in range(0,2):
for targetcat in targetcats:
cat = pywikibot.Category(enwp, targetcat)
# pages = pagegenerators.CategorizedPageGenerator(cat, recurse=False);
# pages = enwp.querypage('UnconnectedPages')
# for page in pages:
# pages = parsequarry('quarry-51950-enwp-categories-without-wikidata-run526620.csv')
if doing_cats:
# redirects = parseredirects(prefix+'wp_category_redirects.csv')
redirects = []
pages = parsequarry(prefix+'wp_categories.csv')
else:
pages = parsequarry(prefix+'wp_articles.csv')
pages.sort()
for pagename in pages:
pagename = str(pagename[2:-1]).encode('latin1').decode('unicode-escape').encode('latin1').decode('utf-8')
if pagename[0] == '"' and pagename[-1] == '"':
pagename = pagename[1:-1]
if doing_cats:
if prefix == 'pt':
pagename = 'Categoria:'+pagename
if 'Categoria:!' in pagename:
continue
elif prefix == 'de':
pagename = 'Kategorie:'+pagename
else:
pagename = 'Category:'+pagename
if pagename.replace('_',' ').strip() in redirects:
print(pagename)
print('Redirected')
continue
try:
page = pywikibot.Category(enwp, pagename)
except:
continue
else:
try:
page = pywikibot.Page(enwp, pagename)
except:
continue
try:
text = page.get()
except:
continue
if 'REDIRECT' in text:
continue
if 'redirect' in text:
continue
# Optional skip-ahead to resume broken runs
if trip == 0:
if "Categoria:Azteca" in page.title():
trip = 1
else:
print(page.title())
continue
# Cut-off at a maximum number of edits
print("")
print(nummodified)
if nummodified >= maxnum:
print('Reached the maximum of ' + str(maxnum) + ' entries modified, quitting!')
exit()
print("\n" + "http://"+prefix+".wikipedia.org/wiki/"+page.title().replace(' ','_'))
if 'Articles for deletion' in page.title():
continue
if page.isRedirectPage():
continue
temp_trip = 0
for template, _ in page.templatesWithParams():
if template in skipping_templates:
temp_trip = template.title()
if temp_trip != 0:
print('Page contains ' + str(temp_trip) + ', skipping')
continue
# if not pageIsBiography(page=page, lang=lang):
# print('Page is not a biography')
# if authorIsNewbie(page=page, lang=lang):
# print("Newbie author, checking quality...")
# if pageIsRubbish(page=page, lang=lang) or \
# (not pageCategories(page=page, lang=lang)) or \
# (not pageReferences(page=page, lang=lang)) or \
# (not len(list(page.getReferences(namespaces=[0])))):
# print("Page didnt pass minimum quality")
# pagebirthyear = calculateBirthYear(page=page, lang=lang)
# pagebirthyear = pagebirthyear and int(pagebirthyear.split('-')[0]) or ''
# if not pagebirthyear:
# print("Page doesnt have birthdate")
# See if we have a Wikidata item already
try:
wd_item = pywikibot.ItemPage.fromPage(page)
item_dict = wd_item.get()
qid = wd_item.title()
print("Has a sitelink already - " + qid)
continue
except:
# If that didn't work, go no further
print(page.title() + ' - no item found')
wd_item = 0
item_dict = 0
qid = 0
sitelink_check = 0
# continue
# If we're here, then we don't have one, see if we can add it through the commons category
try:
searchtag = page.title()
try:
searchtag = searchtag.split('(')[0].strip()
except:
null = 0
wikidataEntries = search_entities(repo, searchtag,lang=prefix)
# print(wikidataEntries)
data = {'sitelinks': [{'site': enwp_site, 'title': page.title()}]}
# print(wikidataEntries['searchinfo'])
done = 0
if wikidataEntries['search'] != []:
results = wikidataEntries['search']
# prettyPrint(results)
numresults = len(results)
if numresults > 5:
print('More than 5 candidates, bot would skip')
for i in range(0,numresults):
if done != 0:
continue
targetpage = pywikibot.ItemPage(wikidata_site, results[i]['id'])
try:
item_dict = targetpage.get()
except:
continue
# print(item_dict)
sitelink = ''
try:
sitelink = get_sitelink_title(item_dict['sitelinks'][enwp_site])
except:
null = 0
if sitelink == '':
print('http://www.wikidata.org/wiki/'+results[i]['id'])
if prefix != 'en':
try:
print(item_dict['labels']['en'])
except:
print('')
try:
print(item_dict['descriptions']['en'])
except:
print('')
try:
print(item_dict['labels'][prefix])
except:
print('')
try:
print(item_dict['descriptions'][prefix])
except:
print('')
print('http://'+prefix+'.wikipedia.org/wiki/' + page.title().replace(' ','_'))
text = input("Save? ")
if text != 'n':
targetpage.editEntity(data, summary=u'Added [['+prefix+':'+page.title()+']]')
done = 1
if prefix != 'en' and prefix != 'simple' and not done and ':' in pagename:
# Also try translating and doing a search in English
# command = 'trans '+prefix+': -brief "'+pagename.replace('"',"'").replace("_"," ")+'" -engine bing'
# print(command)
# stream = os.popen(command)
# output = stream.read().strip()
# output = output.replace(': ', ':')
# print(output)
output = pagename.replace('Kategorie:','Category:')
output = pagename.replace('Categoria:','Category:')
wikidataEntries = search_entities(repo, output,lang='en')
# print(wikidataEntries)
data = {'sitelinks': [{'site': enwp_site, 'title': page.title()}]}
# print(wikidataEntries['searchinfo'])
done = 0
if wikidataEntries['search'] != []:
results = wikidataEntries['search']
# prettyPrint(results)
numresults = len(results)
if numresults > 5:
print('More than 5 candidates, bot would skip')
for i in range(0,numresults):
if done != 0:
continue
targetpage = pywikibot.ItemPage(wikidata_site, results[i]['id'])
try:
item_dict = targetpage.get()
except:
continue
# print(item_dict)
sitelink = ''
try:
sitelink = get_sitelink_title(item_dict['sitelinks'][enwp_site])
except:
null = 0
if sitelink == '':
print('http://www.wikidata.org/wiki/'+results[i]['id'])
if prefix != 'en':
try:
print(item_dict['labels']['en'])
except:
print('')
try:
print(item_dict['descriptions']['en'])
except:
print('')
try:
print(item_dict['labels'][prefix])
except:
print('')
try:
print(item_dict['descriptions'][prefix])
except:
print('')
print('http://'+prefix+'.wikipedia.org/wiki/' + page.title().replace(' ','_'))
text = input("Save? ")
if text != 'n':
targetpage.editEntity(data, summary=u'Added [['+prefix+':'+page.title()+']]')
done = 1
except:
continue
if done == 0 and newitems == 1:
text = input('Create a new item?')
if text != 'n':
# Start assembling the Wikdata entry
items = []
new_item = pywikibot.ItemPage(repo)
new_item.editLabels(labels={"en":page.title()}, summary="Creating item")
itemfound = pywikibot.ItemPage(repo, new_item.getID())
data = {'sitelinks': [{'site': enwp_site, 'title': page.title()}]}
itemfound.editEntity(data, summary=u'Added [['+prefix+':'+page.title()+']]')
text = input('Is it a bio?')
if text != 'n':
addBiographyClaims(repo=repo, wikisite=enwp, item=itemfound, page=page, lang=lang)
# EOF