-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.py
514 lines (420 loc) · 17.4 KB
/
common.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# coding: utf-8
from importlib import reload
from time import sleep
import datetime
import psycopg2 as pg
import psycopg2.extensions
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
import pywikibot as wp
import signal
while True:
try:
from . import const, settings
break
except ImportError:
wp.output("No config info available yet. Sleeping 2 seconds.")
sleep(2)
if settings.mb_user is None or settings.mb_password is None:
wp.output("No MusicBrainz login data, no redirects will be fixed")
editing = None
else:
from .musicbrainz_bot import editing
from sys import version_info
from time import sleep
from urllib.parse import urlparse
# Set up a signal handler to reload the settings on SIGHUP
def signal_handler(signal, frame):
wp.output("HUP received")
reload_settings()
raise SettingsReloadedException("Settings have been reloaded during HUP")
signal.signal(signal.SIGHUP, signal_handler)
WIKI_PREFIX = "/wiki/"
readonly_db = None
readwrite_db = None
class SkipPage(Exception):
def __init__(self, url):
self.url = url
class IsDisambigPage(SkipPage):
def __str__(self):
return "{url} is a disambiguation page".format(url=self.url)
class HasFragment(SkipPage):
def __str__(self):
return "{url} has a fragment".format(url=self.url)
class InstanceOfForbidden(SkipPage):
def __init__(self, url, item_id):
super().__init__(url)
self.item_id = item_id
def __str__(self):
return "{url} is an instance of {id}".format(url=self.url,
id=self.item_id)
class IsRedirectWithItemPage(SkipPage):
def __str__(self):
return "{url} is a redirect page, but is linked to a Wikidata item".format(url=self.url)
class IsRedirectPage(Exception):
def __init__(self, old, new):
self.old = old
self.new = new
def __str__(self):
return "%s is a redirect to %s" % (self.old, self.new)
class PageGone(Exception):
def __init__(self, pagename):
self.pagename = pagename
def __str__(self):
return "%s is no more" % (self.pagename)
class SettingsReloadedException(Exception):
"""Custom Exception class to signal that the settings have been reloaded
during SIGHUP processing."""
pass
def create_url_mbid_query(entitytype, linkids):
"""Creates a specific query for `entitytype` and `linkids` from
`const.GENERIC_URL_MBID_QUERY`.
"""
custom = const.QUERIES[entitytype]
if custom is not None:
return custom
return const.GENERIC_URL_MBID_QUERY.format(etype=entitytype,
wikipedia_linkid=linkids.wikipedia,
wikidata_linkid=linkids.wikidata)
def create_already_processed_query(entitytype):
"""Creates a specific query for `entitytype` from
`CONST.GENERIC_ALREADY_PROCESSED_QUERY`
"""
return const.GENERIC_ALREADY_PROCESSED_QUERY.format(etype=entitytype)
def create_done_func(entitytype):
"""Creates a specific function for `entitytype` from
`const.GENERIC_DONE_QUERY`.
"""
query = const.GENERIC_DONE_QUERY.format(etype=entitytype)
def func(mbid):
do_readwrite_query(query, {'mbid': mbid})
return func
def create_processed_table_query(entitytype):
"""Creates a specific query for `entitytype` from
`const.GENERIC_CREATE_PROCESSED_TABLE_QUERY`.
"""
return const.GENERIC_CREATE_PROCESSED_TABLE_QUERY.format(etype=entitytype)
def reload_settings():
wp.output("Old RO connection: {}".format(settings.readonly_connection_string))
wp.output("Old RW connection: {}".format(settings.readwrite_connection_string))
wp.output("Old mb_user {}".format(repr(settings.mb_user)))
reload(settings)
wp.output("New RO connection: {}".format(settings.readonly_connection_string))
wp.output("New RW connection: {}".format(settings.readwrite_connection_string))
wp.output("New mb_user {}".format(repr(settings.mb_user)))
def setup_db():
global readonly_db
if readonly_db is not None:
readonly_db.close()
readonly_db = pg.connect(settings.readonly_connection_string,
application_name="mb2wikidatabot_readonly")
readonly_db.autocommit = True
global readwrite_db
if readwrite_db is not None:
readwrite_db.close()
readwrite_db = pg.connect(settings.readwrite_connection_string,
application_name="mb2wikidatabot_readwrite")
readwrite_db.autocommit = True
def create_table(query):
cur = readwrite_db.cursor()
cur.execute(query)
readwrite_db.commit()
def do_readonly_query(query, limit):
"""Perform `query` against the read only database."""
cur = readonly_db.cursor()
cur.execute(query, (limit,))
return cur
def do_readwrite_query(query, vars=None):
"""Perform `query` against the read-write database."""
cur = readwrite_db.cursor()
cur.execute(query, vars)
return cur
def check_has_fragment(url):
"""Check if `url` contains a fragment
This is most often the case for discography pages where a single album is
only mentioned in a few paragraphs."""
parsed_url = urlparse(url)
if parsed_url.fragment:
raise HasFragment(url)
def check_url_needs_to_be_skipped(wikilink, page):
"""Check if `page` is a redirect or disambiguation page"""
full_url = page.full_url()
check_has_fragment(full_url)
if page.isRedirectPage():
try:
wp.ItemPage.fromPage(page)
except wp.exceptions.NoPageError:
# Page is a redirect without its own wikidata item -
# everything's OK, we can safely fix the redirect.
# Examples of this are
# https://en.wikipedia.org/w/index.php?title=Star_Wars_Episode_I:_The_Phantom_Menace_(soundtrack)&redirect=no
pass
else:
# Page is a redirect, but has its own wikidata item - it's better
# to not touch this.
# Examples of this are
# https://en.wikipedia.org/wiki/Hybrid_Theory_%28EP%29, which is a
# redirect on en.wp, but has its own article on lots of other
# wikipedias and therefore its own wikidata item.
raise IsRedirectWithItemPage(full_url)
page = page.getRedirectTarget()
full_url = page.full_url()
check_has_fragment(full_url)
raise IsRedirectPage(wikilink, full_url)
if page.isDisambig():
raise IsDisambigPage(full_url)
# page.isDisambig() is False for wikidata items, even if they're instances
# of a disambiguation page, so check for that and other forbidden instance
# of claims manually.
if isinstance(page, wp.ItemPage):
for key, claims in page.claims.items():
if key.lower() == const.PROPERTY_ID_INSTANCE_OF.lower():
for claim in claims:
item_id = claim.target.getID()
if item_id in const.SKIP_INSTANCE_OF_ITEMS:
raise InstanceOfForbidden(full_url, item_id)
def get_wikidata_itempage_from_wikilink(wikilink):
"""Given a link to a wikipedia page, retrieve its page on Wikidata"""
parsed_url = urlparse(wikilink)
if "wikipedia" in parsed_url.netloc:
pagename = parsed_url.path.replace(WIKI_PREFIX, "")
wikilanguage = parsed_url.netloc.split(".")[0]
wikisite = wp.Site(wikilanguage, "wikipedia")
enwikipage = wp.Page(wikisite, pagename)
check_url_needs_to_be_skipped(wikilink, enwikipage)
try:
wikidatapage = wp.ItemPage.fromPage(enwikipage)
except wp.exceptions.NoPageError:
wp.error("%s does not exist" % enwikipage)
return None
elif "wikidata" in parsed_url.netloc:
pagename = parsed_url.path.replace(WIKI_PREFIX, "")
wikidatapage = wp.ItemPage(const.WIKIDATA_DATASITE, pagename)
else:
raise ValueError("%s is not a link to a wikipedia page" % wikilink)
try:
wikidatapage.get(get_redirect=True)
except wp.exceptions.NoPageError:
wp.error("%s does not exist" % pagename)
raise PageGone(pagename)
check_url_needs_to_be_skipped(wikilink, wikidatapage)
return wikidatapage
class Bot(object):
redirect_edit_note = "%s is only a redirect to %s"
removed_edit_note = "%s no longer exists, marking as ended"
def __init__(self):
if not settings.mb_user or not settings.mb_password or not settings.mb_editor_id:
wp.output("MusicBrainz credentials are not configured, not enabling editing.")
self.client = None
else:
wp.output("MusicBrainz credentials are configured, enabling editing.")
self.client = editing.MusicBrainzClient(settings.mb_user,
settings.mb_password,
"https://musicbrainz.org",
settings.mb_editor_id)
self.update_rate_limits()
self._current_entity_type = None
self.linkids = None
self.property_id = None
@property
def current_entity_type(self):
return self._current_entity_type
@current_entity_type.setter
def current_entity_type(self, new_type):
self.donefunc = create_done_func(new_type)
self.linkids = const.LINK_IDS[new_type]
self.property_id = const.PROPERTY_IDS[new_type]
self._current_entity_type = new_type
@property
def can_edit(self):
if self.client is None:
return False
if not self.number_of_allowed_edits:
return False
if wp.config.simulate:
return False
return True
def update_rate_limits(self):
"""
Retrieve the current rate limit status from MusicBrainz.
"""
if self.client is None:
self.number_of_allowed_edits = 0
return
self.number_of_allowed_edits = self.client.edits_left()
# This includes those for today
if not self.number_of_allowed_edits:
wp.output("Reached the limit of open edits.")
def _performed_edit(self):
"""
Callback for decrementing the number of edits that can still be opened.
Prints an informational method when the limit is reached.
"""
self.number_of_allowed_edits -= 1
if not self.number_of_allowed_edits:
wp.output("Reached the limit of open edits, disabling editing")
def add_mbid_claim_to_item(self, item, mbid, entity_name):
"""
Adds a claim with pid `pid` with value `mbid` to `item`,
with qualifiers to indicate the MB source and name,
and call `donefunc` with `mbid` to signal the completion.
:type pid: str
:type mbid: str
:type entity_name: str
:type item: pywikibot.ItemPage
"""
claim = wp.Claim(const.WIKIDATA_DATASITE, self.property_id)
claim.setTarget(mbid)
wp.output(u"Adding property {pid}, value {mbid} to {title}".format
(pid=self.property_id, mbid=mbid, title=item.title()))
wp.debug("Adding the named as qualifier", layer="")
const.NAMED_AS_CLAIM.setTarget(entity_name)
claim.addQualifier(
const.NAMED_AS_CLAIM.copy(),
bot=True,
)
wp.debug("Adding the source claims", layer="")
today = datetime.datetime.today()
date = wp.WbTime(year=today.year, month=today.month, day=today.day)
const.RETRIEVED_CLAIM.setTarget(date)
claim.addSources(
[const.MUSICBRAINZ_CLAIM.copy(), const.RETRIEVED_CLAIM.copy()],
bot=True,
)
if wp.config.simulate:
wp.output("Simulation, no property has been added")
return
try:
item.addClaim(claim, True)
except wp.exceptions.OtherPageSaveError:
wp.warning("Page is protected, cannot save")
return
except wp.exceptions.Error as e:
wp.warning(e)
return
else:
self.donefunc(mbid)
def fix_redirect(self, gid, old, new):
"""
:param gid str:
:param old str:
:param new str:
"""
wp.output("Fixing the redirect from %s to %s" % (old, new))
self.client.edit_url(gid, old, new, self.redirect_edit_note % (old, new))
self._performed_edit()
def end_removed(self, rel_id, link_type_id, entity_gid, url_gid, entitytype, wikipage):
"""
:param rel_id str:
:param link_type_id str:
:param entity_gid str:
:param url_gid str:
:param entitytype str:
"""
url_entity = {'type': 'url', 'gid': url_gid, 'url': wikipage}
other_entity = {'type': entitytype, 'gid': entity_gid}
entity0 = other_entity if (entitytype < 'url') else url_entity
entity1 = url_entity if (entitytype < 'url') else other_entity
wp.output("Removing non existing page %s" % (wikipage))
self.client.edit_relationship(
rel_id,
entity0,
entity1,
link_type_id,
{},
{},
{},
True,
self.removed_edit_note % (wikipage),
False)
self._performed_edit()
def process_result(self, result):
entity_gid, url_gid, wikipage, rel_id, link_type_id, entity_name = result
wp.output("» {wp} https://musicbrainz.org/{entitytype}/{gid}".format(
entitytype=self._current_entity_type.replace("_", "-"),
wp=wikipage,
gid=entity_gid
))
try:
itempage = get_wikidata_itempage_from_wikilink(wikipage)
except wp.exceptions.SiteDefinitionError:
wp.warning("{page} no supported family".format(page=wikipage))
return
except (wp.exceptions.InvalidTitleError) as e:
wp.error("Bad or invalid title received while processing {page}".format(page=wikipage))
wp.exception(e, tb=True)
return
except SkipPage as e:
wp.warning("{page} is being skipped because: {reason}".format(page=wikipage,
reason=e))
return
except IsRedirectPage as e:
wp.output("{page} is a redirect".format(page=wikipage))
if self.can_edit:
self.fix_redirect(url_gid, e.old, e.new)
return
except ValueError as e:
wp.output(e)
return
except PageGone as e:
if self.can_edit:
self.end_removed(rel_id, link_type_id, entity_gid, url_gid, self._current_entity_type, wikipage)
return
if itempage is None:
wp.warning(u"There's no wikidata page for {mbid}".format(
mbid=entity_gid))
return
if any((key.lower() == self.property_id.lower() and
claim.target == entity_gid)
for key, claims in itempage.claims.items() for claim in claims):
wp.debug(u"{page} already has property {pid} with value {mbid}".
format(page=wikipage,
mbid=entity_gid,
pid=self.property_id))
self.donefunc(entity_gid)
return
wp.output("{mbid} is not linked in Wikidata".format(
mbid=entity_gid))
self.add_mbid_claim_to_item(itempage, entity_gid, entity_name)
def entity_type_loop(bot, entitytype, limit):
"""Processes up to `limit` entities of type `entitytype`.
:param bot Bot:
:param entitytype str:
:param limit int:
"""
bot.current_entity_type = entitytype
linkids = const.LINK_IDS[entitytype]
wiki_entity_query = create_url_mbid_query(entitytype, linkids)
already_processed_query = create_already_processed_query(entitytype)
with do_readonly_query(wiki_entity_query, limit) as all_results, do_readwrite_query(already_processed_query) as already_processed:
already_processed_results = frozenset((row[0] for row in already_processed))
results_to_process = [r for r in all_results if r[0] not in
already_processed_results]
if not results_to_process:
wp.output("No more unprocessed entries of type {etype} in MB".format(
etype=entitytype))
else:
wp.output("Processing {amount} {etype}s".format(amount=len(results_to_process),
etype=entitytype))
for r in results_to_process:
bot.process_result(r)
def mainloop():
limit = None
entities = sorted(const.PROPERTY_IDS.keys())
for arg in wp.handle_args():
if arg.startswith('-limit'):
limit = int(arg[len('-limit:'):])
elif arg.startswith("-entities"):
entities = arg[len("-entities:"):].split(",")
const.MUSICBRAINZ_CLAIM.setTarget(const.MUSICBRAINZ_WIKIDATAPAGE)
setup_db()
for entitytype in entities:
processed_table_query = create_processed_table_query(entitytype)
create_table(processed_table_query)
bot = Bot()
while True:
const.WIKIDATA.login()
for entitytype in entities:
entity_type_loop(bot, entitytype, limit)
bot.update_rate_limits()
sleep(settings.sleep_time_in_seconds)