-
Notifications
You must be signed in to change notification settings - Fork 8
/
commons_check_id.py
169 lines (152 loc) · 5.83 KB
/
commons_check_id.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/python
# -*- coding: utf-8 -*-
# Migrate data from Commons to Wikidata
# Started 12 May 2018 by Mike Peel
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 *
maxnum = 100000
nummodified = 0
commons = pywikibot.Site('commons', 'commons')
repo = commons.data_repository() # this is a DataSite object
#category = 'Category:Cultural heritage monuments in Austria with known IDs'#'Category:Listed buildings in Wales with known IDs'#
#templates = ['Denkmalgeschütztes Objekt Österreich','denkmalgeschütztes Objekt Österreich', 'doo', 'Doo']#['Listed building Wales', 'listed building Wales']
#properties = ['P2951']#
category = 'Category:Listed buildings in England with known IDs'
templates = ['Listed building England', 'listed building England']
properties = ['P1216', 'P1216']
shortname = 'NHLE'
# category = 'Category:Rijksmonumenten with known IDs'
# templates = ['Rijksmonument','rijksmonument',]
# properties = ['P359']
# shortname = 'Rijksmonument ID'
# category = 'Category:National Register of Historic Places with known IDs'
# templates = ['NRHP']
# properties = ['P649']
# shortname = 'NRHP ID'
# category = 'Category:National Register of Historic Places with known IDs'
# templates = ['Zabytek nieruchomy', 'zabytek nieruchomy']
# properties = ['P649']
# shortname = 'NRHP ID'
# category = 'Category:Mérimée ID without linked Wikidata'#'Category:Base Mérimée'
# templates = ['Mérimée', 'Merimee','mérimée', 'merimee']
# properties = ['P380']
# shortname = 'Mérimée ID'
# category = 'Category:Protected areas with known WDPA-ID'#'Category:Base Mérimée'
# templates = ['WDPA']
# properties = ['P809']
# shortname = 'WDPA ID'
def checkid(targetcat):
print(targetcat)
try:
wd_item = pywikibot.ItemPage.fromPage(targetcat)
except:
print("No Wikidata sitelink found")
target_text = targetcat.get()
id_val = 0
for i in range(0,len(templates)):
# try:
# value = (target_text.split("{{"+templates[i]+"|"))[1].split("|")[0]
# if value and id_val == 0:
# id_val = value
# except:
# null = 1
# print '1'
# Special for Mirimee
try:
value = (target_text.split("{{"+templates[i]+"|"))[1].split("}}")[0]
print(value)
values = (value.split("|"))
if "type" not in values[0]:
value = values[0]
if "type" not in values[1]:
value = values[1]
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]
if value and id_val == 0:
id_val = value
except:
null = 2
try:
value = (target_text.split("{{"+templates[i]+"|1="))[1].split("}}")[0]
if value and id_val == 0:
id_val = value
except:
null = 3
try:
value = (target_text.split("{{"+templates[i]+"|"))[1].split("}}")[0]
if value and id_val == 0:
id_val = value
except:
null = 1
print(id_val)
if id_val != 0:
try:
query = 'SELECT ?item WHERE { ?item wdt:'+str(properties[0])+' ?id . FILTER (?id = "'+str(id_val)+'") . } LIMIT 10'
generator = pagegenerators.WikidataSPARQLPageGenerator(query, site=repo)
except:
print('Unable to run the query! Skipping this one.')
time.sleep(3)
return 0
count = 0
for testpage in generator:
page = testpage
count+=1
print(count)
if count == 1:
try:
item_dict = page.get()
qid = page.title()
except:
print('Huh - no page found')
try:
sitelink = get_sitelink_title(item_dict['sitelinks']['commonswiki'])
except:
# No existing sitelink found, add the new one
data = {'sitelinks': [{'site': 'commonswiki', 'title': targetcat.title()}]}
try:
print("\n\n")
print(qid)
print(id_val)
print(item_dict['labels'])
print(data)
# text = raw_input("Save? ")
# if text == 'y':
page.editEntity(data, summary=u'Add commons sitelink based on '+shortname+'')
return 1
# else:
# return 0
except:
print('Edit failed')
return 0
return 0
return 0
# Start the category walker
# cat = pywikibot.Category(commons,category)
# nummodified += checkid(cat)
# targetcats = pagegenerators.SubCategoriesPageGenerator(cat, recurse=False);
template = pywikibot.Page(commons, 'Template:'+templates[0])
targetcats = template.embeddedin(namespaces='14')
i = 0
for targetcat in targetcats:
# print targetcat
# print "\n" + targetcat.title()
# print target.text
nummodified += checkid(targetcat)
print(str(nummodified) + "/" + str(i))
i += 1
if nummodified >= maxnum:
print('Reached the maximum of ' + str(maxnum) + ' entries modified, quitting!')
exit()
print('Done! Edited ' + str(nummodified) + ' entries')
# EOF