3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
15 def __init__(self,dbname,user,passwd,host="localhost",port=3306):
22 con = MySQLdb.connect(user= user, passwd = passwd)
24 cur.execute('CREATE DATABASE '+dbname+" DEFAULT CHARACTER SET utf8")
29 db = self.connect_db()
32 CREATE TABLE rectime \
34 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
40 deltatime VARCHAR(5),\
43 UNIQUE uni (type,chtxt,title,btime,deltaday)\
54 con=MySQLdb.connect(db=self.dbname,host=self.dbhost,port=self.dbport,user=self.dbusr,passwd=self.dbpasswd,charset="utf8")
56 cur.execute('set names utf8;')
58 def close_db(self,db):
62 db = self.connect_db()
64 db[1].execute('drop table chdata')
72 ontv VARCHAR(30) PRIMARY KEY,\
82 def add_chdata(self,bctype, ontv, chtxt, ch, csch,updatetime):
83 db = self.connect_db()
86 VALUES (%s,%s,%s,%s,%s,%s,%s)',\
87 (bctype,ontv,chtxt,ch,csch,updatetime,"1"))
89 def select_by_ontv_chdata(self,ontv):
90 db = self.connect_db()
91 dbexe=db[1].execute("\
92 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
103 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
106 def select_by_chtxt_chdata(self,chtxt):
107 db = self.connect_db()
108 dbexe=db[1].execute("\
109 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
120 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
123 def select_by_bctype_chdata(self,bctype):
124 db = self.connect_db()
125 dbexe=db[1].execute("\
126 SELECT bctype,ontv,chtxt,ch,csch,updatetime,status \
138 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
142 def select_by_ch_chdata(self,ch):
143 db = self.connect_db()
144 dbexe=db[1].execute("\
146 bctype,ontv,chtxt,ch,csch,updatetime \
157 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
160 def select_all_chdata(self):
161 db = self.connect_db()
162 dbexe=db[1].execute("\
163 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
173 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
176 def select_get_update_chdata(self,dhour):
177 db = self.connect_db()
178 dbexe=db[1].execute("SELECT bctype,chtxt,status FROM chdata \
181 updatetime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR) \
187 ORDER BY status DESC")
194 def update_by_bctype_chdata(self,bctype):
195 db = self.connect_db()
201 WHERE bctype = %s",(bctype,))
203 def update_by_bctype_and_chtxt_chdata(self,bctype,chtxt):
204 db = self.connect_db()
210 WHERE bctype = %s AND chtxt = %s",(bctype,chtxt))
212 def update_status_by_bctype_chdata(self,bctype,status):
213 db = self.connect_db()
222 def add_rectime(self,type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
223 db = self.connect_db()
225 INSERT IGNORE into rectime \
226 (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
227 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
228 (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
231 def del_rectime(self,type="", title="", chtxt="", btime=""):
237 DELETE FROM rectime \
238 WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
239 (type, title, chtxt, btime))
242 def select_all_rectime(self):
246 SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
251 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
255 ret['title']=title.encode('utf-8')
256 btime=btime.strftime("%Y-%m-%d %H:%M:%S")
257 etime=etime.strftime("%Y-%m-%d %H:%M:%S")
263 if deltatime == None:
268 ret['deltatime']=deltatime
269 elif typet == 'keyevery':
270 ret['deltatime']=deltatime
271 ret['deltaday']=deltaday
275 def select_bytime_rectime(self,dminutes):
278 #dbr=db[1].execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime WHERE btime < DATE_SUB(now(),INTERVAL "+dminutes+" MINUTE ) AND btime > DATE_ADD(now(),INTERVAL "+dminutes+" MINUTE )")
279 dbr=db[1].execute("SELECT \
280 type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
282 WHERE btime BETWEEN DATE_SUB(now(),INTERVAL "+dminutes+" MINUTE ) AND \
283 DATE_ADD(now(),INTERVAL "+dminutes+" MINUTE )")
288 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
292 ret['title']=title.encode('utf-8')
293 btime=btime.strftime("%Y-%m-%d %H:%M:%S")
294 etime=etime.strftime("%Y-%m-%d %H:%M:%S")
298 if deltatime == None or deltatime == "":
300 if deltaday == None or deltaday == "":
303 ret['deltatime']=deltatime
304 elif typet == 'keyevery':
305 ret['deltatime']=deltatime
306 ret['deltaday']=deltaday
309 def delete_old_rectime(self,dhour):
312 DELETE FROM rectime \
314 NOT ( type = %s OR type = %s ) \
316 btime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR )", \
317 (recdb.REC_MISS_ENCODE, recdb.REC_KEYWORD_EVERY_SOME_DAYS))
320 def new_tv(self,bctype):
325 WHERE bctype = %s", \
334 channel VARCHAR(100) NOT NULL,\
339 UNIQUE unitv(bctype,channel,start,stop,title)\
345 def add_tv(self,bctype, channel, start, stop, title, desc):
348 INSERT IGNORE INTO tv \
349 VALUES (%s,%s,%s,%s,%s,%s)', \
350 (bctype, channel, start, stop, title, desc))
353 def add_multi_tv(self,tvlists):
355 tvlists is (bctype,channel,start,stop,title,desc) lists.
359 INSERT IGNORE INTO tv \
360 (bctype,channel,start,stop,title,exp) \
361 values(%s,%s,%s,%s,%s,%s)', \
367 def select_by_time_ngram_tv(self,btime,etime,chtxt):
371 chdata.chtxt,title,start,stop \
374 WHERE chdata.ontv=tv.channel \
381 dbcmd=db[1].execute(dbexe,(btime,etime,chtxt))
384 retall=db[1].fetchall()
387 def new_ch(self,bctype):
390 db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
398 channel VARCHAR(20) NOT NULL,\
399 display VARCHAR(100),\
400 UNIQUE unich(bctype,channel)\
406 def add_ch(self,bctype, channel, display):
408 db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
411 def add_multi_ch(self,chlists):
413 chlists is (bctype,channel,display) lists
416 db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
418 def import_from_SQLite(self,dbpath):
419 dbb=dbSQLite.DB_SQLite(dbpath)
420 dbl=dbb.select_all_rectime()
422 self.add_rectime(db['type'],db['chtxt'],db['title'],db['btime'],db['etime'],db['deltatime'],db['deltaday'],db['opt'])