3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
13 def __init__(self, dbname, user, passwd, host="localhost", port=3306):
17 self.dbpasswd = passwd
20 con = MySQLdb.connect(user=user, passwd=passwd)
22 cur.execute('CREATE DATABASE ' + dbname + " DEFAULT CHARACTER SET utf8")
27 db = self.connect_db()
30 CREATE TABLE rectime \
32 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
38 deltatime VARCHAR(5),\
41 UNIQUE uni (type,chtxt,title,btime,deltaday)\
53 con = MySQLdb.connect(db=self.dbname, host=self.dbhost, port=self.dbport, user=self.dbusr, passwd=self.dbpasswd, charset="utf8")
55 cur.execute('set names utf8;')
57 def close_db(self, db):
61 db = self.connect_db()
63 db[1].execute('drop table chdata')
71 ontv VARCHAR(30) PRIMARY KEY,\
81 def add_chdata(self, bctype, ontv, chtxt, ch, csch, updatetime):
82 db = self.connect_db()
85 VALUES (%s,%s,%s,%s,%s,%s,%s)', \
86 (bctype, ontv, chtxt, ch, csch, updatetime, "1"))
88 def select_by_ontv_chdata(self, ontv):
89 db = self.connect_db()
90 dbexe = db[1].execute("\
91 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
98 dls = db[1].fetchall()
102 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
105 def select_by_chtxt_chdata(self, chtxt):
106 db = self.connect_db()
107 dbexe = db[1].execute("\
108 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
115 dls = db[1].fetchall()
119 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
122 def select_by_bctype_chdata(self, bctype):
123 db = self.connect_db()
124 dbexe = db[1].execute("\
125 SELECT bctype,ontv,chtxt,ch,csch,updatetime,status \
127 WHERE bctype = %s", \
132 dls = db[1].fetchall()
137 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
141 def select_by_ch_chdata(self, ch):
142 db = self.connect_db()
143 dbexe = db[1].execute("\
145 bctype,ontv,chtxt,ch,csch,updatetime \
152 dls = db[1].fetchall()
156 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
159 def select_all_chdata(self):
160 db = self.connect_db()
161 dbexe = db[1].execute("\
162 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
168 dls = db[1].fetchall()
172 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
175 def select_get_update_chdata(self, dhour):
176 db = self.connect_db()
177 dbexe = db[1].execute("SELECT bctype,chtxt,status FROM chdata \
180 updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
186 ORDER BY status DESC")
190 ret = db[1].fetchall()
193 def update_by_bctype_chdata(self, bctype):
194 db = self.connect_db()
200 WHERE bctype = %s", (bctype,))
202 def update_by_bctype_and_chtxt_chdata(self, bctype, chtxt):
203 db = self.connect_db()
209 WHERE bctype = %s AND chtxt = %s", (bctype, chtxt))
211 def update_status_by_bctype_chdata(self, bctype, status):
212 db = self.connect_db()
217 WHERE bctype = %s", \
221 def add_reclog(self , chtxt="", title="", btime="", etime="", opt="", exp="", longexp="", category=""):
222 db = self.connect_db()
224 INSERT IGNORE into reclog \
225 (chtxt,title,btime,etime,opt,exp,longexp,category) \
226 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
227 ( chtxt, title, btime, etime, opt,exp,longexp,category))
230 def del_reclog(self, title="", chtxt="", btime=""):
234 db = self.connect_db()
237 WHERE title = %s AND chtxt = %s AND btime = %s", \
238 (title, chtxt, btime))
241 def new_reclog(self):
242 db = self.connect_db()
245 CREATE TABLE reclog \
247 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
255 category VARCHAR(100),\
256 UNIQUE uni (chtxt,title,btime,category)\
261 def add_rectime(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
262 db = self.connect_db()
264 INSERT IGNORE into rectime \
265 (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
266 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
267 (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
270 def del_rectime(self, type="", title="", chtxt="", btime=""):
274 db = self.connect_db()
276 DELETE FROM rectime \
277 WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
278 (type, title, chtxt, btime))
281 def select_all_rectime(self):
282 db = self.connect_db()
284 dbr = db[1].execute("\
285 SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
287 dbl = db[1].fetchall()
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 ret['deltatime'] = ""
302 if deltatime == None:
307 ret['deltatime'] = deltatime
308 elif typet == 'keyevery':
309 ret['deltatime'] = deltatime
310 ret['deltaday'] = deltaday
314 def select_bytime_rectime(self, dminutes):
315 db = self.connect_db()
317 #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 )")
318 dbr = db[1].execute("SELECT \
319 type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
321 WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
322 DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
323 dbl = db[1].fetchall()
327 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
331 ret['title'] = title.encode('utf-8')
332 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
333 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
337 if deltatime == None or deltatime == "":
339 if deltaday == None or deltaday == "":
342 ret['deltatime'] = deltatime
343 elif typet == 'keyevery':
344 ret['deltatime'] = deltatime
345 ret['deltaday'] = deltaday
348 def delete_old_rectime(self, dhour):
349 db = self.connect_db()
351 DELETE FROM rectime \
353 btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
356 def new_status(self):
357 db = self.connect_db()
360 CREATE TABLE status \
362 ts2avi TINYINT DEFAULT 0,\
363 terec TINYINT DEFAULT 0,\
364 bscsrec TINYINT DEFAULT 0,\
365 b252ts TINYINT DEFAULT 0\
367 db[1].execute("INSERT IGNORE into status VALUE (0,0,0,0)")
372 def select_all_status(self):
373 db = self.connect_db()
374 dbexe = db[1].execute("\
375 SELECT ts2avi,terec,bscsrec,b252ts \
381 dls = db[1].fetchall()
392 def change_ts2avi_status(self,i):
397 db = self.connect_db()
399 UPDATE status SET ts2avi=ts2avi+%s",i)
401 def change_terec_status(self,i):
406 db = self.connect_db()
408 UPDATE status SET terec=terec+%s",i)
410 def change_bscsrec_status(self,i):
415 db = self.connect_db()
417 UPDATE status SET bscsrec=bscsrec+%s",i)
419 def change_b252ts_status(self,i):
424 db = self.connect_db()
426 UPDATE status SET b252ts=b252ts+%s",i)
428 def new_tv(self, bctype):
429 db = self.connect_db()
433 WHERE bctype = %s", \
442 channel VARCHAR(100) NOT NULL,\
448 category VARCHAR(100),\
449 UNIQUE unitv(bctype,channel,start,stop,title)\
455 def add_tv(self, bctype, channel, start, stop, title, desc,longdesc, category):
456 db = self.connect_db()
458 INSERT IGNORE INTO tv \
459 VALUES (%s,%s,%s,%s,%s,%s,%s,%s)', \
460 (bctype, channel, start, stop, title, desc,longdesc,category))
463 def add_multi_tv(self, tvlists):
465 tvlists is (bctype,channel,start,stop,title,desc,longdesc,category) lists.
467 db = self.connect_db()
469 INSERT IGNORE INTO tv \
470 (bctype,channel,start,stop,title,exp,longexp,category) \
471 values(%s,%s,%s,%s,%s,%s,%s,%s)', \
475 def select_by_time_ngram_tv(self, btime, etime, chtxt):
476 db = self.connect_db()
479 chdata.chtxt,title,start,stop,exp,longexp,category \
482 WHERE chdata.ontv=tv.channel \
489 dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
492 retall = db[1].fetchall()
495 def new_ch(self, bctype):
496 db = self.connect_db()
498 db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
506 channel VARCHAR(20) NOT NULL,\
507 display VARCHAR(100),\
508 UNIQUE unich(bctype,channel)\
514 def add_ch(self, bctype, channel, display):
515 db = self.connect_db()
516 db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
519 def add_multi_ch(self, chlists):
521 chlists is (bctype,channel,display) lists
523 db = self.connect_db()
524 db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
526 def new_bayeskey(self):
527 db = self.connect_db()
530 CREATE TABLE bayeskey \
534 ratio_rec DECIMAL(24,8),\
535 ratio_all DECIMAL(24,8),\
540 def add_bayeskey(self,key,chtxt,ratio_rec,ratio_all):
541 db = self.connect_db()
543 INSERT IGNORE INTO bayeskey \
544 (key,chtxt,ratio_rec,ratio_all) \
545 values(%s,%s,%s,%s)',\
546 (key,chtxt,ratio_rec,ratio_all))
548 def change_bayeskey(self,key,chtxt,new_ratio_rec,new_ratio_all):
553 db = self.connect_db()
555 UPDATE bayeskey SET ratio_rec=%s,ratio_all=%s WHERE key=%s AND chtxt=%s",(new_ratio_rec,new_ratio_all,key,chtxt)\
558 def change_ratio_all_bayeskey(self,chtxt,beforenum,newnum):
559 db = self.connect_db()
561 UPDATE bayeskey SET ratio_all=ratio_all*%s/(%s+%s) WHERE chtxt=%s",(beforenum,newnum,beforenum,chtxt)\
564 def change_ratio_rec_bayeskey(self,key,chtxt,beforenum,addnum):
565 db = self.connect_db()
567 UPDATE bayeskey SET ratio_rec=(ratio_rec*%s+%s)/%s WHERE chtxt=%s AND key=%s",\
568 (beforenum,addnum,beforenum,chtxt,key)\
571 def select_by_key_bayeskey(self,chtxt,key):
572 db = self.connect_db()
573 dbexe = db[1].execute("\
574 SELECT key,chtxt,ratio_rec,ratio_all \
576 WHERE key = %s AND chtxt = %s", \
580 dls = db[1].fetchall()