-
Notifications
You must be signed in to change notification settings - Fork 8
/
doublecheck_move.py
84 lines (76 loc) · 2.64 KB
/
doublecheck_move.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Find cases where the sitelink move was attempted but failed
# Mike Peel 24-May-2019 v1
from __future__ import unicode_literals
import pywikibot
from pywikibot.data import api
import numpy as np
import time
import string
from pywikibot import pagegenerators
import urllib
import pprint
import csv
import json
from pibot_functions import *
seen = []
wikidata_site = pywikibot.Site("wikidata", "wikidata")
repo = wikidata_site.data_repository() # this is a DataSite object
commons = pywikibot.Site('commons', 'commons')
user = pywikibot.User(wikidata_site,'Pi bot')
targetcats = user.contributions(total=5000);
trip = 1
for targetcat in targetcats:
print(targetcat)
if "Moving commons category sitelink" in targetcat[3]:
if "Moving commons category sitelink from main item" in targetcat[3] or "fix incomplete move due to lag" in targetcat[3]:
qid = targetcat[3].split('(')[1].split(')')[0].replace('[','').replace(']','')
print(qid)
print(targetcat[0].title())
seen.append(targetcat[0].title())
elif "Moving commons category sitelink to category item" in targetcat[3]:
qid = targetcat[3].split('(')[1].split(')')[0].replace('[','').replace(']','')
print(qid)
if qid not in seen:
print('There is a problem')
print(targetcat[0].title())
target_item = pywikibot.ItemPage(repo, targetcat[0].title())
target_dict = target_item.get()
# Check to see if it's already been fixed
sitelink = ''
target_item2 = pywikibot.ItemPage(repo, qid)
target_dict2 = target_item2.get()
try:
sitelink = get_sitelink_title(target_dict2['sitelinks']['commonswiki'])
except:
print('No sitelink')
if sitelink != '':
seen.append(qid)
else:
targetedits = target_item.revisions()
for revision in targetedits:
print(revision)
revision_page = json.loads(target_item.getOldVersion(revision.revid))
print(revision_page)
try:
print(revision_page['sitelinks']['commonswiki']['title'])
except:
print('nope')
try:
sitelink = revision_page['sitelinks']['commonswiki']['title']
break
except:
continue
data = {'sitelinks': [{'site': 'commonswiki', 'title': sitelink}]}
print(data)
cat_item = pywikibot.ItemPage(repo, qid)
# text = input("Save? ")
# if text == 'y':
print('Saving!')
try:
cat_item.editEntity(data, summary=u'Moving commons category sitelink from main item item (' + str(targetcat[0].title()) + ') - fix incomplete move due to lag')
except:
print('Problem with ' + str(qid) + ' from ' + str(targetcat[0].title()))
exit()
# EOF