-
Notifications
You must be signed in to change notification settings - Fork 0
/
desuwiki.py
executable file
·160 lines (115 loc) · 3.46 KB
/
desuwiki.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python3
# -*- encoding: utf-8 -*-
import simplemediawiki
import sys
import urllib.parse
from conf import load_vars, VariablesMissing
def get_recent_changes (wiki, rcstart=None):
query = {
'action': 'query',
'list': 'recentchanges',
'rcdir': 'newer',
'rcprop': 'user|title|ids|timestamp',
'rvdiffto': 'prev',
'rctype': 'edit|new',
}
if rcstart is not None:
query['rcstart'] = rcstart
changes = []
while True:
reply = wiki.call(query)
changes += reply['query']['recentchanges']
if 'query-continue' in reply:
query.update(reply['query-continue']['recentchanges'])
else:
break
return changes
def load_state (filename):
STATE_VARS = {
'last_timestamp': str,
'last_revid': int,
}
DEFAULTS = {
'last_timestamp': None,
'last_revid': 0,
}
try:
state = load_vars(STATE_VARS, filename)
except VariablesMissing as e:
# If the file was empty, use defaults.
# Otherwise, something is wrong. Re-raise.
if e.variables == set(STATE_VARS.keys()):
state = DEFAULTS
else:
raise e
return state
def save_state (filename, state):
f = open(filename, 'w')
for var, value in state.items():
f.write('{:s}: {:s}\n'.format(var, str(value)))
def get_changes (conf, state_file, state_override={}):
state = load_state(state_file)
state.update(state_override)
uri_api = conf['uri_base'] + '/api.php'
w = simplemediawiki.MediaWiki(uri_api)
w.login(conf['username'], conf['password'])
changes = get_recent_changes(w, state['last_timestamp'])
if state['last_revid'] is not None:
changes = [c for c in changes if c['revid'] > state['last_revid']]
if changes:
last_change = changes[-1]
state['last_revid'] = last_change['revid']
state['last_timestamp'] = last_change['timestamp']
save_state(STATE_FILE, state)
return changes
def changes_by_page_id (changes):
res = {}
for change in changes:
id = change['pageid']
if id not in res:
res[id] = []
res[id].append(change)
return res
def encode_title (title):
return title.replace(' ', '_')
def page_uri (uri_base, title):
return '{:s}/{:s}'.format(uri_base, encode_title(title))
def diff_uri (uri_base, title, prev, cur):
return '{:s}/index.php?title={:s}&diff={:d}&oldid={:d}'.format(
uri_base, encode_title(title), cur, prev)
def format_page_changes (uri_base, changes):
if not changes:
return '<?>'
title = changes[-1]['title']
new_page = (changes[0]['type'] == 'new')
if new_page:
msg = '\2{:s}\2 - uusi sivu <{:s}>'.format(
title,
page_uri(uri_base, title))
else:
diff_prev = changes[0]['old_revid']
diff_cur = changes[-1]['revid']
uri = diff_uri(uri_base, title, diff_prev, diff_cur)
count = len(changes)
msg = '\2{:s}\2 - {:d} muokkaus{:s} <{:s}>'.format(
title, count, '' if count == 1 else 'ta',
uri)
return msg
def format_changes (uri_base, changes):
for page_id, page_changes in changes_by_page_id(changes).items():
page_title = page_changes[0]['title']
yield format_page_changes(conf['uri_base'], page_changes)
CONF_FILE = 'desuwiki.conf'
STATE_FILE = 'desuwiki.state'
CONF_VARS = {
'uri_base': str,
'username': str,
'password': str,
}
conf = load_vars(CONF_VARS, CONF_FILE)
changes = get_changes(conf, STATE_FILE)
#changes = get_changes(conf, STATE_FILE, {
# 'last_revid': 0,
#})
if changes:
print('\n'.join(format_changes(conf['uri_base'], changes)))