forked from michaelverkooijen/kinmune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesbooks.py
executable file
·76 lines (59 loc) · 2.68 KB
/
tesbooks.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
#!/usr/bin/env python3
import core
import os
import requests
import json
from urllib.parse import quote
import sys
headers = {'Connection': 'Keep alive', 'Content-Type': 'application/x-www-form-urlencoded', 'user-agent': 'Flightmare/bot'}
# load settings.json
print(os.environ['HOME'])
settings = json.load(open(os.environ['HOME']+'/.discussions-bot/settings.json', 'r'))
wiki = settings['wiki']
username = settings['username']
password = settings['password']
is_mod = settings['isMod']
print(username)
# Log in to Wikia network
session = core.login(wiki, username, password)
print(core.is_logged_in(session, username, wiki))
wiki_id = core.get_wiki_id(session, wiki)
edit_token = core.get_edit_token(session, wiki)
print(edit_token)
payload = {'action': 'query', 'list': 'embeddedin', 'eititle': 'Template:Book', 'eilimit': '5000', 'format': 'json'}
r = session.get('https://'+wiki+'.wikia.com/api.php', params=payload)
print(r.url)
#FIXME: not compatible with articles over 8k in size due to not using multi-part uploads.
#TODO: implement continue from articleID in case of resuming after emergency quit
for page in r.json()['query']['embeddedin']:
#sys.stdout.buffer.write((page['title']).encode('utf-8'))
# get page contents
payload = {'action': 'raw'}
body = session.get('https://'+wiki+'.wikia.com/wiki/'+page['title'], params=payload).text
new_body = ''
in_gallery = False
page_number = 1
# Searches for <gallery type="slideshow" widths="250"> or <gallery type="slideshow" widths="250" position="center">
# Some pages have galleries without images, skip these!
for line in body.splitlines():
if '</gallery>' in line:
in_gallery = False
if in_gallery:
if '|' not in line:
line = line + '|Page ' + str(page_number)
page_number = page_number + 1
if '<gallery type="slideshow" widths="250">' in line:
if '</gallery>' not in line:
in_gallery = True
line = '|image = <gallery>'
if '<gallery type="slideshow" widths="250" position="center">' in line:
if '</gallery>' not in line:
in_gallery = True
line = '|image = <gallery>'
if '}}}}{{For' in line:
print(page['title'])
new_body = new_body + line + '\n'
#sys.stdout.buffer.write((new_body).encode('utf-8'))
#if body is not new_body:
#payload = {'action': 'edit', 'title': page['title'], 'summary': 'PI Gallery fix', 'bot': '1', 'watchlist': 'nochange', 'format': 'json', 'text': new_body, 'token': edit_token}
#print(session.post('http://'+wiki+'.wikia.com/api.php', data=payload, headers=headers).text)