added Time class to hold start and end times, more structured way than to handle...
[tvfs.git] / main.py
blob21f58f55ae5aaf657c24d574182f13a2f00a7a12
1 #!/usr/bin/python
3 # tvfs - a simple fuse-module to mount a tv-guide as a filesystem
4 # Copyright (C) 2008, Kristian Rumberg (kristianrumberg@gmail.com)
6 # Permission to use, copy, modify, and/or distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 import re
19 import urllib
20 import tempfile
21 import gzip
22 import libxml2
23 import datetime
25 class Error(Exception):
26 def __init__(self, value):
27 Exception.__init__(self)
28 self.value = value
29 def __str__(self):
30 return repr(self.value)
32 class GZStream:
33 def __init__(self, url):
34 self.url = url
36 def read(self):
37 stream = urllib.urlopen(self.url)
38 buff = stream.read()
39 f = tempfile.NamedTemporaryFile()
40 f.write(buff)
41 f.flush()
42 gs = gzip.open(f.name)
43 buff = gs.read()
44 gs.close()
45 f.close()
46 return buff
48 class Time:
49 def __init__(self, timestr):
50 o = re.match("([0-9][0-9]):([0-9][0-9])", timestr)
51 if None == o:
52 raise Error("Unable to parse time string")
53 self.hour = str(o.group(1))
54 self.minute = str(o.group(2))
56 def __str__(self):
57 return self.hour + ":" + self.minute
59 class Program:
60 def __init__(self, name, desc, start_str, end_str):
61 self.name = name
62 self.desc = desc
63 self.start = Time(start_str)
64 self.end = Time(end_str)
66 def get_name(self):
67 return self.name
69 def get_description(self):
70 return self.desc
72 def get_start(self):
73 return self.start
75 def get_end(self):
76 return self.end
78 class ProgramList:
79 def __init__(self, name, url):
80 self.url = url
81 self.name = name
82 self.cl = []
84 istream = GZStream(url)
85 doc = libxml2.parseDoc(istream.read())
87 namelist = doc.xpathEval('//programme//title/text()')
88 desclist = doc.xpathEval('//programme//desc/text()')
89 startlist = doc.xpathEval('//programme/attribute::start')
90 endlist = doc.xpathEval('//programme/attribute::stop')
92 for name,desc,start,end in zip(namelist,desclist,startlist,endlist):
93 name = str(name.content)
94 desc = str(desc.content)
96 start = str(start.content)[8:12]
97 start = start[0:2] + ":" + start[2:4]
99 end = str(end.content)[8:12]
100 end = end[0:2] + ":" + end[2:4]
102 self.cl.append( Program(name, desc, start, end) )
104 def __iter__(self):
105 return self.cl.__iter__()
107 class Channel:
108 def __init__(self, name, baseurl):
109 self.name = name
110 self.baseurl = baseurl
111 self.today = None
113 def list_today(self):
114 if None == self.today:
115 d = datetime.datetime.now()
116 path = self.baseurl + "_" + d.strftime("%Y-%m-%d") + ".xml.gz"
117 self.today = ProgramList(self.name, path)
118 return self.today
120 def get_name(self):
121 return self.name
123 class ChannelList:
124 def __init__(self):
125 basepath = "http://tv.swedb.se/xmltv/"
126 self.cl = []
128 istream = GZStream(basepath + "channels.xml.gz")
129 doc = libxml2.parseDoc(istream.read())
131 namelist = doc.xpathEval('//display-name/text()')
132 idlist = doc.xpathEval('//channel/attribute::id')
133 baselist = doc.xpathEval('//base-url/text()')
135 for name,idn,base in zip(namelist,idlist,baselist):
136 name = str(name.content)
137 idn = str(idn.content)
138 base = str(base.content)
139 self.cl.append( Channel(name, base + idn))
141 def __iter__(self):
142 return self.cl.__iter__()
144 def has_channel(self, chanstr):
145 return -1 != self.cl.index[chanstr]
148 def main():
149 cl = ChannelList()
150 for c in cl:
151 print c.get_name()
152 pl = c.list_today()
153 for p in pl:
154 print p.get_name()
155 print p.get_start()
156 print p.get_end()
157 print p.get_description()
159 if __name__ == "__main__":
160 main()