[Wepg-devel] mythcond serie.py,NONE,1.1 myth.sql,1.2,1.3
Brought to you by:
leonvs
|
From: Leon v. S. <le...@us...> - 2004-08-07 18:53:00
|
Update of /cvsroot/wepg/mythcond In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5879 Modified Files: myth.sql Added Files: serie.py Log Message: ok --- NEW FILE: serie.py --- # mythcond - a pilot-link conduit for wepg # Copyright (C) 2004 Leon van Stuivenberg <l.v...@ch...> # # 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 2 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, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import MySQLdb import random import string import time import os.path import socket import sys import tempfile import mx # defines; flags: true = 1==1 false = not true #oneDay = mx.RelativeDateTime.oneDay oneDay = 1 # fixme: too large, but allows shifts between DST and noDST this way. TOLERANCE = 3.0/24 def mux(a,b,c): if a: return b return c def loadBroadcasts(mythdb, chanid, start_time, end_time, base_day_offset, day_offset, count, offsets): offsets.extend( range(base_day_offset, base_day_offset + count * day_offset, day_offset) ) print "loading off",offsets bca = [] for i in range(count): yc1 = start_time - offsets[i] * oneDay yc2 = end_time - offsets[i] * oneDay cc = mythdb.cursor() #note: includes overlap # fixme: uid cc.execute("""SELECT starttime, endtime, title, lower(left(title,31)) FROM program WHERE chanid=%s AND starttime<%s AND endtime>%s ORDER BY starttime""", (chanid, yc2, yc1)) tmp = [] while 1: row = cc.fetchone() if row == None: break; tuple = { "starttime":row[0], "endtime":row[1], "title":row[2], "uid":row[3] } tmp.append(tuple) cc.close() bca.append(tmp) print "in this tmp ",len(tmp) return bca def saveSerie(mythdb, bc): print "serie: ",len(bc) for i in bc: print i["starttime"],i["uid"] def matchEpisodes(a, b, delta, day_offset): a_st = a["starttime"] b_st = b["starttime"] margin_low = b_st - delta + day_offset; margin_high = b_st + delta + day_offset; if not (a_st > margin_low and a_st < margin_high): return None a_et = a["endtime"] b_et = b["endtime"] margin_low = b_et - delta + day_offset margin_high = b_et + delta + day_offset if not (a_et > margin_low and a_et < margin_high): return None if a["uid"] == b["uid"]: #fixme: move forward devi = (b_st + 0.5*(b_et - b_st)) - (a_st + 0.5*(a_et-a_st)) + day_offset return [b, devi] return None def findSerieInDay(master, candidates, day_offset): best = None besti = -1 for i in range(len(candidates)): c = candidates[i] if c == None: continue; r = matchEpisodes(master, c, TOLERANCE, day_offset) if r != None and (best == None or r[1] < best[1]): best = r besti = i if best != None: print "found match for ",master,"->",best[0] candidates[besti] = None return best[0] return None def findSerieStrings(mythdb, bca, day_offset): # for each day (except last) for i in range(0, len(bca)-1): # for each broadcast that day for j in range(0, len(bca[i])): m = bca[i][j] if m == None: # already assigned to a serie continue v = [m] # try finding a similar broadcast the previous day/week for k in range(i+1, len(bca)): f = findSerieInDay(m, bca[k], day_offset[k] - day_offset[i]); if f != None: v.append(f) if len(v) > 1: saveSerie(mythdb, v) return def findSeries2(mythdb, chanid, start_time, end_time, day_count, week_count, week_day_count): # Consecutive days. # find broadcasts that repeat daily on the current station. # 1. per day a list of broadcasts: # # [now] [yesterday] [now-2days] ... [now-(daycount-1)] # {bc, {bc, {bc, { { # bc, bc, bc, . . # bc, bc bc, . . # bc, } bc, . } # bc bc, } # } bc # } # # 2. for each available bc in these arrays, check if on previous day(s) it was also on. # 3. if a 'string' of bcs is found, save as a series, and mark the bcs unavailable # by setting the array element to null. # # note: to catch series that skip the weekend, use a day_count >3. offsets = [] bcs = loadBroadcasts(mythdb, chanid, start_time, end_time, 0, 1, day_count, offsets) findSerieStrings(mythdb, bcs, offsets) # if this runs every day, it's sufficient if week_day_count ==1. # otherwise, also weekly bcs that were on yesterday (and further back) # should be checked. for i in range(week_day_count): offsets = [] bcs = loadBroadcasts(mythdb, chanid, start_time, end_time, i, 7, week_count, offsets) findSerieStrings(mythdb, bcs, offsets) return mythdb = MySQLdb.connect(host="bender", user="mythtv", passwd="mythtv", db="mythconverg") day_offset = 0 day_count = 5 week_count = 3 week_day_count = 1 c1 = mx.DateTime.today() c1 = c1 + day_offset * oneDay c2 = c1 + oneDay cu=mythdb.cursor() cu.execute("SELECT chanid FROM channel WHERE visible=1") while 1: row = cu.fetchone() if row == None: break; chanid = row[0] print "processing channel ",chanid findSeries2(mythdb, chanid, c1, c2, day_count, week_count, week_day_count) cu.close() mythdb.close() print "done." Index: myth.sql =================================================================== RCS file: /cvsroot/wepg/mythcond/myth.sql,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** myth.sql 3 Aug 2004 17:15:25 -0000 1.2 --- myth.sql 7 Aug 2004 18:52:52 -0000 1.3 *************** *** 11,16 **** ); ! DROP TABLE IF EXISTS wepgbc; ! CREATE TABLE wepgbc ( user_id int(11) NOT NULL, chanid int(11) NOT NULL, --- 11,16 ---- ); ! DROP TABLE IF EXISTS UserBroadcast; ! CREATE TABLE UserBroadcast ( user_id int(11) NOT NULL, chanid int(11) NOT NULL, *************** *** 22,28 **** rec_id int(11) NOT NULL default '0', rec_status int(11) default '0', PRIMARY KEY (user_id,chanid,starttime), ! KEY wepgbc_archived (archived), ! KEY wepgbc_rec_id (rec_id) ); --- 22,32 ---- rec_id int(11) NOT NULL default '0', rec_status int(11) default '0', + uid varchar(31), + orgtitle varchar(128), + category varchar(63), PRIMARY KEY (user_id,chanid,starttime), ! KEY ub_archived (archived), ! KEY ub_rec_id (rec_id), ! KEY ub_uid (uid) ); |