forked from toastdriven/colloquy_to_textual
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_colloquy_to_textual.py
197 lines (156 loc) · 6.75 KB
/
convert_colloquy_to_textual.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from dateutil.parser import parse
import glob
from lxml import etree
from lxml import objectify
import os
import sys
__author__ = 'Daniel Lindsley'
__version__ = (0, 2, 1)
__license__ = 'New BSD'
# Approach:
# - Glob the transcripts
# - Create the "converted" directory
# - Per transcript:
# - Read the file
# - Load it with lxml
# - Open the file to write to
# - Iterate over the messages
# - Parse them, yanking out the details
# - Write the revised version to the new log file.
class ColloquyConvertor(object):
def __init__(self, directory_path, network='irc.freenode.net', iso_date_format=0):
self.colloquy_dir = directory_path
self.converted_path = os.path.join(directory_path, 'converted')
self.network = network
self.iso_date_format = iso_date_format
if self.iso_date_format:
self.message_format = u"[%(year)4d-%(month)02d-%(day)02dT%(hour)02d-%(minute)02d-%(second)02d] %(nick)s: %(message)s\n"
self.event_format = u"[%(year)4d-%(month)02d-%(day)02dT%(hour)02d-%(minute)02d-%(second)02d] %(event)s\n"
else:
self.message_format = u"[%(month)02d/%(day)02d/%(year)4d -:- %(hour)02d:%(minute)02d:%(second)02d %(ampm)s] %(nick)s: %(message)s\n"
self.event_format = u"[%(month)02d/%(day)02d/%(year)4d -:- %(hour)02d:%(minute)02d:%(second)02d %(ampm)s] %(event)s\n"
def get_transcript_list(self):
path = os.path.join(self.colloquy_dir, '*.colloquyTranscript')
return glob.glob(path)
def get_private_list(self):
path = os.path.join(self.colloquy_dir, self.network, '*.colloquyTranscript')
return glob.glob(path)
def read_file(self, filename):
log_file = open(filename, 'r')
return objectify.parse(log_file)
def get_new_transcript_path(self, old_transcript_filename):
channel_details, ext = os.path.splitext(os.path.basename(old_transcript_filename))
channel_name, date = channel_details.split(' ')
channels_dir = os.path.join(self.converted_path, 'Channels')
channel_dir = os.path.join(channels_dir, channel_name)
if not os.path.exists(channels_dir):
os.mkdir(channels_dir)
if not os.path.exists(channel_dir):
os.mkdir(channel_dir)
month, day, year = date.split('-')
log_filename = "20%02d-%02d-%02d.txt" % (int(year), int(month), int(day))
return os.path.join(channel_dir, log_filename)
def get_new_private_path(self, old_private_filename):
nick_details, ext = os.path.splitext(os.path.basename(old_private_filename))
nick, date = nick_details.split(' ')
queries_dir = os.path.join(self.converted_path, 'Queries')
query_dir = os.path.join(queries_dir, nick)
if not os.path.exists(queries_dir):
os.mkdir(queries_dir)
if not os.path.exists(query_dir):
os.mkdir(query_dir)
month, day, year = date.split('-')
log_filename = "20%02d-%02d-%02d.txt" % (int(year), int(month), int(day))
return os.path.join(query_dir, log_filename)
def clean_message(self, element):
etree.strip_tags(element, 'span', 'samp', 'a')
return element
def parse_envelope(self, element):
message_data = []
nick = element.sender.text
for message in element.message:
date = parse(message.get('received'))
ampm = 'AM'
hour = date.hour
if self.iso_date_format:
ampm = ''
else:
if date.hour > 12:
ampm = 'PM'
hour = hour - 12
if date.hour == 0:
hour = 12
if date.hour == 12:
ampm = 'PM'
clean_message = self.clean_message(message)
message_data.append({
'year': date.year,
'month': date.month,
'day': date.day,
'hour': hour,
'minute': date.minute,
'second': date.second,
'ampm': ampm,
'nick': nick,
'message': clean_message.text,
})
return message_data
def parse_event(self, element):
date = parse(element.get('occurred'))
ampm = 'AM'
hour = date.hour
if self.iso_date_format:
ampm = ''
else:
if date.hour > 12:
ampm = 'PM'
hour = hour - 12
if date.hour == 0:
hour = 12
if date.hour == 12:
ampm = 'PM'
clean_message = self.clean_message(element.message)
return {
'year': date.year,
'month': date.month,
'day': date.day,
'hour': hour,
'minute': date.minute,
'second': date.second,
'ampm': ampm,
'event': clean_message.text,
}
def write_updated(self, old_log, new_transcript_path):
new_log_file = open(new_transcript_path, 'w')
log = old_log.getroot()
for element in log.iterchildren():
if element.tag == 'envelope':
message_data = self.parse_envelope(element)
for data in message_data:
message = self.message_format % data
new_log_file.write(message.encode('utf-8'))
elif element.tag == 'event':
event_data = self.parse_event(element)
event = self.event_format % event_data
new_log_file.write(event.encode('utf-8'))
else:
print "Saw unrecognized tag '%s'. Continuing..." % element.tag
def run(self):
if not os.path.exists(self.converted_path):
os.mkdir(self.converted_path)
transcripts = self.get_transcript_list()
privates = self.get_private_list()
for transcript_filename in transcripts:
old_log = self.read_file(transcript_filename)
new_transcript_path = self.get_new_transcript_path(transcript_filename)
self.write_updated(old_log, new_transcript_path)
for private_filename in privates:
old_log = self.read_file(private_filename)
new_private_path = self.get_new_private_path(private_filename)
self.write_updated(old_log, new_private_path)
if __name__ == '__main__':
if not len(sys.argv) == 2:
print "Usage: %s <colloquy_path>"
sys.exit(1)
cc = ColloquyConvertor(directory_path=sys.argv[1], iso_date_format=0)
cc.run()