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()
56 con = MySQLdb.connect(db=self.dbname, host=self.dbhost, port=self.dbport, user=self.dbusr, passwd=self.dbpasswd, charset="utf8")
58 cur.execute('set names utf8;')
60 def close_db(self, db):
64 db = self.connect_db()
66 db[1].execute('drop table epg_ch')
74 ontv VARCHAR(30) PRIMARY KEY,\
85 def add_epg_ch(self, bctype, ontv, chtxt, ch, csch, updatetime):
86 db = self.connect_db()
89 VALUES (%s,%s,%s,%s,%s,"",%s,%s)', \
90 (bctype, ontv, chtxt, ch, csch, updatetime, "1"))
92 def select_by_ontv_epg_ch(self, ontv):
93 db = self.connect_db()
94 dbexe = db[1].execute("\
95 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
102 dls = db[1].fetchall()
106 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
109 def select_by_chtxt_epg_ch(self, chtxt):
110 db = self.connect_db()
111 dbexe = db[1].execute("\
112 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
119 dls = db[1].fetchall()
123 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
126 def select_by_bctype_epg_ch(self, bctype):
127 db = self.connect_db()
128 dbexe = db[1].execute("\
129 SELECT bctype,ontv,chtxt,ch,csch,updatetime,status \
131 WHERE bctype = %s", \
136 dls = db[1].fetchall()
141 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
145 def select_by_ch_epg_ch(self, ch):
146 db = self.connect_db()
147 dbexe = db[1].execute("\
149 bctype,ontv,chtxt,ch,csch,updatetime \
156 dls = db[1].fetchall()
160 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
163 def select_all_epg_ch(self):
164 db = self.connect_db()
165 dbexe = db[1].execute("\
166 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
172 dls = db[1].fetchall()
176 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
179 def select_get_update_epg_ch(self, dhour):
180 db = self.connect_db()
181 dbexe = db[1].execute("SELECT bctype,chtxt,status FROM epg_ch \
184 updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
190 ORDER BY status DESC")
194 ret = db[1].fetchall()
197 def update_by_bctype_epg_ch(self, bctype):
198 db = self.connect_db()
204 WHERE bctype = %s", (bctype,))
206 def update_by_bctype_and_chtxt_epg_ch(self, bctype, chtxt):
207 db = self.connect_db()
213 WHERE bctype = %s AND chtxt = %s", (bctype, chtxt))
215 def update_status_by_bctype_epg_ch(self, bctype, status):
216 db = self.connect_db()
221 WHERE bctype = %s", \
225 def update_chname_by_ontv_epg_ch(self,ontv,chname):
226 db = self.connect_db()
235 def add_auto_proc(self,type,title):
236 db = self.connect_db()
238 INSERT IGNORE into auto_proc \
244 def new_auto_proc(self):
245 db = self.connect_db()
247 db[1].execute('drop table auto_proc')
252 CREATE TABLE auto_proc \
255 title VARCHAR(100) PRIMARY KEY,\
256 UNIQUE unibayeskey(title)\
261 def add_in_timeline_log(self , chtxt="", title="", btime="", etime="", opt="", exp="", longexp="", category=""):
262 db = self.connect_db()
264 INSERT IGNORE into in_timeline_log \
265 (chtxt,title,btime,etime,opt,exp,longexp,category) \
266 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
267 ( chtxt, title, btime, etime, opt,exp,longexp,category))
270 def del_in_timeline_log(self, title="", chtxt="", btime=""):
274 db = self.connect_db()
276 DELETE FROM in_timeline_log \
277 WHERE title = %s AND chtxt = %s AND btime = %s", \
278 (title, chtxt, btime))
281 def new_in_timeline_log(self):
282 db = self.connect_db()
285 CREATE TABLE in_timeline_log \
287 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
295 category VARCHAR(100),\
296 UNIQUE uni (chtxt,title,btime,category)\
301 def add_main_timetable(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
302 db = self.connect_db()
304 INSERT IGNORE into main_timetable \
305 (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
306 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
307 (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
310 def del_main_timetable(self, type="", title="", chtxt="", btime=""):
314 db = self.connect_db()
316 DELETE FROM main_timetable \
317 WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
318 (type, title, chtxt, btime))
321 def select_all_main_timetable(self):
322 db = self.connect_db()
324 dbr = db[1].execute("\
325 SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
326 FROM main_timetable")
327 dbl = db[1].fetchall()
330 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
334 ret['title'] = title.encode('utf-8')
335 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
336 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
340 ret['deltatime'] = ""
342 if deltatime == None:
347 ret['deltatime'] = deltatime
348 elif typet == 'keyevery':
349 ret['deltatime'] = deltatime
350 ret['deltaday'] = deltaday
354 def select_bytime_main_timetable(self, dminutes):
355 db = self.connect_db()
357 #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 )")
358 dbr = db[1].execute("SELECT \
359 type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
360 FROM main_timetable \
361 WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
362 DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
363 dbl = db[1].fetchall()
367 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
371 ret['title'] = title.encode('utf-8')
372 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
373 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
377 if deltatime == None or deltatime == "":
379 if deltaday == None or deltaday == "":
382 ret['deltatime'] = deltatime
383 elif typet == 'keyevery':
384 ret['deltatime'] = deltatime
385 ret['deltaday'] = deltaday
388 def delete_old_main_timetable(self, dhour):
389 db = self.connect_db()
391 DELETE FROM main_timetable \
393 btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
396 def new_in_auto_jbk_key(self):
397 db = self.connect_db()
400 CREATE TABLE in_auto_jbk_key \
402 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
403 keyword VARCHAR(200),\
404 UNIQUE unijbk (keyword)\
409 def add_in_auto_jbk_key(self,key):
410 db = self.connect_db()
412 INSERT IGNORE into in_auto_jbk_key \
418 def select_all_in_auto_jbk_key(self):
419 db = self.connect_db()
420 dbexe = db[1].execute("\
422 FROM in_auto_jbk_key \
426 ret = db[1].fetchall()
429 def new_in_status(self):
430 db = self.connect_db()
433 CREATE TABLE in_status \
435 ts2avi TINYINT DEFAULT 0,\
436 terec TINYINT DEFAULT 0,\
437 bscsrec TINYINT DEFAULT 0,\
438 b252ts TINYINT DEFAULT 0\
440 db[1].execute("INSERT IGNORE into 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 \
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()