3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
6 from decimal import Decimal
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 main_timetable \
32 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
38 deltatime VARCHAR(5),\
41 UNIQUE uni (type,chtxt,title,btime,deltaday)\
46 self.new_epg_timeline("")
48 self.new_in_timeline_log()
49 self.new_in_auto_bayes_key()
50 self.new_in_auto_jbk_key()
57 con = MySQLdb.connect(db=self.dbname, host=self.dbhost, port=self.dbport, user=self.dbusr, passwd=self.dbpasswd, charset="utf8")
59 cur.execute('set names utf8;')
61 def close_db(self, db):
65 db = self.connect_db()
67 db[1].execute('drop table epg_ch')
75 ontv VARCHAR(30) PRIMARY KEY,\
86 def add_epg_ch(self, bctype, ontv, chtxt, ch, csch, updatetime):
87 db = self.connect_db()
90 VALUES (%s,%s,%s,%s,%s,"",%s,%s)', \
91 (bctype, ontv, chtxt, ch, csch, updatetime, "1"))
93 def select_by_ontv_epg_ch(self, ontv):
94 db = self.connect_db()
95 dbexe = db[1].execute("\
96 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
103 dls = db[1].fetchall()
107 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
110 def select_by_chtxt_epg_ch(self, chtxt):
111 db = self.connect_db()
112 dbexe = db[1].execute("\
113 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
120 dls = db[1].fetchall()
124 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
127 def select_by_bctype_epg_ch(self, bctype):
128 db = self.connect_db()
129 dbexe = db[1].execute("\
130 SELECT bctype,ontv,chtxt,ch,csch,updatetime,status \
132 WHERE bctype = %s", \
137 dls = db[1].fetchall()
142 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
146 def select_by_ch_epg_ch(self, ch):
147 db = self.connect_db()
148 dbexe = db[1].execute("\
150 bctype,ontv,chtxt,ch,csch,updatetime \
157 dls = db[1].fetchall()
161 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
164 def select_all_epg_ch(self):
165 db = self.connect_db()
166 dbexe = db[1].execute("\
167 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
173 dls = db[1].fetchall()
177 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
180 def select_get_update_epg_ch(self, dhour):
181 db = self.connect_db()
182 dbexe = db[1].execute("SELECT bctype,chtxt,status FROM epg_ch \
185 updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
191 ORDER BY status DESC")
195 ret = db[1].fetchall()
198 def update_by_bctype_epg_ch(self, bctype):
199 db = self.connect_db()
205 WHERE bctype = %s", (bctype,))
207 def update_by_bctype_and_chtxt_epg_ch(self, bctype, chtxt):
208 db = self.connect_db()
214 WHERE bctype = %s AND chtxt = %s", (bctype, chtxt))
216 def update_status_by_bctype_epg_ch(self, bctype, status):
217 db = self.connect_db()
222 WHERE bctype = %s", \
226 def update_chname_by_ontv_epg_ch(self,ontv,chname):
227 db = self.connect_db()
236 def add_auto_proc(self,type,title):
237 db = self.connect_db()
239 INSERT IGNORE into auto_proc \
245 def new_auto_proc(self):
246 db = self.connect_db()
248 db[1].execute('drop table auto_proc')
253 CREATE TABLE auto_proc \
256 title VARCHAR(100) PRIMARY KEY,\
257 UNIQUE unibayeskey(title)\
262 def add_in_timeline_log(self , chtxt="", title="", btime="", etime="", opt="", exp="", longexp="", category=""):
263 db = self.connect_db()
265 INSERT IGNORE into in_timeline_log \
266 (chtxt,title,btime,etime,opt,exp,longexp,category) \
267 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
268 ( chtxt, title, btime, etime, opt,exp,longexp,category))
271 def del_in_timeline_log(self, title="", chtxt="", btime=""):
275 db = self.connect_db()
277 DELETE FROM in_timeline_log \
278 WHERE title = %s AND chtxt = %s AND btime = %s", \
279 (title, chtxt, btime))
282 def new_in_timeline_log(self):
283 db = self.connect_db()
286 CREATE TABLE in_timeline_log \
288 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
296 category VARCHAR(100),\
297 UNIQUE uni (chtxt,title,btime,category)\
302 def add_main_timetable(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
303 db = self.connect_db()
305 INSERT IGNORE into main_timetable \
306 (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
307 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
308 (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
311 def del_main_timetable(self, type="", title="", chtxt="", btime=""):
315 db = self.connect_db()
317 DELETE FROM main_timetable \
318 WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
319 (type, title, chtxt, btime))
322 def select_all_main_timetable(self):
323 db = self.connect_db()
325 dbr = db[1].execute("\
326 SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
327 FROM main_timetable")
328 dbl = db[1].fetchall()
331 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
335 ret['title'] = title.encode('utf-8')
336 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
337 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
341 ret['deltatime'] = ""
343 if deltatime == None:
348 ret['deltatime'] = deltatime
349 elif typet == 'keyevery':
350 ret['deltatime'] = deltatime
351 ret['deltaday'] = deltaday
355 def select_bytime_main_timetable(self, dminutes):
356 db = self.connect_db()
358 #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 )")
359 dbr = db[1].execute("SELECT \
360 type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
361 FROM main_timetable \
362 WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
363 DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
364 dbl = db[1].fetchall()
368 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
372 ret['title'] = title.encode('utf-8')
373 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
374 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
378 if deltatime == None or deltatime == "":
380 if deltaday == None or deltaday == "":
383 ret['deltatime'] = deltatime
384 elif typet == 'keyevery':
385 ret['deltatime'] = deltatime
386 ret['deltaday'] = deltaday
389 def delete_old_main_timetable(self, dhour):
390 db = self.connect_db()
392 DELETE FROM main_timetable \
394 btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
397 def new_in_auto_jbk_key(self):
398 db = self.connect_db()
401 CREATE TABLE in_auto_jbk_key \
403 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
404 keyword VARCHAR(200),\
405 UNIQUE unijbk (keyword)\
410 def add_in_auto_jbk_key(self,key):
411 db = self.connect_db()
413 INSERT IGNORE into in_auto_jbk_key \
419 def select_all_in_auto_jbk_key(self):
420 db = self.connect_db()
421 dbexe = db[1].execute("\
423 FROM in_auto_jbk_key \
427 ret = db[1].fetchall()
430 def new_in_status(self):
431 db = self.connect_db()
434 CREATE TABLE in_status \
436 ts2avi TINYINT DEFAULT 0,\
437 terec TINYINT DEFAULT 0,\
438 bscsrec TINYINT DEFAULT 0,\
439 b252ts TINYINT DEFAULT 0\
441 db[1].execute("INSERT IGNORE into in_status VALUE (0,0,0,0)")
445 def select_all_in_status(self):
446 db = self.connect_db()
447 dbexe = db[1].execute("\
448 SELECT ts2avi,terec,bscsrec,b252ts \
454 dls = db[1].fetchall()
465 def change_ts2avi_in_status(self,i):
470 db = self.connect_db()
472 UPDATE in_status SET ts2avi=ts2avi+%s",i)
474 def change_terec_in_status(self,i):
479 db = self.connect_db()
481 UPDATE in_status SET terec=terec+%s",i)
483 def change_bscsrec_in_status(self,i):
488 db = self.connect_db()
490 UPDATE in_status SET bscsrec=bscsrec+%s",i)
492 def change_b252ts_in_status(self,i):
497 db = self.connect_db()
499 UPDATE in_status SET b252ts=b252ts+%s",i)
501 def new_epg_timeline(self, bctype):
502 db = self.connect_db()
505 DELETE FROM epg_timeline \
506 WHERE bctype = %s", \
512 CREATE TABLE epg_timeline \
515 channel VARCHAR(100) NOT NULL,\
521 category VARCHAR(100),\
522 UNIQUE unitv(bctype,channel,start,stop,title)\
528 def add_epg_timeline(self, bctype, channel, start, stop, title, desc,longdesc, category):
529 db = self.connect_db()
531 INSERT IGNORE INTO epg_timeline \
532 VALUES (%s,%s,%s,%s,%s,%s,%s,%s)', \
533 (bctype, channel, start, stop, title, desc,longdesc,category))
536 def add_multi_epg_timeline(self, tvlists):
538 tvlists is (bctype,channel,start,stop,title,desc,longdesc,category) lists.
540 db = self.connect_db()
542 INSERT IGNORE INTO epg_timeline \
543 (bctype,channel,start,stop,title,exp,longexp,category) \
544 values(%s,%s,%s,%s,%s,%s,%s,%s)', \
548 def select_by_time_ngram_epg_timeline(self, btime, etime, chtxt):
549 db = self.connect_db()
552 chdata.chtxt,title,start,stop,exp,longexp,category \
555 WHERE chdata.ontv=epg_timeline.channel \
562 dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
565 retall = db[1].fetchall()
568 def select_by_time_auto_suggest_epg_timeline(self,keyword,btime,etime):
569 db = self.connect_db()
572 chdata.chtxt,title,start,stop,exp,longexp,category \
575 WHERE chdata.ontv=epg_timeline.channel \
582 ( title LIKE \'%%"+keyword+"%%\' ) \
584 ( exp LIKE \'%%"+keyword+"%%\' ) \
586 ( longexp LIKE \'%%"+keyword+"%%\' ) \
588 dbcmd = db[1].execute(dbexe,(btime, etime))
591 retall = db[1].fetchall()
594 #def new_epg_ch(self, bctype):
595 # db = self.connect_db()
597 # db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
602 # CREATE TABLE epg_ch \
604 # bctype VARCHAR(20),\
605 # channel VARCHAR(20) NOT NULL,\
606 # display VARCHAR(100),\
607 # UNIQUE unich(bctype,channel)\
613 #def add_epg_ch(self, bctype, channel, display):
614 # db = self.connect_db()
615 ## db[1].execute('INSERT IGNORE INTO epg_ch VALUES (%s,%s,%s)', (bctype, channel, display))
618 #def add_multi_ch(self, chlists):
620 # chlists is (bctype,channel,display) lists
622 # db = self.connect_db()
623 # db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
625 def new_in_auto_bayes_key(self):
626 db = self.connect_db()
628 db[1].execute('CREATE TABLE in_auto_bayes_key \
630 keychar VARCHAR(10),\
632 ratio_rec DECIMAL(32,14),\
633 ratio_all DECIMAL(32,14),\
634 UNIQUE unibayeskey(keychar,chtxt)\
636 db[1].execute('CREATE INDEX keycharindex ON in_auto_bayes_key(keychar)')
641 def add_in_auto_bayes_key(self,key,chtxt,ratio_rec,ratio_all):
642 db = self.connect_db()
643 ratio_rec=str(ratio_rec)
644 ratio_all=str(ratio_all)
646 INSERT IGNORE INTO in_auto_bayes_key \
647 (keychar,chtxt,ratio_rec,ratio_all) \
648 values (%s,%s,%s,%s)',\
649 (key,chtxt,ratio_rec,ratio_all))
651 def add_num_in_auto_bayes_key(self,chtxt,add_rec_num,add_all_num):
652 db = self.connect_db()
653 add_rec_num=str(add_rec_num)
654 add_all_num=str(add_all_num)
656 UPDATE in_auto_bayes_key SET ratio_rec=CONVERT(ratio_rec+%s,DECIMAL(32,14)),ratio_all=CONVERT(ratio_all+%s,DECIMAL(32,14)) WHERE keychar=\"NUM\" AND chtxt=%s",\
657 (add_rec_num,add_all_num,chtxt))
659 def change_in_auto_bayes_key(self,key,chtxt,new_ratio_rec,new_ratio_all):
662 db = self.connect_db()
664 UPDATE in_auto_bayes_key SET ratio_rec=%s,ratio_all=%s WHERE keychar=%s AND chtxt=%s",(str(new_ratio_rec),str(new_ratio_all),key,chtxt)\
667 def change_ratio_all_reduce_in_auto_bayes_key(self,chtxt,beforenum,newnum):
668 beforenum=str(beforenum)
670 db = self.connect_db()
672 UPDATE in_auto_bayes_key SET ratio_all=CONVERT(ratio_all*%s/(%s+%s),DECIMAL(32,14)) WHERE chtxt=%s AND NOT (keychar=\"NUM\")",(beforenum,newnum,beforenum,chtxt)\
675 def change_ratio_all_in_auto_bayes_key(self,key,chtxt,beforenum,addnum):
676 db = self.connect_db()
678 beforenum=str(beforenum)
679 db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
680 VALUES (%s,%s,%s,%s)\
681 ON DUPLICATE KEY UPDATE \
682 ratio_all=CONVERT((ratio_all*%s+%s)/%s,DECIMAL(32,14))",(key,chtxt,"0",str(Decimal(addnum)/beforenumf),beforenum,chtxt,key))
684 def change_multi_ratio_all_in_auto_bayes_key(self,chtxt,beforenum,list):
686 list={key:addnum}のリスト
689 beforenum=str(beforenum)
690 db = self.connect_db()
691 for i,j in list.items():
692 retl=(i,chtxt,"0",str(Decimal(j)/beforenumf),beforenum,str(j),beforenum)
693 db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
694 VALUES (%s,%s,%s,%s)\
695 ON DUPLICATE KEY UPDATE \
696 ratio_all=CONVERT((ratio_all*%s+%s)/%s,DECIMAL(32,14))",retl)
698 def change_ratio_rec_reduce_in_auto_bayes_key(self,chtxt,beforenum,newnum):
699 beforenum=str(beforenum)
701 db = self.connect_db()
703 UPDATE in_auto_bayes_key SET ratio_rec=CONVERT(ratio_rec*%s/(%s+%s),DECIMAL(32,14)) WHERE chtxt=%s AND NOT (keychar=\"NUM\")",(beforenum,newnum,beforenum,chtxt)\
706 def change_ratio_rec_in_auto_bayes_key(self,key,chtxt,beforenum,addnum):
707 db = self.connect_db()
709 beforenum=str(beforenum)
710 db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
711 VALUES (%s,%s,%s,%s)\
712 ON DUPLICATE KEY UPDATE \
713 ratio_rec=CONVERT((ratio_rec*%s+%s)/%s,DECIMAL(32,14))",(key,chtxt,str(Decimal(addnum)/beforenumf),"0",beforenum,chtxt,key))
715 def change_multi_ratio_rec_in_auto_bayes_key(self,chtxt,beforenum,list):#self,key,chtxt,beforenum,addnum):
717 beforenum=str(beforenum)
718 db = self.connect_db()
719 for i,j in list.items():
720 retl=(i,chtxt,str(Decimal(j)/beforenumf),"0",beforenum,str(j),beforenum)
721 db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
722 VALUES (%s,%s,%s,%s)\
723 ON DUPLICATE KEY UPDATE \
724 ratio_rec=CONVERT((ratio_rec*%s+%s)/%s,DECIMAL(32,14))",retl)
726 def select_by_key_in_auto_bayes_key(self,key,chtxt):
727 db = self.connect_db()
728 dbexe = db[1].execute("\
729 SELECT keychar,chtxt,ratio_rec,ratio_all \
730 FROM in_auto_bayes_key \
731 WHERE keychar = %s AND chtxt = %s", \
735 dls = db[1].fetchall()