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)')
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)',(bctype,ontv,chtxt,ch,csch,station,station_name,updatetime))
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 FROM chdata WHERE updatetime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR)")
113 def update_by_bctype_chdata(self,bctype):
114 db = self.connect_db()
115 db[1].execute("UPDATE chdata SET updatetime=now() 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() WHERE bctype = %s AND chtxt = %s",(bctype,chtxt))
121 def add_rectime(self,type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
122 db = self.connect_db()
123 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))
126 def del_rectime(self,type="", title="", chtxt="", btime=""):
131 db[1].execute("delete from rectime where type = %s AND title = %s AND chtxt = %s AND btime = %s", (type, title, chtxt, btime))
134 def select_all_rectime(self):
137 dbr=db[1].execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime")
141 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
145 ret['title']=title.encode('utf-8')
146 btime=btime.strftime("%Y-%m-%d %H:%M:%S")
147 etime=etime.strftime("%Y-%m-%d %H:%M:%S")
153 if deltatime == None:
158 ret['deltatime']=deltatime
159 elif typet == 'keyevery':
160 ret['deltatime']=deltatime
161 ret['deltaday']=deltaday
165 def select_bytime_rectime(self,dminutes):
168 #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 )")
169 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 )")
174 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
178 ret['title']=title.encode('utf-8')
179 btime=btime.strftime("%Y-%m-%d %H:%M:%S")
180 etime=etime.strftime("%Y-%m-%d %H:%M:%S")
184 if deltatime == None or deltatime == "":
186 if deltaday == None or deltaday == "":
189 ret['deltatime']=deltatime
190 elif typet == 'keyevery':
191 ret['deltatime']=deltatime
192 ret['deltaday']=deltaday
195 def delete_old_rectime(self,dhour):
197 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))
200 def new_tv(self,bctype):
203 db[1].execute("delete from tv where bctype = %s", (bctype,))
207 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))")
212 def add_tv(self,bctype, channel, start, stop, title, desc):
214 db[1].execute('INSERT IGNORE INTO tv VALUES (%s,%s,%s,%s,%s,%s)', (bctype, channel, start, stop, title, desc))
217 def add_multi_tv(self,tvlists):
219 tvlists is (bctype,channel,start,stop,title,desc) lists.
222 db[1].executemany('INSERT IGNORE INTO tv (bctype,channel,start,stop,title,exp) values(%s,%s,%s,%s,%s,%s)', tvlists)
225 def select_by_title_tv(self,title):
228 dbr=db[1].execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\"")
232 for ch, title, start, stop in dbl:
233 btime=start.replace(" +0900","")
234 btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
235 etime=stop.replace(" +0900","")
236 etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
238 chtxt=chdb.ontvsearch(ch)['chtxt']
241 ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
243 def select_by_time_ngram_tv(self,btime,etime,chtxt):
245 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"
246 dbcmd=db[1].execute(dbexe,(btime,etime,chtxt))
249 retall=db[1].fetchall()
252 def new_ch(self,bctype):
255 db[1].execute("delete from ch where bctype = %s", (bctype,))
259 db[1].execute('create table ch (bctype VARCHAR(20),channel VARCHAR(20) NOT NULL,display VARCHAR(100),UNIQUE unich(bctype,channel))')
264 def add_ch(self,bctype, channel, display):
266 db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
269 def add_multi_ch(self,chlists):
271 chlists is (bctype,channel,display) lists
274 db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
276 def import_from_SQLite(self,dbpath):
277 dbb=dbSQLite.DB_SQLite(dbpath)
278 dbl=dbb.select_all_rectime()
280 self.add_rectime(db['type'],db['chtxt'],db['title'],db['btime'],db['etime'],db['deltatime'],db['deltaday'],db['opt'])