-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcast_timestamps.py
165 lines (124 loc) · 5 KB
/
podcast_timestamps.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
#!/usr/bin/env python
# This script calculates podcast timestamps in IRC logs written by
# irclog2html.py (by Marius Gedminas, see http://mg.pov.lt/irclog2html/)
# relative to a given start and duration. It finally inserts them
# into the given HTML logs.
#
# Copyright (C) 2011 Bastian S.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from lxml import etree
from datetime import datetime, timedelta
import StringIO
import time
import sys
# symbols identifying preshow, show and postshow IRC logs
preShowSymbol = "-"
showSymbol = " "
postShowSymbol = "+"
# function calculating the podcast timestamp from IRC timestamp
def generatePlayTime(timestamp, startTime, endTime):
global showStatus, preShowSymbol, showSymbol, postShowSymbol
# preshow
if timestamp < startTime and showStatus == "preshow":
timestamp = startTime - timestamp - timedelta(hours=1)
timestamp = datetime.fromtimestamp(timestamp.seconds)
symbol = preShowSymbol
# cut everything off 1 hour before show started
if timestamp.hour >= 1:
return None
# postshow
elif (timestamp > endTime and showStatus == "show") or showStatus == "postshow":
laststamp = timestamp
timestamp = timestamp - timedelta(hours=startTime.hour, minutes=startTime.minute)
symbol = postShowSymbol
# entering postshow
if showStatus == "show":
symbol = showSymbol
showStatus = "postshow"
# cut everything off 1 hour after show stopped
if (startTime.hour + timestamp.hour == endTime.hour + 1 and (startTime.minute + timestamp.minute) % 60 > endTime.minute) or startTime.hour + timestamp.hour > endTime.hour + 1:
return None
# show
elif timestamp >= startTime:
laststamp = timestamp
timestamp = timestamp - timedelta(hours=startTime.hour, minutes=startTime.minute)
symbol = showSymbol
# entering show
if showStatus == "preshow":
symbol = showSymbol
showStatus = "show"
else:
print "Could not calculate podcast timestamps prior to \"%s\". Timestamps are ambigous." % timestamp.strftime("%H:%M")
quit()
return "%s%s" % (symbol, timestamp.strftime("%H:%M"))
if len(sys.argv) <= 3:
# print usage information
print "Usage: %s [HTML LOG FILE] [START TIME HH:MM] [DURATION HH:MM]" % sys.argv[0]
else:
# we use utf-8 to avoid encoding fuckups
reload(sys)
sys.setdefaultencoding("utf-8")
# retrieve log file, start/duration timestamps from commandline arguments
filename = sys.argv[1]
startTime = sys.argv[2]
duration = sys.argv[3]
# convert start/duration time
startTime = time.strptime(startTime, "%H:%M")
duration = time.strptime(duration, "%H:%M")
# use a random static date to calculate podcast timestamp
startTime = datetime(2000, 1, 1, startTime.tm_hour, startTime.tm_min)
endTime = startTime + timedelta(hours=duration.tm_hour, minutes=duration.tm_min)
# set initial show status
showStatus = "preshow"
# open html log file
logFile = open(filename)
log = logFile.read()
logFile.close()
# parse XHTML-source
parser = etree.XMLParser(remove_blank_text=True, resolve_entities=False)
tree = etree.parse(StringIO.StringIO(log), parser)
# XPath to log rows
lines = tree.xpath("//table[@class='irclog']/tr")
# check if XPath points to something
if len(lines) == 0:
print "Error: You can run this script only once per logfile."
else:
# beginning of time ;)
lastStamp = datetime(1970, 1, 1, 0, 1)
# loop through log rows
for line in lines:
if "id" in line.attrib:
# get time from table row (tr) id (using a random static date to calculate podcast timestamp)
timestamp = time.strptime(line.attrib["id"], "t%H:%M")
timestamp = datetime(2000, 1, 1, timestamp.tm_hour, timestamp.tm_min)
# running over midnight?
if timestamp < lastStamp:
timestamp = datetime(timestamp.year, timestamp.month, timestamp.day + 1, timestamp.hour, timestamp.minute)
# remember last timestamp
lastStamp = timestamp
playTime = generatePlayTime(timestamp, startTime, endTime)
if playTime:
# create podcast timestamp column
timestampTd = etree.Element("td")
timestampTd.text = generatePlayTime(timestamp, startTime, endTime)
timestampTd.set("class", "timestamp")
# insert podcast timestamp as first child of table row (tr)
line.insert(0, timestampTd)
else:
line.getparent().remove(line)
elif len(list(line)) > 0 and list(line)[0].get("class") == "servermsg":
list(line)[0].getparent().remove(list(line)[0])
# write changes to file
tree.write(filename, encoding="utf-8", pretty_print=True)