-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
50 lines (42 loc) · 1.48 KB
/
bot.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
import json
import os.path
import parser
import pywikibot
import random
def create_lexeme(site, lexeme):
request = {
'action': 'wbeditentity',
'format': 'json',
'new': 'lexeme',
'summary': '[[:d:Wikidata:Requests for permissions/Bot/EnvlhBot 1|Henry import]]',
'token': site.tokens['edit'],
'bot': '1',
'data': lexeme,
}
site._simple_request(**request).submit()
def get_site():
site = pywikibot.Site('wikidata', 'wikidata')
site.login()
return site
def main():
site = get_site()
conf = parser.load_json_file('conf/general.json')
todo_filepath = 'data/lexemes_todo.json'
if os.path.isfile(todo_filepath):
print('Resuming import with todo file {}'.format(todo_filepath))
lexemes = parser.load_json_file(todo_filepath)
else:
lexemes_filepath = 'data/{}/lexemes_{}.json'.format(conf['iteration'], conf['iteration'])
print('Starting new import with lexemes file {}'.format(lexemes_filepath))
lexemes = parser.load_json_file(lexemes_filepath)
for i in range(0, 100):
if len(lexemes) < 1:
break
lexeme = lexemes.pop(random.randrange(len(lexemes)))
lexeme_str = json.dumps(lexeme, ensure_ascii=False)
print(lexeme_str)
with open(todo_filepath, 'w', encoding='utf-8') as myfile:
json.dump(lexemes, myfile, ensure_ascii=False)
create_lexeme(site, lexeme_str)
if __name__ == '__main__':
main()