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,\
77 station_name VARCHAR(30),\
84 def add_chdata(self,bctype, ontv, chtxt, ch, csch, station, station_name,updatetime):
85 db = self.connect_db()
88 VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)',\
89 (bctype,ontv,chtxt,ch,csch,station,station_name,updatetime,"1"))
91 def select_by_ontv_chdata(self,ontv):
92 db = self.connect_db()
93 dbexe=db[1].execute("\
94 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
105 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
108 def select_by_chtxt_chdata(self,chtxt):
109 db = self.connect_db()
110 dbexe=db[1].execute("\
111 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
122 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
125 def select_by_bctype_chdata(self,bctype):
126 db = self.connect_db()
127 dbexe=db[1].execute("\
128 SELECT bctype,ontv,chtxt,ch,csch,updatetime,status \
140 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
144 def select_by_ch_chdata(self,ch):
145 db = self.connect_db()
146 dbexe=db[1].execute("\
148 bctype,ontv,chtxt,ch,csch,updatetime \
159 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
162 def select_all_chdata(self):
163 db = self.connect_db()
164 dbexe=db[1].execute("\
165 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
175 r[5]=r[5].strftime("%Y-%m-%d %H:%M:%S")
178 def select_get_update_chdata(self,dhour):
179 db = self.connect_db()
180 dbexe=db[1].execute("SELECT bctype,chtxt,status FROM chdata \
183 updatetime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR) \
189 ORDER BY status DESC")
196 def update_by_bctype_chdata(self,bctype):
197 db = self.connect_db()
203 WHERE bctype = %s",(bctype,))
205 def update_by_bctype_and_chtxt_chdata(self,bctype,chtxt):
206 db = self.connect_db()
212 WHERE bctype = %s AND chtxt = %s",(bctype,chtxt))
214 def update_status_by_bctype_chdata(self,bctype,status):
215 db = self.connect_db()
224 def add_rectime(self,type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
225 db = self.connect_db()
227 INSERT IGNORE into rectime \
228 (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
229 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
230 (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
233 def del_rectime(self,type="", title="", chtxt="", btime=""):
239 DELETE FROM rectime \
240 WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
241 (type, title, chtxt, btime))
244 def select_all_rectime(self):
248 SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
253 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
257 ret['title']=title.encode('utf-8')
258 btime=btime.strftime("%Y-%m-%d %H:%M:%S")
259 etime=etime.strftime("%Y-%m-%d %H:%M:%S")
265 if deltatime == None:
270 ret['deltatime']=deltatime
271 elif typet == 'keyevery':
272 ret['deltatime']=deltatime
273 ret['deltaday']=deltaday
277 def select_bytime_rectime(self,dminutes):
280 #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 )")
281 dbr=db[1].execute("SELECT \
282 type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
284 WHERE btime BETWEEN DATE_SUB(now(),INTERVAL "+dminutes+" MINUTE ) AND \
285 DATE_ADD(now(),INTERVAL "+dminutes+" MINUTE )")
290 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
294 ret['title']=title.encode('utf-8')
295 btime=btime.strftime("%Y-%m-%d %H:%M:%S")
296 etime=etime.strftime("%Y-%m-%d %H:%M:%S")
300 if deltatime == None or deltatime == "":
302 if deltaday == None or deltaday == "":
305 ret['deltatime']=deltatime
306 elif typet == 'keyevery':
307 ret['deltatime']=deltatime
308 ret['deltaday']=deltaday
311 def delete_old_rectime(self,dhour):
314 DELETE FROM rectime \
316 NOT ( type = %s OR type = %s ) \
318 btime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR )", \
319 (recdb.REC_MISS_ENCODE, recdb.REC_KEYWORD_EVERY_SOME_DAYS))
322 def new_tv(self,bctype):
327 WHERE bctype = %s", \
336 channel VARCHAR(100) NOT NULL,\
341 UNIQUE unitv(bctype,channel,start,stop,title)\
347 def add_tv(self,bctype, channel, start, stop, title, desc):
350 INSERT IGNORE INTO tv \
351 VALUES (%s,%s,%s,%s,%s,%s)', \
352 (bctype, channel, start, stop, title, desc))
355 def add_multi_tv(self,tvlists):
357 tvlists is (bctype,channel,start,stop,title,desc) lists.
361 INSERT IGNORE INTO tv \
362 (bctype,channel,start,stop,title,exp) \
363 values(%s,%s,%s,%s,%s,%s)', \
369 def select_by_time_ngram_tv(self,btime,etime,chtxt):
373 chdata.chtxt,title,start,stop \
376 WHERE chdata.ontv=tv.channel \
383 dbcmd=db[1].execute(dbexe,(btime,etime,chtxt))
386 retall=db[1].fetchall()
389 def new_ch(self,bctype):
392 db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
400 channel VARCHAR(20) NOT NULL,\
401 display VARCHAR(100),\
402 UNIQUE unich(bctype,channel)\
408 def add_ch(self,bctype, channel, display):
410 db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
413 def add_multi_ch(self,chlists):
415 chlists is (bctype,channel,display) lists
418 db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
420 def import_from_SQLite(self,dbpath):
421 dbb=dbSQLite.DB_SQLite(dbpath)
422 dbl=dbb.select_all_rectime()
424 self.add_rectime(db['type'],db['chtxt'],db['title'],db['btime'],db['etime'],db['deltatime'],db['deltaday'],db['opt'])