-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpost.py
More file actions
executable file
·137 lines (106 loc) · 4.24 KB
/
post.py
File metadata and controls
executable file
·137 lines (106 loc) · 4.24 KB
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
#!/usr/bin/python
# Read irc logs from our private channel and post them to our wiki
import datetime
import glob
import json
import os
import re
import sys
import textwrap
from simplemediawiki import MediaWiki
with open(os.path.expanduser('~/.mediawiki'), 'r') as f:
conf = json.loads(f.read())['ircbot']
wiki = MediaWiki(conf['url'])
day_re = re.compile('^--- Day changed (.*) (.*) ([0-9]+) (20[0-9]+)$')
human_re = re.compile('.*<([^>]+)>.*')
days = {}
days_order = []
months = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
def make_wiki_login_call(packet):
packet.update({'lgname': conf['username'],
'lgpassword': conf['password']})
return wiki.call(packet)
def post_page(title, text, day_tuple):
# If day_tuple is set, then this is a daily log file, not the index
if day_tuple:
key = '%s %s' %(day_tuple[1], day_tuple[3])
days.setdefault(key, [])
days[key].append('[[%s|%s]]' %(title, day_tuple[2]))
if not key in days_order:
days_order.append(key)
# Sample ['Wed', 'Jun', '25', '2014']
dt = datetime.datetime(int(day_tuple[3]),
months[day_tuple[1]],
int(day_tuple[2]))
age = datetime.datetime.now() - dt
if age.days > 30:
return
page_token = wiki.call({'action': 'query',
'prop': 'info',
'titles': title,
'intoken': 'edit'})
pages = page_token['query']['pages']
page_id = pages.keys()[0]
response = wiki.call({'action': 'edit',
'minor': True,
'bot': True,
'title': title,
'text': json.dumps(text).replace('\\n', '\n')[1:-1],
'token': pages[page_id]['edittoken']})
if not 'nochange' in response['edit']:
print 'Modified %s' % title
def process_log(logglob, name):
global days
global days_order
days = {}
days_order = []
day = None
content = []
filenames = sorted(glob.glob(logglob))
ordered_filenames = filenames[1:]
ordered_filenames.append(filenames[0])
for filename in ordered_filenames:
print filename
with open(filename, 'r') as f:
l = f.readline()
while l:
if l.startswith('--- Day'):
m = day_re.match(l)
if m:
new_day = [m.group(1), m.group(2), m.group(3),
m.group(4)]
# print 'Day %s to %s' %(day, new_day)
if content and day != new_day:
post_page('%s irc log for %s' % (name, ' '.join(day)),
''.join(content), day)
content = []
day = new_day
elif day:
lines = textwrap.wrap(l.rstrip(), 120)
m = human_re.match(l)
if m and len(m.group(1)) > 1:
content.append(' \'\'\'%s\'\'\'\n'
% '\n \'\'\' '.join(lines))
elif l[7] == '*':
content.append(' \'\'%s\'\'\n'
% '\n \'\' '.join(lines))
else:
content.append(' %s\n' % '\n '.join(lines))
l = f.readline()
if day and content:
post_page('%s irc log for %s' % (name, ' '.join(day)),
''.join(content), day)
if days:
content = ''
for day in reversed(days_order):
content += ' %s: ' % day
content += ' '.join(days[day])
content += '\n'
post_page('%s irc log index' % name, content, None)
if __name__ == '__main__':
login = make_wiki_login_call({'action': 'login'})
token = make_wiki_login_call({'action': 'login',
'lgtoken': login['login']['token']})
for logconfig in conf['logpath']:
process_log(logconfig['glob'], logconfig['name'])