-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathenwiki_commons.py
165 lines (145 loc) · 4.77 KB
/
enwiki_commons.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
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
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')
debug = 1
trip = 1
templates = ['Commons','Common','Wikicommons','Wikicommon']
catredirect_templates = ["category redirect", "Category redirect", "seecat", "Seecat", "see cat", "See cat", "categoryredirect", "Categoryredirect", "catredirect", "Catredirect", "cat redirect", "Cat redirect", "catredir", "Catredir", "redirect category", "Redirect category", "cat-red", "Cat-red", "redirect cat", "Redirect cat", "category Redirect", "Category Redirect", "cat-redirect", "Cat-redirect"]
template = pywikibot.Page(enwp, 'Template:'+templates[0])
targetcats = template.embeddedin(namespaces='14')
# targetcats = template.embeddedin(namespaces='0')
for page in targetcats:
# Optional skip-ahead to resume broken runs
if trip == 0:
if "Zizia" in page.title():
trip = 1
else:
print(page.title())
continue
print("\nhttps://en.wikipedia.org/wiki/" + page.title().replace(" ",'_'))
# Get the candidate commonscat links
try:
target_text = page.get()
except:
continue
id_val = 0
abort = 0
commonscat_string = ""
for i in range(0,len(templates)):
if "{{"+templates[i]+"}}" in target_text:
id_val = ''
id_template = templates[i]
id_string = "{{"+templates[i]+"}}"
try:
print(templates[i])
value = (target_text.split("{{"+templates[i]))[1].split("}}")[0]
if value and id_val == 0:
id_val = value
id_template = templates[i]
id_string = "{{"+templates[i]+id_val+"}}"
except:
null = 1
try:
template = templates[i][0].lower() + templates[i][1:]
# print(template)
value = (target_text.split("{{"+template))[1].split("}}")[0]
if value and id_val == 0:
id_val = value
id_template = template
id_string = "{{"+template+id_val+"}}"
except:
null = 1
if id_val == 0:
# We didn't find the commons category link, skip this one.
# input('Check?')
continue
print(id_string)
print(id_template)
print(id_val)
id_vals = id_val.split('|')
print(id_vals)
# Do some tidying of the link
bad_id = np.zeros(len(id_vals))
for i in range(0,len(id_vals)):
if 'position' in id_vals[i] or 'bullet' in id_vals[i] or 'nowrap' in id_vals[i] or 'lcfirst' in id_vals[i] or 'lcf' in id_vals[i] or 'align' in id_vals[i] or 'width' in id_vals[i] or id_vals[i] == '' or id_vals[i] == ' ':
bad_id[i] = 1
id_vals = np.asarray(id_vals,dtype="str")
# print(id_vals)
# print(bad_id)
id_vals = id_vals[bad_id == 0]
num_vals = len(id_vals)
# print(id_vals)
# exit()
# Get the Wikidata item
try:
wd_item = pywikibot.ItemPage.fromPage(page)
item_dict = wd_item.get()
qid = wd_item.title()
print(qid)
except:
# If that didn't work, go no further
print(page.title() + ' - no page found')
wd_item = 0
item_dict = 0
qid = 0
sitelink_check = 0
# continue
# If we have a P910 value, switch to using that Wikidata item
if qid != 0:
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()
qid = wd_item.title()
print(wd_item.title())
except:
null = 0
# Double-check that there is a sitelink on Wikidata
try:
sitelink = get_sitelink_title(item_dict['sitelinks']['commonswiki'])
sitelink_check = 1
except:
sitelink = ''
sitelink_check = 0
print("sitelink: " + str(sitelink))
for val in id_vals:
if 'Category' in val:
id_string_new = id_string
id_string_new = id_string_new.replace(':Category:','')
id_string_new = id_string_new.replace('Category:','')
id_string_new = id_string_new.replace(id_template, 'Commons category')
print(id_string)
print(id_string_new)
target_text = target_text.replace(id_string, id_string_new)
if target_text != page.text:
test = input('Found a link to a Commons category - change it?')
if test == 'y':
page.text = target_text
page.save('Change {{Commons}} to {{Commons category}}',minor=False)
nummodified += 1
# If we have a sitelink and a blank commons template, use commonscat
# if sitelink != '' and id_vals.size == 0:
# print(id_string)
# target_text = target_text.replace(id_string,'{{Commons category}}')
# if target_text != page.text:
# test = input('Replace with sitelink?')
# if test == 'y':
# page.text = target_text
# page.save('Change {{Commons}} to {{Commons category}}',minor=False)
# nummodified += 1
# input('Next?')
print('Done! Edited ' + str(nummodified) + ' entries')
# EOF