3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
9 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
17 def __init__(self,dbname,user,passwd,host="localhost",port=3306):
24 con = MySQLdb.connect(user= user, passwd = passwd)
26 cur.execute('CREATE DATABASE '+dbname+" DEFAULT CHARACTER SET utf8")
31 db = self.connect_db()
33 db[1].execute('CREATE TABLE rectime (id INTEGER PRIMARY KEY,type VARCHAR(10),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))')
43 con=MySQLdb.connect(db=self.dbname,host=self.dbhost,port=self.dbport,user=self.dbusr,passwd=self.dbpasswd,charset="utf8")
45 cur.execute('set names utf8;')
47 def close_db(self,db):
51 db = self.connect_db()
53 db[1].execute('drop table chdata')
57 db[1].execute('create table chdata (bctype VARCHAR(15),ontv VARCHAR(30),chtxt VARCHAR(15),ch VARCHAR(10),csch VARCHAR(10),station VARCHAR(20),station_name VARCHAR(30),updatetime DATETIME)')
61 def add_chdata(self,bctype, ontv, chtxt, ch, csch, station, station_name,updatetime):
62 db = self.connect_db()
63 db[1].execute('insert into chdata values (%s,%s,%s,%s,%s,%s,%s,%s)',(bctype,ontv,chtxt,ch,csch,station,station_name,updatetime))
65 def select_by_ontv_chdata(self,ontv):
66 db = self.connect_db()
67 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ontv = %s",(ontv,))
73 def select_by_chtxt_chdata(self,chtxt):
74 db = self.connect_db()
75 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE chtxt = %s",(chtxt,))
81 def select_by_bctype_chdata(self,bctype):
82 db = self.connect_db()
83 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE bctype = %s",(bctype,))
89 def select_by_ch_chdata(self,ch):
90 db = self.connect_db()
91 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata WHERE ch = %s",(ch,))
97 def select_all_chdata(self):
98 db = self.connect_db()
99 dbexe=db[1].execute("SELECT bctype,ontv,chtxt,ch,csch,updatetime FROM chdata ")
105 def select_get_update_chdata(self,dhour):
106 db = self.connect_db()
107 dbexe=db[1].execute("SELECT bctype,chtxt FROM chdata WHERE updatetime < DATE_SUB(now(),INTERVAL "+dhour+" HOUR)")
114 def update_by_bctype_chdata(self,bctype):
115 db = self.connect_db()
116 db[1].execute("UPDATE chdata SET updatetime=now() WHERE bctype = %s",(bctype,))
118 def update_by_bctype_and_chtxt_chdata(self,bctype,chtxt):
119 db = self.connect_db()
120 db[1].execute("UPDATE chdata SET updatetime=now() WHERE bctype = %s AND chtxt = %s",(bctype,chtxt))
122 def add_rectime(self,type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
123 db = self.connect_db()
124 db[1].execute('insert 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))
127 def del_rectime(self,type="", title="", chtxt="", btime=""):
132 db[1].execute("delete from rectime where type = %s AND title = %s AND chtxt = %s AND btime = %s", (type, title, chtxt, btime))
135 def select_all_rectime(self):
138 dbr=db[1].execute("SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt FROM rectime")
140 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db[1].fetchone():
144 ret['title']=title.encode('utf-8')
148 if deltatime == None:
153 ret['deltatime']=deltatime
154 elif typet == 'keyevery':
155 ret['deltatime']=deltatime
156 ret['deltaday']=deltaday
160 def select_bytime_rectime(self,dminutes):
163 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 )")
165 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in db[1].fetchone():
169 ret['title']=title.encode('utf-8')
173 if deltatime == None or deltatime == "":
175 if deltaday == None or deltaday == "":
178 ret['deltatime']=deltatime
179 elif typet == 'keyevery':
180 ret['deltatime']=deltatime
181 ret['deltaday']=deltaday
185 def delete_old_rectime(self,dhour):
187 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))
190 def new_tv(self,bctype):
193 db[1].execute("delete from tv where bctype = %s", (bctype,))
197 db[1].execute("create table tv (bctype VARCHAR(20),channel VARCHAR(20) NOT NULL,start VARCHAR(20),stop VARCHAR(20),title VARCHAR(100),exp VARCHAR(200),UNIQUE uni(bctype,channel,start,stop,title))")
202 def add_tv(self,bctype, channel, start, stop, title, desc):
204 db[1].execute('INSERT IGNORE INTO tv VALUES (%s,%s,%s,%s,%s,%s)', (bctype, channel, start, stop, title, desc))
207 def add_multi_tv(self,tvlists):
209 tvlists is (bctype,channel,start,stop,title,desc) lists.
212 db[1].executemany('INSERT IGNORE INTO tv (bctype,channel,start,stop,title,exp) values(%s,%s,%s,%s,%s,%s)', tvlists)
215 def select_by_title_tv(self,title):
218 dbr=db[1].execute("SELECT channel,title,start,stop FROM tv WHERE title LIKE \"%"+titletxt+"%\"")
220 for ch, title, start, stop in db[1].fetchone():
221 btime=start.replace(" +0900","")
222 btime=btime[0:4]+"-"+btime[4:6]+"-"+btime[6:8]+" "+btime[8:10]+":"+btime[10:12]+":00"
223 etime=stop.replace(" +0900","")
224 etime=etime[0:4]+"-"+etime[4:6]+"-"+etime[6:8]+" "+etime[8:10]+":"+etime[10:12]+":00"
226 chtxt=chdb.ontvsearch(ch)['chtxt']
229 ret.append(chtxt+","+title.encode('utf-8')+","+btime+","+etime)
232 def select_by_time_ngram_tv(self,btime,etime,chtxt):
234 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"
235 dbcmd=db[1].execute(dbexe,(btime,etime,chtxt))
238 retall=db[1].fetchall()
241 def new_ch(self,bctype):
244 db[1].execute("delete from ch where bctype = %s", (bctype,))
248 db[1].execute('create table ch (bctype VARCHAR(20),channel VARCHAR(20) NOT NULL,display VARCHAR(100),UNIQUE uni(bctype,channel))')
253 def add_ch(self,bctype, channel, display):
255 db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
258 def add_multi_ch(self,chlists):
260 chlists is (bctype,channel,display) lists
263 db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)