-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimlog.py
66 lines (55 loc) · 1.97 KB
/
imlog.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
import os
import re
import iso8601
import xml.etree.ElementTree as exml
import xml
import subprocess
import datetime
__all__ = ['AdiumLog']
class Message:
"""
Cheap container class for all messages
"""
def __init__(self):
pass
def __repr__(self):
return("%s: %s" % (self.sender, self.text))
class AdiumLog:
"""
Parses an Adium .chatlog.
Takes a path to either a .chatlog bundle or older-style
.chatlog file.
"""
def __init__(self, path):
self.path = path
# in the case we are passed the raw xml
if re.search('\.xml$', path):
self.handle = open(path)
# at some point adium went from using xml files with
# .chatlog extension to OS X bundles with xml files inside
if re.search('\.chatlog$', path):
if os.path.isdir(path):
basename = os.path.basename(path)
basename = re.sub(r"chatlog$", "xml", basename, 1)
xml_path = os.path.join(path, basename)
if os.path.isfile(xml_path):
self.handle = open(xml_path)
else:
self.handle = open(path)
# elementree xml confuses me
self.root = exml.fromstring(
re.sub('xmlns="http://purl.org/net/ulf/ns/0.4-02"',
' ',
self.handle.read()))
self.account = self.root.attrib['account']
self.service = self.root.attrib['service']
self.messages = []
for logmsg in self.root.findall('message'):
msg = Message()
tags = [exml.tostring(i, encoding="UTF-8", method="html")
for i in list(logmsg)]
msg.html = "".join(tags)
msg.text = "".join(logmsg.itertext())
msg.time = iso8601.parse_date(logmsg.attrib['time'])
msg.sender = logmsg.attrib['sender']
self.messages.append(msg)