Update of /cvsroot/freevo/freevo/lib/pyepg
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26016
Modified Files:
__init__.py guide.py
Added Files:
channel.py program.py
Log Message:
merged freevo/src/tv/channels.py into the guide, needs much cleanup
--- NEW FILE: program.py ---
class Program:
"""
A tv program item for the tv guide and other parts of the tv submenu.
"""
def __init__(self, id, title, start, stop, episode, subtitle,
description, channel):
self.title = title
self.name = self.title
self.start = start
self.stop = stop
self.channel = channel
self.id = id
self.subtitle = subtitle
self.description = description
self.episode = episode
# FIXME: remove that information
self.scheduled = False
# TODO: add category support (from epgdb)
self.categories = ''
# TODO: add ratings support (from epgdb)
self.ratings = ''
Index: __init__.py
===================================================================
RCS file: /cvsroot/freevo/freevo/lib/pyepg/__init__.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** __init__.py 13 Nov 2004 16:10:20 -0000 1.5
--- __init__.py 9 Dec 2004 20:23:00 -0000 1.6
***************
*** 1,5 ****
from guide import EPGDB
! def get_epg(dbfile = '/tmp/epgdb'):
! return EPGDB(dbfile)
--- 1,10 ----
from guide import EPGDB
+ from program import Program
! guide = EPGDB()
!
! connect = guide.connect
! load = guide.init
!
! channels = guide.channel_list
--- NEW FILE: channel.py ---
from program import Program
class Channel:
"""
Information about one specific channel, also containing
epg informations.
"""
def __init__(self, id, display_name, access_id, epg):
self.id = id
self.access_id = access_id
self.programs = []
self.logo = ''
self.epg = epg
# XXX Change this to config.TV_CHANNEL_DISPLAY_FORMAT or something as
# XXX many people won't want to see the access_id.
# XXX What should we use, name or title, or both?
self.name = self.title = display_name
def sort_programs(self):
f = lambda a, b: cmp(a.start, b.start)
self.programs.sort(f)
def __get_dummy_programs(self, start, stop):
"""
Return some default Program with intervals no longer than a
set default.
"""
default_prog_interval = 30 * 60
dummies = []
d_start = start
d_stop = 0
sec_after_last = start % default_prog_interval
sec_until_next = default_prog_interval - sec_after_last
while(d_stop < stop):
d_stop = d_start + sec_until_next
if d_stop > stop:
d_stop = stop
dummies.append(Program(-1, u'NO DATA', d_start, d_stop,
'', '', '', self))
sec_until_next = default_prog_interval
d_start = d_stop
return dummies
def __import_programs(self, start, stop=-1, progs=[]):
"""
Get programs from the database to create Program from then
add them to our local list. If there are gaps between the programs
we will add dummy programs to fill it (TODO).
"""
new_progs = []
dummy_progs = []
if not progs:
progs = self.epg.get_programs(self.id, start, stop)
for p in progs:
i = Program(p.id, p.title, p.start, p.stop, p.episode, p.subtitle,
p['description'], channel=self)
new_progs.append(i)
# TODO: add information about program being recorded which
# comes from another DB table - same with categories,
# ratings and advisories.
l = len(new_progs)
if not l:
dummy_progs = self.__get_dummy_programs(start, stop)
else:
for p in new_progs:
i = new_progs.index(p)
if i == 0:
# fill gaps before
if p.start > start:
n = self.__get_dummy_programs(start, p.start)
dummy_progs += n
if i < l-1:
# fill gaps between programs
next_p = new_progs[i+1]
if p.stop < next_p.start:
n = self.__get_dummy_programs(p.stop, next_p.start)
dummy_progs += n
elif i == l-1:
# fill gaps at the end
if p.stop < stop:
n = self.__get_dummy_programs(p.stop, stop)
dummy_progs += n
for i in new_progs + dummy_progs:
if not i in self.programs:
self.programs.append(i)
self.sort_programs()
def get(self, start, stop=0):
"""
get programs between start and stop time or if stop=0, get
the program running at 'start'
"""
# get programs
if self.programs:
# see if we're missing programs before start
p = self.programs[0]
if p.start > start - 60:
self.__import_programs(start - 60, p.start)
# see if we're missing programs after end
p = self.programs[-1]
if stop == -1:
self.__import_programs(p.stop, stop)
elif stop == 0 and p.stop < start + 60:
self.__import_programs(p.stop, start + 60)
elif p.stop < stop:
self.__import_programs(p.stop, stop + 60)
else:
if stop == 0:
self.__import_programs(start - 60, start + 60)
else:
self.__import_programs(start, stop)
# return the needed programs
if stop == 0:
# only get what's running at time start
return filter(lambda x: x.start <= start, self.programs)[-1]
elif stop == -1:
# get everything from time start onwards
return filter(lambda x: x.stop > start, self.programs)
elif stop > 0:
# get everything from time start to time stop
return filter(lambda x: (x.start <= start and x.stop > start) or \
(x.start > start and x.stop < stop) or \
(x.start < stop and x.stop >= stop),
self.programs)
raise Exception('bad request: %s-%s' % (start, stop))
def get_relative(self, pos, prog):
new_pos = self.programs.index(prog) + pos
if new_pos < 0:
# requested program before start
self.__import_programs(prog.start-3*3600, prog.start)
return self.get_relative(pos, prog)
if new_pos >= len(self.programs):
# requested program after end
last = self.programs[-1]
self.__import_programs(last.stop, last.stop+3*3600)
return self.get_relative(pos, prog)
return self.programs[new_pos]
Index: guide.py
===================================================================
RCS file: /cvsroot/freevo/freevo/lib/pyepg/guide.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** guide.py 5 Dec 2004 17:08:53 -0000 1.12
--- guide.py 9 Dec 2004 20:23:00 -0000 1.13
***************
*** 11,14 ****
--- 11,17 ----
# -----------------------------------------------------------------------
# $Log$
+ # Revision 1.13 2004/12/09 20:23:00 dischi
+ # merged freevo/src/tv/channels.py into the guide, needs much cleanup
+ #
# Revision 1.12 2004/12/05 17:08:53 dischi
# wait when db is locked
***************
*** 27,65 ****
# checks which channels to exclude. Call expire_programs().
#
- # Revision 1.10 2004/11/27 15:00:37 dischi
- # create db if missing
- #
- # Revision 1.9 2004/11/27 02:22:38 rshortt
- # -Some verbose prints for debug.
- # -Debugging and changes to add_program().
- # -Impliment add_data_vdr() which uses vdrpylib to get guide info from VDR.
- # This is still full of debug and is a work in progress but its working
- # well at this point. You can find vdrpylib at http://vdrpylib.sf.net/ and
- # a CVS snapshot is available:
http://vdrpylib.sf.net/vdrpylib-20041126.tar.gz
- #
- # Revision 1.8 2004/11/19 19:03:29 dischi
- # add get_program_by_id
- #
- # Revision 1.7 2004/11/13 16:10:21 dischi
- # replace compat.py and config.py with a sysconfig variante
- #
- # Revision 1.6 2004/11/12 20:40:36 dischi
- # support program get from all channels
- #
- # Revision 1.5 2004/10/23 16:20:07 rshortt
- # Comment out a sometimes spammy debug.
- #
- # Revision 1.4 2004/10/23 14:29:35 rshortt
- # Updates and bugfixes. Please excuse the mass of commented code and debug.
- #
- # Revision 1.3 2004/10/20 01:06:30 rshortt
- # Unicode changes.
- #
- # Revision 1.2 2004/10/18 01:04:01 rshortt
- # Rework pyepg to use an sqlite database instead of an object pickle.
- # The EPGDB class is the primary interface to the databse, wrapping database
- # queries in method calls.
- #
- #
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
--- 30,33 ----
***************
*** 95,98 ****
--- 63,68 ----
import config
+ from channel import Channel
+ from program import Program
def escape_query(sql):
***************
*** 175,180 ****
Class for working with the EPG database
"""
! def __init__(self, dbpath):
if not os.path.isfile(dbpath):
print 'epg database missing, creating it'
--- 145,153 ----
Class for working with the EPG database
"""
+ def __init__(self):
+ self.channel_list = []
+ self.channel_dict = {}
! def connect(self, dbpath):
if not os.path.isfile(dbpath):
print 'epg database missing, creating it'
***************
*** 185,188 ****
--- 158,178 ----
+ def init(self, TV_CHANNELS=[], TV_CHANNELS_EXCLUDE=[]):
+
+ # Check TV_CHANNELS and add them to the list
+ for c in TV_CHANNELS:
+ self.add_channel(Channel(c[0], c[1], c[2:], self))
+
+ # Check the EPGDB for channels. All channels not in the exclude list
+ # will be added if not already in the list
+ for c in self.get_channels():
+ if String(c['id']) in TV_CHANNELS_EXCLUDE:
+ # Skip channels that we explicitly do not want.
+ continue
+ if not c['id'] in self.channel_dict.keys():
+ self.add_channel(Channel(c['id'], c['display_name'],
c['access_id'], self))
+
+
+
def execute(self, query, close=False):
query = escape_query(query)
***************
*** 221,225 ****
! def add_channel(self, id, display_name, access_id):
query = 'insert or replace into channels (id, display_name,
access_id) \
values ("%s", "%s", "%s")' % (id, display_name, access_id)
--- 211,215 ----
! def db_add_channel(self, id, display_name, access_id):
query = 'insert or replace into channels (id, display_name,
access_id) \
values ("%s", "%s", "%s")' % (id, display_name, access_id)
***************
*** 502,520 ****
- def search_programs(self, subs, by_chan=None):
- now = time.time()
- clause = 'where stop > %d' % now
- if by_chan:
- clause = '%s and channel_id="%s"' % (clause, by_chan)
-
- clause = '%s and title like "%%%s%%"' % (clause, subs)
-
- query = 'select * from programs %s order by channel_id, start' %
clause
- # print 'QUERY: %s' % query
- rows = self.execute(query)
-
- return rows
-
-
def add_data_xmltv(self, xmltv_file, exclude_channels=None, verbose=1):
import xmltv
--- 492,495 ----
***************
*** 579,583 ****
print 'Adding channel: %s as %s' % (c.id, access_id)
! self.add_channel(c.id, c.name, access_id)
for e in c.events:
subtitle = e.subtitle
--- 554,558 ----
print 'Adding channel: %s as %s' % (c.id, access_id)
! self.db_add_channel(c.id, c.name, access_id)
for e in c.events:
subtitle = e.subtitle
***************
*** 593,594 ****
--- 568,616 ----
return True
+ # channel list code
+
+ def sort_channels(self):
+ pass
+
+
+ def add_channel(self, channel):
+ """
+ add a channel to the list
+ """
+ if not self.channel_dict.has_key(channel.id):
+ # Add the channel to both the dictionary and the list. This works
+ # well in Python since they will both point to the same object!
+ self.channel_dict[channel.id] = channel
+ self.channel_list.append(channel)
+
+
+ def __getitem__(self, key):
+ return self.channel_list[key]
+
+
+ def get_channel(self, pos, start=None):
+ if not start:
+ start = self.channel_list[0]
+ cpos = self.channel_list.index(start)
+ pos = (cpos + pos) % len(self.channel_list)
+ return self.channel_list[pos]
+
+
+ def search_programs(self, subs, by_chan=None):
+ now = time.time()
+ clause = 'where stop > %d' % now
+ if by_chan:
+ clause = '%s and channel_id="%s"' % (clause, by_chan)
+
+ clause = '%s and title like "%%%s%%"' % (clause, subs)
+
+ query = 'select * from programs %s order by channel_id, start' %
clause
+ result = []
+ for p in self.execute(query):
+ if self.channel_dict.has_key(p.channel_id):
+ result.append(Program(p.id, p.title, p.start, p.stop,
p.episode,
+ p.subtitle,
description=p['description'],
+
channel=self.channel_dict[p.channel_id]))
+ return result
+
+
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog