4 # Rec10 TS Recording Tools
5 # Copyright (C) 2009 Yukikaze
11 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
18 def __init__(self,dbname,host="localhost",user,passwd,port=3306):
24 db = self.connect_db(self.dbname,self.dbhost,self.dbport,self.dbusr,self.dbpasswd)
26 db.execute('create table rectime (type TEXT,chtxt TEXT,title TEXT,btime DATETIME,etime DATETIME,deltatime TEXT,deltaday TEXT,opt TEXT,id INTEGER PRIMARY KEY,UNIQUE unique (type,chtxt,title,btime,deltaday))')
31 def connect_db(self,db,host,port,user,passwd):
36 return MySQLdb.connect(db=self.dbname,host=dbhost,port=dbport,user=self.dbusr,passwd=self.dbpasswd)
37 def close_db(self,db):
40 db = self.connect_db(60)
42 db.execute('drop table chdata')
47 db.execute('create table chdata (bctype TEXT,ontv TEXT,chtxt TEXT,ch TEXT,csch TEXT,station TEXT,station_name TEXT,updatetime DATETIME)')
52 def add_chdata(self,bctype, ontv, chtxt, ch, csch, station, station_name,updatetime):
53 db = self.connect_db(60)
54 db.execute('insert into chdata values (?,?,?,?,?,?,?,?)',(bctype,ontv,chtxt,ch,csch,station,station_name,updatetime))
57 def select_by_ontv_chdata(self,ontv):
58 db = self.connect_db(60)
59 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ontv = ?",(ontv,))
63 def select_by_chtxt_chdata(self,chtxt):
64 db = self.connect_db(60)
65 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE chtxt = ?",(chtxt,))
69 def select_by_bctype_chdata(self,bctype):
70 db = self.connect_db(60)
71 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE bctype = ?",(bctype,))
75 def select_by_ch_chdata(self,ch):
76 db = self.connect_db(60)
77 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ch = ?",(ch,))
81 def select_all_chdata(self):
82 db = self.connect_db(60)
83 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata ")
87 def select_get_update_chdata(self,dhour):
88 db = self.connect_db(60)
89 dbexe=db.execute("SELECT bctype,chtxt FROM chdata WHERE updatetime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR)")
93 def update_by_bctype_chdata(self,bctype):
94 db = self.connect_db(60)
95 db.execute("UPDATE chdata SET updatetime=now() WHERE bctype = ?",(bctype,))
98 def update_by_bctype_and_chtxt_chdata(self,bctype,chtxt):
99 db = self.connect_db(60)
100 db.execute("UPDATE chdata SET updatetime=now() WHERE bctype = ? AND chtxt = ?",(bctype,chtxt))
103 def add_rectime(self,type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
104 db = self.connect_db(60)
105 db.execute('insert into rectime (type,chtxt,title,btime,etime,deltatime,deltaday,opt) values (?,?,?,?,?,?,?,?)', (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
108 def del_rectime(self,type="", title="", chtxt="", btime=""):
112 db=self.connect_db(60)
113 db.execute("delete from rectime where type = ? AND title = ? AND chtxt = ? AND btime = ?", (type, title, chtxt, btime))
116 def select_all_rectime(self):
117 db=self.connect_db(60)
119 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db.execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime"):
123 ret['title']=title.encode('utf-8')
127 if deltatime == None:
132 ret['deltatime']=deltatime
133 elif typet == 'keyevery':
134 ret['deltatime']=deltatime
135 ret['deltaday']=deltaday
139 def select_bytime_rectime(self,dhour):
140 db=self.connect_db(60)
142 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db.execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime WHERE btime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR ) AND btime > DATE_ADD(now(),INTERVAL "+dhour+" HOUR )"):
146 ret['title']=title.encode('utf-8')
150 if deltatime == None or deltatime == "":
152 if deltaday == None or deltaday == "":
155 ret['deltatime']=deltatime
156 elif typet == 'keyevery':
157 ret['deltatime']=deltatime
158 ret['deltaday']=deltaday
162 def delete_old_rectime(self,dhour):
163 db=self.connect_db(60)
164 db.execute("DELETE FROM rectime WHERE NOT ( type = ? OR type = ? ) AND btime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR )", (recdb.REC_MISS_ENCODE, recdb.REC_KEYWORD_EVERY_SOME_DAYS))
167 def new_tv(self,bctype):
168 db=self.connect_db(60)
170 db.execute("delete from tv where bctype = ?", (bctype,))
174 db.execute('create table tv (bctype TEXT,channel TEXT NOT NULL,start TEXT,stop TEXT,title TEXT,desc TEXT)')
179 def add_tv(self,bctype, channel, start, stop, title, desc):
180 db=self.connect_db(240)
181 db.execute('insert into tv values (?,?,?,?,?,?)', (bctype, channel, start, stop, title, desc))
184 def add_multi_tv(self,tvlists):
186 tvlists is (bctype,channel,start,stop,title,desc) lists.
188 db=self.connect_db(240)
189 db.executemany('insert into tv values (?,?,?,?,?,?)', tvlists)
192 def select_by_title_tv(self,title):
193 db=self.connect_db(120)
195 for ch, title, start, stop in db.execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\""):
196 btime=start.replace(" +0900","")
197 btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
198 etime=stop.replace(" +0900","")
199 etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
201 chtxt=chdb.ontvsearch(ch)['chtxt']
204 ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
207 def select_by_time_ngram_tv(self,btime,etime,chtxt):
208 db=self.connect_db(120)
209 dbexe="SELECT chdata.chtxt,title,start,stop FROM tv INNER JOIN chdata WHERE chdata.ontv=tv.channel AND start >= ? AND start <= ? AND chdata.chtxt=?"
210 dbcmd=db.execute(dbexe,(btime,etime,chtxt))
211 retall=dbcmd.fetchall()
214 def new_ch(self,bctype):
215 db=self.connect_db(120)
217 db.execute("delete from ch where bctype = ?", (bctype,))
221 db.execute('create table ch (bctype TEXT,channel TEXT NOT NULL,display TEXT)')
226 def add_ch(self,bctype, channel, display):
227 db=self.connect_db(240)
228 db.execute('insert into ch values (?,?,?)', (bctype, channel, display))
231 def add_multi_ch(self,chlists):
233 chlists is (bctype,channel,display) lists
235 db=self.connect_db(240)
236 db.executemany('insert into ch values (?,?,?)', chlists)