3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
8 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
9 dbpath = path + "ch.db"
12 def __init__(self,dbpath):
14 db = self.connect_db(480)
16 db.execute('CREATE TABLW rectime (type TEXT,chtxt TEXT,title TEXT,btime TEXT,etime TEXT,deltatime TEXT,deltaday TEXT,opt TEXT,id INTEGER PRIMARY KEY,UNIQUE(type,chtxt,title,btime,deltaday))')
21 def connect_db(self,tout=10):
28 return sqlite3.connect(self.dbpath, timeout=tout)
29 def close_db(self,db):
32 db = self.connect_db(480)
34 db.execute('DROP TABLE chdata')
39 db.execute('CREATE TABLE chdata (bctype TEXT,ontv TEXT,chtxt TEXT,ch TEXT,csch TEXT,station TEXT,station_name TEXT,updatetime TEXT,status INTEGER)')
44 def add_chdata(self,bctype, ontv, chtxt, ch, csch, station, station_name,updatetime):
45 db = self.connect_db(480)
46 db.execute('INSERT OR IGNORE INTO chdata VALUES (?,?,?,?,?,?,?,datetime(?),?)',(bctype,ontv,chtxt,ch,csch,station,station_name,updatetime,"1"))
49 def select_by_ontv_chdata(self,ontv):
50 db = self.connect_db(480)
51 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ontv = ?",(ontv,))
55 def select_by_chtxt_chdata(self,chtxt):
56 db = self.connect_db(480)
57 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE chtxt = ?",(chtxt,))
61 def select_by_bctype_chdata(self,bctype):
62 db = self.connect_db(480)
63 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE bctype = ?",(bctype,))
67 def select_by_ch_chdata(self,ch):
68 db = self.connect_db(480)
69 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ch = ?",(ch,))
73 def select_all_chdata(self):
74 db = self.connect_db(480)
75 dbexe=db.execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata ")
79 def select_get_update_chdata(self,dhour):
80 db = self.connect_db(480)
81 dbexe=db.execute("SELECT bctype,chtxt,status FROM chdata WHERE updatetime < datetime(\'now\',\'localtime\',\'-"+dhour+" hours\') AND status > 0 ORDER BY status DESC")
85 def update_by_bctype_chdata(self,bctype):
86 db = self.connect_db(480)
87 db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') , status = 1 WHERE bctype = ?",(bctype,))
90 def update_by_bctype_and_chtxt_chdata(self,bctype,chtxt):
91 db = self.connect_db(480)
92 db.execute("UPDATE chdata SET updatetime=datetime(\'now\',\'localtime\') , status = 1 WHERE bctype = ? AND chtxt = ?",(bctype,chtxt))
95 def update_status_by_bctype_chdata(self,bctype,status):
96 db = self.connect_db(480)
97 db.execute("UPDATE chdata SET status = ? WHERE bctype = ?",(status,bctype))
100 def add_rectime(self,type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
101 db = self.connect_db(480)
102 db.execute('INSERT OR IGNORE INTO rectime (type,chtxt,title,btime,etime,deltatime,deltaday,opt) values (?,?,?,datetime(?),datetime(?),?,?,?)', (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
105 def del_rectime(self,type="", title="", chtxt="", btime=""):
109 db=self.connect_db(480)
110 db.execute("DELETE FROM rectime where type = ? AND title = ? AND chtxt = ? AND btime = datetime(?)", (type, title, chtxt, btime))
113 def select_all_rectime(self):
114 db=self.connect_db(480)
116 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db.execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime"):
120 ret['title']=title.encode('utf-8')
126 if deltatime == None:
131 ret['deltatime']=deltatime
132 elif typet == 'keyevery':
133 ret['deltatime']=deltatime
134 ret['deltaday']=deltaday
138 def select_bytime_rectime(self,dminutes):
139 db=self.connect_db(480)
141 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db.execute("SELECT type, chtxt, title, DATETIME(btime), DATETIME(etime), deltatime ,deltaday ,opt FROM rectime WHERE btime < datetime(\'now\',\'localtime\',\'+" + dminutes + " minutes\') AND btime >datetime(\'now\',\'localtime\',\'-" + dminutes + " minutes\')"):
145 ret['title']=title.encode('utf-8')
149 if deltatime == None or deltatime == "":
151 if deltaday == None or deltaday == "":
154 ret['deltatime']=deltatime
155 elif typet == 'keyevery':
156 ret['deltatime']=deltatime
157 ret['deltaday']=deltaday
161 def delete_old_rectime(self,dhour):
162 db=self.connect_db(480)
163 db.execute("DELETE FROM rectime WHERE NOT ( type = ? OR type = ? ) AND btime < datetime(\'now\',\'localtime\',\'-" + dhour + " hours\')", (recdb.REC_MISS_ENCODE, recdb.REC_KEYWORD_EVERY_SOME_DAYS))
166 def new_tv(self,bctype):
167 db=self.connect_db(480)
169 db.execute("DELETE FROM tv WHERE bctype = ?", (bctype,))
173 db.execute('CREATE TABLE tv (bctype TEXT,channel TEXT NOT NULL,start TEXT,stop TEXT,title TEXT,exp TEXT, UNIQUE (bctype,channel,start,stop,title,exp))')
178 def add_tv(self,bctype, channel, start, stop, title, exp):
179 db=self.connect_db(480)
180 db.execute('INSERT OR IGNORE INTO tv (bctype,channel,start,stop,title,exp) values (?,?,?,?,?,?)', (bctype, channel, start, stop, title, exp))
183 def add_multi_tv(self,tvlists):
185 tvlists is (bctype,channel,start,stop,title,exp) lists.
187 db=self.connect_db(480)
188 db.executemany('INSERT OR IGNORE INTO tv (bctype,channel,start,stop,title,exp) values (?,?,?,?,?,?)', tvlists)
191 def select_by_time_ngram_tv(self,btime,etime,chtxt):
192 db=self.connect_db(480)
193 dbexe="SELECT chdata.chtxt,title,start,stop FROM tv INNER JOIN chdata WHERE chdata.ontv=tv.channel AND start >= ? AND start <= ? AND chdata.chtxt=?"
194 dbcmd=db.execute(dbexe,(btime,etime,chtxt))
195 retall=dbcmd.fetchall()
198 def new_ch(self,bctype):
199 db=self.connect_db(480)
201 db.execute("DELETE FROM ch WHERE bctype = ?",(bctype,))
205 db.execute('CREATE TABLE ch (bctype TEXT,channel TEXT NOT NULL,display TEXT,UNIQUE (bctype,channel))')
210 def add_ch(self,bctype, channel, display):
211 db=self.connect_db(480)
212 db.execute('INSERT OR IGNORE INTO ch values (?,?,?)', (bctype, channel, display))
215 def add_multi_ch(self,chlists):
217 chlists is (bctype,channel,display) lists
219 db=self.connect_db(480)
220 db.executemany('INSERT OR IGNORE INTO ch values (?,?,?)', chlists)
223 """ def select_by_title_tv(self,title):
224 db=self.connect_db(480)
226 for ch, title, start, stop in db.execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\""):
227 btime=start.replace(" +0900","")
228 btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
229 etime=stop.replace(" +0900","")
230 etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
232 chtxt=chdb.ontvsearch(ch)['chtxt']
235 ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)