3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
16 def __init__(self,dbname,user,passwd,host="localhost",port=3306):
23 con = MySQLdb.connect(user= user, passwd = passwd)
25 cur.execute('CREATE DATABASE '+dbname+" DEFAULT CHARACTER SET utf8")
30 db = self.connect_db()
32 db[1].execute('CREATE TABLE rectime (id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,type VARCHAR(20),chtxt VARCHAR(20),title VARCHAR(100),btime DATETIME,etime DATETIME,deltatime VARCHAR(5),deltaday VARCHAR(5),opt VARCHAR(20),UNIQUE uni (type,chtxt,title,btime,deltaday))')
42 con=MySQLdb.connect(db=self.dbname,host=self.dbhost,port=self.dbport,user=self.dbusr,passwd=self.dbpasswd,charset="utf8")
44 cur.execute('set names utf8;')
46 def close_db(self,db):
50 db = self.connect_db()
52 db[1].execute('drop table chdata')
56 db[1].execute('create table chdata (bctype VARCHAR(15),ontv VARCHAR(30) PRIMARY KEY,chtxt VARCHAR(15),ch VARCHAR(20),csch VARCHAR(20),station VARCHAR(20),station_name VARCHAR(30),updatetime DATETIME,status TINYINT)')
60 def add_chdata(self,bctype, ontv, chtxt, ch, csch, station, station_name,updatetime):
61 db = self.connect_db()
62 db[1].execute('insert into chdata values (%s,%s,%s,%s,%s,%s,%s,%s,%s)',(bctype,ontv,chtxt,ch,csch,station,station_name,updatetime,"1"))
64 def select_by_ontv_chdata(self,ontv):
65 db = self.connect_db()
66 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ontv = %s",(ontv,))
72 def select_by_chtxt_chdata(self,chtxt):
73 db = self.connect_db()
74 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE chtxt = %s",(chtxt,))
80 def select_by_bctype_chdata(self,bctype):
81 db = self.connect_db()
82 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE bctype = %s",(bctype,))
88 def select_by_ch_chdata(self,ch):
89 db = self.connect_db()
90 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ch = %s",(ch,))
96 def select_all_chdata(self):
97 db = self.connect_db()
98 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata ")
104 def select_get_update_chdata(self,dhour):
105 db = self.connect_db()
106 dbexe=db[1].execute("SELECT bctype,chtxt,status FROM chdata WHERE updatetime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR) AND status > 0 ORDER BY status DESC")
113 def update_by_bctype_chdata(self,bctype):
114 db = self.connect_db()
115 db[1].execute("UPDATE chdata SET updatetime=now() , status = 1 WHERE bctype = %s",(bctype,))
117 def update_by_bctype_and_chtxt_chdata(self,bctype,chtxt):
118 db = self.connect_db()
119 db[1].execute("UPDATE chdata SET updatetime=now() , status = 1WHERE bctype = %s AND chtxt = %s",(bctype,chtxt))
121 def update_status_by_bctype_chdata(self,bctype,status):
122 db = self.connect_db()
123 db[1].execute("UPDATE chdata SET status=%s WHERE bctype = %s",(status,bctype))
125 def add_rectime(self,type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
126 db = self.connect_db()
127 db[1].execute('INSERT IGNORE into rectime (type,chtxt,title,btime,etime,deltatime,deltaday,opt) values (%s,%s,%s,%s,%s,%s,%s,%s)', (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
130 def del_rectime(self,type="", title="", chtxt="", btime=""):
135 db[1].execute("delete from rectime where type = %s AND title = %s AND chtxt = %s AND btime = %s", (type, title, chtxt, btime))
138 def select_all_rectime(self):
141 dbr=db[1].execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime")
145 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
149 ret['title']=title.encode('utf-8')
150 btime=btime.strftime("%Y-%m-%d %H:%M:%S")
151 etime=etime.strftime("%Y-%m-%d %H:%M:%S")
157 if deltatime == None:
162 ret['deltatime']=deltatime
163 elif typet == 'keyevery':
164 ret['deltatime']=deltatime
165 ret['deltaday']=deltaday
169 def select_bytime_rectime(self,dminutes):
172 #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 )")
173 dbr=db[1].execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime WHERE btime BETWEEN DATE_SUB(now(),INTERVAL "+dminutes+" MINUTE ) AND DATE_ADD(now(),INTERVAL "+dminutes+" MINUTE )")
178 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
182 ret['title']=title.encode('utf-8')
183 btime=btime.strftime("%Y-%m-%d %H:%M:%S")
184 etime=etime.strftime("%Y-%m-%d %H:%M:%S")
188 if deltatime == None or deltatime == "":
190 if deltaday == None or deltaday == "":
193 ret['deltatime']=deltatime
194 elif typet == 'keyevery':
195 ret['deltatime']=deltatime
196 ret['deltaday']=deltaday
199 def delete_old_rectime(self,dhour):
201 db[1].execute("DELETE FROM rectime WHERE NOT ( type = %s OR type = %s ) AND btime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR )", (recdb.REC_MISS_ENCODE, recdb.REC_KEYWORD_EVERY_SOME_DAYS))
204 def new_tv(self,bctype):
207 db[1].execute("delete from tv where bctype = %s", (bctype,))
211 db[1].execute("create table tv (bctype VARCHAR(20),channel VARCHAR(100) NOT NULL,start VARCHAR(30),stop VARCHAR(30),title VARCHAR(100),exp VARCHAR(200),UNIQUE unitv(bctype,channel,start,stop,title))")
216 def add_tv(self,bctype, channel, start, stop, title, desc):
218 db[1].execute('INSERT IGNORE INTO tv VALUES (%s,%s,%s,%s,%s,%s)', (bctype, channel, start, stop, title, desc))
221 def add_multi_tv(self,tvlists):
223 tvlists is (bctype,channel,start,stop,title,desc) lists.
226 db[1].executemany('INSERT IGNORE INTO tv (bctype,channel,start,stop,title,exp) values(%s,%s,%s,%s,%s,%s)', tvlists)
231 def select_by_time_ngram_tv(self,btime,etime,chtxt):
233 dbexe="SELECT chdata.chtxt,title,start,stop FROM tv INNER JOIN chdata WHERE chdata.ontv=tv.channel AND start >= %s AND start <= %s AND chdata.chtxt=%s"
234 dbcmd=db[1].execute(dbexe,(btime,etime,chtxt))
237 retall=db[1].fetchall()
240 def new_ch(self,bctype):
243 db[1].execute("delete from ch where bctype = %s", (bctype,))
247 db[1].execute('create table ch (bctype VARCHAR(20),channel VARCHAR(20) NOT NULL,display VARCHAR(100),UNIQUE unich(bctype,channel))')
252 def add_ch(self,bctype, channel, display):
254 db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
257 def add_multi_ch(self,chlists):
259 chlists is (bctype,channel,display) lists
262 db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
264 def import_from_SQLite(self,dbpath):
265 dbb=dbSQLite.DB_SQLite(dbpath)
266 dbl=dbb.select_all_rectime()
268 self.add_rectime(db['type'],db['chtxt'],db['title'],db['btime'],db['etime'],db['deltatime'],db['deltaday'],db['opt'])
270 """ def select_by_title_tv(self,title):
273 dbr=db[1].execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\"")
277 for ch, title, start, stop in dbl:
278 btime=start.replace(" +0900","")
279 btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
280 etime=stop.replace(" +0900","")
281 etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
283 chtxt=chdb.ontvsearch(ch)['chtxt']
286 ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)