forked from Pascalco/DeltaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadges.py
83 lines (77 loc) · 2.52 KB
/
badges.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# licensed under CC-Zero: https://creativecommons.org/publicdomain/zero/1.0
import requests
import json
import pywikibot
from pywikibot.data import api
site = pywikibot.Site('wikidata', 'wikidata')
site.login()
repo = site.data_repository()
site.get_tokens('edit')
ERROR_THRES = 50
r = requests.get('https://www.wikidata.org/wiki/User:DeltaBot/badges?action=raw')
tasks = r.json()
# remove badges
for t in tasks:
payload = {
'categories': t['category'],
'language': t['language'],
'project': t['project'],
'sparql': 'SELECT ?item WHERE { ?article schema:about ?item;wikibase:badge wd:'+t['badge']+';schema:isPartOf <https://'+t['language']+'.'+t['project']+'.org/>}',
'source_combination': 'sparql not categories',
'ns[0]': 1,
'ns[100]': 1,
'common_wiki': 'wikidata',
'format': 'json',
'doit': 'Do it!'
}
r = requests.get('https://petscan.wmflabs.org/', params=payload)
data = r.json()
if len(data['*'][0]['a']['*']) > ERROR_THRES:
continue
for m in data['*'][0]['a']['*']:
try:
params = {
'action': 'wbsetsitelink',
'id': m['title'],
'linksite': t['site'],
'badges': '',
'token': site.tokens['edit']
}
print(m)
#req = api.Request(site=site, **params)
#data = req.submit()
except:
pass
# add badges
for t in tasks:
payload = {
'categories': t['category'],
'language': t['language'],
'project': t['project'],
'sparql': 'SELECT ?item WHERE { ?article schema:about ?item;wikibase:badge wd:'+t['badge']+';schema:isPartOf <https://'+t['language']+'.'+t['project']+'.org/>}',
'source_combination': 'categories not sparql',
'ns[0]': 1,
'ns[100]': 1,
'common_wiki': 'wikidata',
'format': 'json',
'doit': 'Do it!'
}
r = requests.get('https://petscan.wmflabs.org/', params=payload)
data = r.json()
if len(data['*'][0]['a']['*']) > ERROR_THRES:
continue
for m in data['*'][0]['a']['*']:
try:
params = {
'action': 'wbsetsitelink',
'id': m['title'],
'linksite': t['site'],
'badges': t['badge'],
'token': site.tokens['edit']
}
req = api.Request(site=site, **params)
data = req.submit()
except:
pass