-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBEIC_thanks.py
executable file
·129 lines (117 loc) · 3.72 KB
/
BEIC_thanks.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
#!/usr/bin/python
"""
Script to thank contributors to articles linked to a list of Wikidata IDs.
Q# (integers) must be placed one per line in a "qids.txt" file.
"""
#
# (C) Federico Leva e Fondazione BEIC, 2015
#
# Distributed under the terms of the MIT license.
#
__version__ = '0.1.0'
#
# Needs wikitools 1.3
from wikitools import wiki
from wikitools import api
from wikitools import page
import time
import re
from collections import defaultdict
base = wiki.Wiki("http://www.wikidata.org/w/api.php")
thankscount = defaultdict(int)
deferred = open('deferred-thanks.txt', 'a')
def getRelevantRevisions(wikiapi, title):
revisions = set([])
editors = set([])
hashes = set([])
revisionby = defaultdict()
size_old = 0
site = wiki.Wiki(wikiapi)
article = page.Page(site, title)
hist = article.getHistory(direction='newer', content=False)
for i in hist:
# Cannot thank unregistered users yet
# if i['userid'] == 0:
# continue
if 'userhidden' in i:
continue
try:
# TODO: Consider thanking for the biggest edit instead
if not revisions or ( i['user'] not in editors
and i['sha1'] not in hashes and i['size'] - size_old > 1000 ):
if thankscount[i['user']] <= 5:
revisions.add( i['revid'] )
editors.add( i['user'] )
revisionby[i['revid']] = i['user']
thankscount[i['user']] += 1
else:
deferred.write( '%s\t%d\n' % ( wikiapi, i['revid'] ) )
# Never thank occurrences of a text after the first.
hashes.add( i['sha1'] )
except KeyError:
continue
size_old = i['size']
params = { 'action': 'query', 'list': 'blocks', 'bkprop': 'userid', 'bkusers': '|'.join( editors ) }
blocked = api.APIRequest(site, params).query()
try:
for block in blocked['query']['blocks']:
print 'Skipping blocked user "%s"' % block['user']
revisions.remove( revisionby[block['user']] )
except KeyError:
continue
return revisions
def getBlockedAuthors(wikiapi, revisions, history)
site = wiki.Wiki(wikiapi)
params = { 'action': 'query', 'list': 'blocks', 'bkprop': 'userid', }
def thankRevisions(wikiapi, revisions):
thanker = wiki.Wiki(wikiapi)
thanker.login("Federico Leva (BEIC)", "****")
for revision in revisions:
# Should use getToken()
params = { 'action':'query', 'meta':'tokens' }
token = api.APIRequest(thanker, params).query()['query']['tokens']['csrftoken']
params = { 'action': 'thank', 'rev': revision, 'token': token }
try:
request = api.APIRequest(thanker, params).query()
except:
print "Thanking for revision %d failed!" % revision
time.sleep(7)
def getPages():
qids = open('qids.txt', 'r')
apiMap = getApiMap()
for qid in qids:
qid = qid.strip()
print "Now looking into %s" % qid
params = { 'action': 'wbgetentities', 'props' : 'sitelinks', 'ids': 'Q' + qid }
request = api.APIRequest(base, params)
result = request.query()
try:
sitelinks = result['entities']['Q' + qid]['sitelinks']
except:
continue
for sitelink in sitelinks:
# We ignore specials
if sitelink in apiMap:
wikiapi = apiMap[sitelink]
yield [ wikiapi, sitelinks[sitelink]['title'] ]
raise StopIteration
def getApiMap():
apiMap = {}
request = api.APIRequest(base, { 'action': 'sitematrix' })
query = request.query()
result = query['sitematrix']
# Two extra items, count and specials
for i in range( len(result) - 2 ):
for site in result[str(i)]['site']:
apiMap.update( { site['dbname']: site['url'] + '/w/api.php' } )
return apiMap
def main():
for [wikiapi, title] in getPages():
if re.search('(ru|de|it|sv|fr)\.wikipedia', wikiapi):
continue
print 'Now doing "%s" via %s' % (title, wikiapi)
revisions = getRelevantRevisions(wikiapi, title)
print 'Found %d revisions to thank for' % len(revisions)
thankRevisions(wikiapi, revisions)
if __name__ == "__main__":
main()