OSDN Git Service

fix encoding process.
[rec10/rec10-git.git] / rec10 / trunk / src / dbMySQL.py
index 03fba75..dce770f 100644 (file)
@@ -3,7 +3,8 @@
 # Rec10 TS Recording Tools
 # Copyright (C) 2009 Yukikaze
 import MySQLdb
-
+import recdblist
+from decimal import Decimal
 class DB_MySQL:
     dbname = ""
     dbhost = ""
@@ -27,11 +28,11 @@ class DB_MySQL:
         db = self.connect_db()
         try:
             db[1].execute('\
-            CREATE TABLE rectime \
+            CREATE TABLE timeline \
             (\
             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
-            type VARCHAR(20),\
-            chtxt VARCHAR(20),\
+            type VARCHAR(40),\
+            chtxt VARCHAR(40),\
             title VARCHAR(100),\
             btime DATETIME,\
             etime DATETIME,\
@@ -43,10 +44,14 @@ class DB_MySQL:
         except:
             ""
         self.close_db(db)
-        self.new_tv("")
-        self.new_ch("")
-        self.new_reclog()
-        self.new_bayeskey()
+        self.new_epg_timeline("")
+        self.new_in_timeline_log()
+        self.new_in_auto_bayes_key()
+        self.new_in_auto_jbk_key()
+        self.new_auto_proc()
+        self.new_in_status()
+        self.new_auto_timeline_bayes()
+        self.new_auto_timeline_keyword()
     def connect_db(self):
         """
         dbへの接続
@@ -58,39 +63,40 @@ class DB_MySQL:
     def close_db(self, db):
         db[1].close()
         db[0].close()
-    def new_chdata(self):
+    def new_epg_ch(self):
         db = self.connect_db()
         try:
-            db[1].execute('drop table chdata')
+            db[1].execute('drop table epg_ch')
         except:
             ""
         try:
             db[1].execute('\
-            CREATE TABLE chdata \
+            CREATE TABLE epg_ch \
             (\
             bctype VARCHAR(15),\
             ontv VARCHAR(30) PRIMARY KEY,\
-            chtxt VARCHAR(15),\
+            chtxt VARCHAR(20),\
             ch VARCHAR(20),\
             csch VARCHAR(20),\
+            chname VARCHAR(100),\
             updatetime DATETIME,\
             status TINYINT\
             )')
         except:
             ""
         self.close_db(db)
-    def add_chdata(self, bctype, ontv, chtxt, ch, csch, updatetime):
+    def add_epg_ch(self, bctype, ontv, chtxt, ch, csch, updatetime):
         db = self.connect_db()
         db[1].execute('\
-        INSERT INTO chdata \
-        VALUES (%s,%s,%s,%s,%s,%s,%s)', \
+        INSERT INTO epg_ch \
+        VALUES (%s,%s,%s,%s,%s,"",%s,%s)', \
                       (bctype, ontv, chtxt, ch, csch, updatetime, "1"))
         self.close_db(db)
-    def select_by_ontv_chdata(self, ontv):
+    def select_by_ontv_epg_ch(self, ontv):
         db = self.connect_db()
         dbexe = db[1].execute("\
         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
-        FROM chdata \
+        FROM epg_ch \
         WHERE ontv = %s", \
                               (ontv,))
         ret = []
@@ -103,11 +109,11 @@ class DB_MySQL:
             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
             ret.append(r)
         return ret
-    def select_by_chtxt_chdata(self, chtxt):
+    def select_by_chtxt_epg_ch(self, chtxt):
         db = self.connect_db()
         dbexe = db[1].execute("\
         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
-        FROM chdata \
+        FROM epg_ch \
         WHERE chtxt = %s", \
                               (chtxt,))
         ret = []
@@ -120,11 +126,11 @@ class DB_MySQL:
             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
             ret.append(r)
         return ret
-    def select_by_bctype_chdata(self, bctype):
+    def select_by_bctype_epg_ch(self, bctype):
         db = self.connect_db()
         dbexe = db[1].execute("\
         SELECT bctype,ontv,chtxt,ch,csch,updatetime,status \
-        FROM chdata \
+        FROM epg_ch \
         WHERE bctype = %s", \
                               (bctype,))
         ret = []
@@ -133,18 +139,18 @@ class DB_MySQL:
             dls = db[1].fetchall()
         self.close_db(db)
         for dl in dls:
-            #print dl
+            #recdblist.printutf8(dl)
             r = list(dl)
             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
             r[6] = str(r[6])
             ret.append(r)
         return ret
-    def select_by_ch_chdata(self, ch):
+    def select_by_ch_epg_ch(self, ch):
         db = self.connect_db()
         dbexe = db[1].execute("\
         SELECT \
         bctype,ontv,chtxt,ch,csch,updatetime \
-        FROM chdata \
+        FROM epg_ch \
         WHERE ch = %s", \
                               (ch,))
         ret = []
@@ -157,11 +163,11 @@ class DB_MySQL:
             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
             ret.append(r)
         return ret
-    def select_all_chdata(self):
+    def select_all_epg_ch(self):
         db = self.connect_db()
         dbexe = db[1].execute("\
         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
-        FROM chdata \
+        FROM epg_ch \
         ")
         ret = []
         dls = []
@@ -173,9 +179,9 @@ class DB_MySQL:
             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
             ret.append(r)
         return ret
-    def select_get_update_chdata(self, dhour):
+    def select_get_update_epg_ch(self, dhour):
         db = self.connect_db()
-        dbexe = db[1].execute("SELECT bctype,chtxt,status FROM chdata \
+        dbexe = db[1].execute("SELECT bctype,chtxt,status FROM epg_ch \
         WHERE \
         ( \
         updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
@@ -186,64 +192,100 @@ class DB_MySQL:
         status > 1 \
         ORDER BY status DESC")
         ret = []
-        #print dbexe
+        #recdblist.printutf8(dbexe)
         if dbexe > 0:
             ret = db[1].fetchall()
         self.close_db(db)
         return ret
-    def update_by_bctype_chdata(self, bctype):
+    def update_by_bctype_epg_ch(self, bctype):
         db = self.connect_db()
         db[1].execute("\
-        UPDATE chdata \
+        UPDATE epg_ch \
         SET \
         updatetime=now() , \
         status = 1 \
         WHERE bctype = %s", (bctype,))
         self.close_db(db)
-    def update_by_bctype_and_chtxt_chdata(self, bctype, chtxt):
+    def update_by_bctype_and_chtxt_epg_ch(self, bctype, chtxt):
         db = self.connect_db()
         db[1].execute("\
-        UPDATE chdata \
+        UPDATE epg_ch \
         SET \
         updatetime=now() , \
         status = 1\
         WHERE bctype = %s AND chtxt = %s", (bctype, chtxt))
         self.close_db(db)
-    def update_status_by_bctype_chdata(self, bctype, status):
+    def update_status_by_bctype_epg_ch(self, bctype, status):
         db = self.connect_db()
         db[1].execute("\
-        UPDATE chdata \
+        UPDATE epg_ch \
         SET \
         status=%s \
         WHERE bctype = %s", \
                       (status, bctype)\
                       )
         self.close_db(db)
-    def add_reclog(self , chtxt="", title="", btime="", etime="", opt="", exp="", longexp="", category=""):
+    def update_chname_by_ontv_epg_ch(self,ontv,chname):
+        db = self.connect_db()
+        db[1].execute("\
+        UPDATE epg_ch \
+        SET \
+        chname=%s \
+        WHERE ontv = %s", \
+                      (chname, ontv)\
+                      )
+        self.close_db(db)
+    def add_auto_proc(self,type,title):
         db = self.connect_db()
         db[1].execute('\
-        INSERT IGNORE into reclog \
+        INSERT IGNORE into auto_proc \
+        (type,title) \
+        values (%s,%s)', \
+                      ( type, title))
+        ##db.commit()
+        self.close_db(db)
+    def new_auto_proc(self):
+        db = self.connect_db()
+        try:
+            db[1].execute('drop table auto_proc')
+        except:
+            ""
+        try:
+            db[1].execute('\
+            CREATE TABLE auto_proc \
+            (\
+            type VARCHAR(20),\
+            title VARCHAR(100) PRIMARY KEY,\
+            UNIQUE unibayeskey(title)\
+            )')
+        except:
+            ""
+        self.close_db(db)
+    def add_in_timeline_log(self , chtxt="", title="", btime="", etime="", opt="", exp="", longexp="", category=""):
+        db = self.connect_db()
+        db[1].execute('\
+        INSERT IGNORE into in_timeline_log \
         (chtxt,title,btime,etime,opt,exp,longexp,category) \
         values (%s,%s,%s,%s,%s,%s,%s,%s)', \
                       ( chtxt, title, btime, etime, opt,exp,longexp,category))
         ##db.commit()
         self.close_db(db)
-    def del_reclog(self, title="", chtxt="", btime=""):
+    def del_in_timeline_log(self, title="", chtxt="", btime=""):
         """
 
         """
         db = self.connect_db()
         db[1].execute("\
-        DELETE FROM reclog \
+        DELETE FROM in_timeline_log \
         WHERE title = %s AND chtxt = %s AND btime = %s", \
                       (title, chtxt, btime))
         #db.commit()
         self.close_db(db)
-    def new_reclog(self):
+    def new_in_timeline_log(self):
         db = self.connect_db()
         try:
             db[1].execute('\
-            CREATE TABLE reclog \
+            CREATE TABLE in_timeline_log \
             (\
             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
             chtxt VARCHAR(20),\
@@ -259,32 +301,32 @@ class DB_MySQL:
         except:
             ""
         self.close_db(db)
-    def add_rectime(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
+    def add_timeline(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
         db = self.connect_db()
         db[1].execute('\
-        INSERT IGNORE into rectime \
+        INSERT IGNORE into timeline \
         (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))
         ##db.commit()
         self.close_db(db)
-    def del_rectime(self, type="", title="", chtxt="", btime=""):
+    def del_timeline(self, type="", title="", chtxt="", btime=""):
         """
 
         """
         db = self.connect_db()
         db[1].execute("\
-        DELETE FROM rectime \
+        DELETE FROM timeline \
         WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
                       (type, title, chtxt, btime))
         #db.commit()
         self.close_db(db)
-    def select_all_rectime(self):
+    def select_all_timeline(self):
         db = self.connect_db()
         recdata = []
         dbr = db[1].execute("\
         SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
-        FROM rectime")
+        FROM timeline")
         dbl = db[1].fetchall()
         self.close_db(db)
         if dbr > 0:
@@ -292,7 +334,7 @@ class DB_MySQL:
                 ret = {}
                 ret['type'] = typet
                 ret['chtxt'] = chtxt
-                ret['title'] = title.encode('utf-8')
+                ret['title'] = title
                 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
                 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
                 ret['btime'] = btime
@@ -312,24 +354,24 @@ class DB_MySQL:
                 recdata.append(ret)
         self.close_db(db)
         return recdata
-    def select_bytime_rectime(self, dminutes):
+    def select_bytime_timeline(self, dminutes):
         db = self.connect_db()
         recdatum = []
         #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 )")
         dbr = db[1].execute("SELECT \
         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
-        FROM rectime \
+        FROM timeline \
         WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
         DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
         dbl = db[1].fetchall()
         self.close_db(db)
-        #print dbl
+        #recdblist.printutf8(dbl)
         if dbr > 0:
             for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
                 ret = {}
                 ret['type'] = typet
                 ret['chtxt'] = chtxt
-                ret['title'] = title.encode('utf-8')
+                ret['title'] = title
                 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
                 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
                 ret['btime'] = btime
@@ -339,42 +381,74 @@ class DB_MySQL:
                     deltatime = "3"
                 if deltaday == None or deltaday == "":
                     deltaday = "7"
-                if typet == 'key':
+                if typet == recdblist.REC_KEYWORD:
                     ret['deltatime'] = deltatime
-                elif typet == 'keyevery':
+                elif typet == recdblist.REC_KEYWORD_EVERY_SOME_DAYS:
                     ret['deltatime'] = deltatime
                     ret['deltaday'] = deltaday
                 recdatum.append(ret)
         return recdatum
-    def delete_old_rectime(self, dhour):
+    def delete_old_timeline(self, dhour):
         db = self.connect_db()
         db[1].execute("\
-        DELETE FROM rectime \
+        DELETE FROM timeline \
         WHERE \
         btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
         #db.commit()
         self.close_db(db)
-    def new_status(self):
+    def new_in_auto_jbk_key(self):
         db = self.connect_db()
         try:
             db[1].execute("\
-            CREATE TABLE status \
+            CREATE TABLE in_auto_jbk_key \
+            (\
+            id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
+            keyword VARCHAR(200),\
+            UNIQUE unijbk (keyword)\
+            )")
+        except:
+            ""
+        self.close_db(db)
+    def add_in_auto_jbk_key(self,key):
+        db = self.connect_db()
+        db[1].execute('\
+        INSERT IGNORE into in_auto_jbk_key \
+        (keyword) \
+        values (%s)', \
+                      (key,))
+        ##db.commit()
+        self.close_db(db)
+    def select_all_in_auto_jbk_key(self):
+        db = self.connect_db()
+        dbexe = db[1].execute("\
+        SELECT keyword \
+        FROM in_auto_jbk_key \
+        ")
+        ret = []
+        if dbexe > 0:
+            ret = db[1].fetchall()
+        self.close_db(db)
+        return ret
+    def new_in_status(self):
+        db = self.connect_db()
+        try:
+            db[1].execute("\
+            CREATE TABLE in_status \
             (\
             ts2avi TINYINT DEFAULT 0,\
             terec TINYINT DEFAULT 0,\
             bscsrec TINYINT DEFAULT 0,\
             b252ts TINYINT DEFAULT 0\
             )")
-            db[1].execute("INSERT IGNORE into status VALUE (0,0,0,0)")
+            db[1].execute("INSERT IGNORE into in_status VALUE (0,0,0,0)")
         except:
             ""
-
         self.close_db(db)
-    def select_all_status(self):
+    def select_all_in_status(self):
         db = self.connect_db()
         dbexe = db[1].execute("\
         SELECT ts2avi,terec,bscsrec,b252ts \
-        FROM status \
+        FROM in_status \
         ")
         ret = []
         dls = []
@@ -389,55 +463,55 @@ class DB_MySQL:
             r[3]=str(r[3])
             ret.append(r)
         return ret
-        self.close_db(db)
-    def change_ts2avi_status(self,i):
+        
+    def change_ts2avi_in_status(self,i):
         """
         statuをiだけ増減する
         iはint
         """
         db = self.connect_db()
         db[1].execute("\
-        UPDATE status SET ts2avi=ts2avi+%s",i)
+        UPDATE in_status SET ts2avi=ts2avi+%s",i)
         self.close_db(db)
-    def change_terec_status(self,i):
+    def change_terec_in_status(self,i):
         """
         statuをiだけ増減する
         iはint
         """
         db = self.connect_db()
         db[1].execute("\
-        UPDATE status SET terec=terec+%s",i)
+        UPDATE in_status SET terec=terec+%s",i)
         self.close_db(db)
-    def change_bscsrec_status(self,i):
+    def change_bscsrec_in_status(self,i):
         """
         statuをiだけ増減する
         iはint
         """
         db = self.connect_db()
         db[1].execute("\
-        UPDATE status SET bscsrec=bscsrec+%s",i)
+        UPDATE in_status SET bscsrec=bscsrec+%s",i)
         self.close_db(db)
-    def change_b252ts_status(self,i):
+    def change_b252ts_in_status(self,i):
         """
         statuをiだけ増減する
         iはint
         """
         db = self.connect_db()
         db[1].execute("\
-        UPDATE status SET b252ts=b252ts+%s",i)
+        UPDATE in_status SET b252ts=b252ts+%s",i)
         self.close_db(db)
-    def new_tv(self, bctype):
+    def new_epg_timeline(self, bctype):
         db = self.connect_db()
         try:
             db[1].execute("\
-            DELETE FROM tv \
+            DELETE FROM epg_timeline \
             WHERE bctype = %s", \
                           (bctype,))
         except:
             ""
         try:
             db[1].execute("\
-            CREATE TABLE tv \
+            CREATE TABLE epg_timeline \
             (\
             bctype VARCHAR(20),\
             channel VARCHAR(100) NOT NULL,\
@@ -453,160 +527,208 @@ class DB_MySQL:
             ""
         #db.commit()
         self.close_db(db)
-    def add_tv(self, bctype, channel, start, stop, title, desc,longdesc, category):
+    def add_epg_timeline(self, bctype, channel, start, stop, title, desc,longdesc, category):
         db = self.connect_db()
         db[1].execute('\
-        INSERT IGNORE INTO tv \
+        INSERT IGNORE INTO epg_timeline \
         VALUES (%s,%s,%s,%s,%s,%s,%s,%s)', \
                       (bctype, channel, start, stop, title, desc,longdesc,category))
         #db.commit()
         self.close_db(db)
-    def add_multi_tv(self, tvlists):
+    def add_multi_epg_timeline(self, tvlists):
         """
         tvlists is (bctype,channel,start,stop,title,desc,longdesc,category) lists.
         """
         db = self.connect_db()
         db[1].executemany('\
-        INSERT IGNORE INTO tv \
+        INSERT IGNORE INTO epg_timeline \
         (bctype,channel,start,stop,title,exp,longexp,category) \
         values(%s,%s,%s,%s,%s,%s,%s,%s)', \
                           tvlists)
-        #db.commit()
         self.close_db(db)
-    def select_by_time_ngram_tv(self, btime, etime, chtxt):
+    def select_by_time_ngram_epg_timeline(self, btime, etime, chtxt):
         db = self.connect_db()
         dbexe = "\
         SELECT \
-        chdata.chtxt,title,start,stop,exp,longexp,category \
-        FROM tv \
-        INNER JOIN chdata \
-        WHERE chdata.ontv=tv.channel \
+        epg_ch.chtxt,title,start,stop,exp,longexp,category \
+        FROM epg_timeline \
+        INNER JOIN epg_ch \
+        WHERE epg_ch.ontv=epg_timeline.channel \
         AND \
         start >= %s \
         AND \
         start <= %s \
         AND \
-        chdata.chtxt=%s"
+        epg_ch.chtxt=%s"
         dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
         retall = []
         if dbcmd > 0:
             retall = db[1].fetchall()
         self.close_db(db)
         return retall
-    def new_ch(self, bctype):
+    def select_by_time_auto_suggest_epg_timeline(self,keyword,btime,etime):
         db = self.connect_db()
-        try:
-            db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
-        except:
-            ""
-        try:
-            db[1].execute('\
-            CREATE TABLE ch \
-            (\
-            bctype VARCHAR(20),\
-            channel VARCHAR(20) NOT NULL,\
-            display VARCHAR(100),\
-            UNIQUE unich(bctype,channel)\
-            )')
-        except:
-            ""
-        #db.commit()
+        dbexe = "\
+        SELECT \
+        epg_ch.chtxt,title,start,stop,exp,longexp,category \
+        FROM epg_timeline \
+        INNER JOIN epg_ch \
+        WHERE epg_ch.ontv=epg_timeline.channel \
+        AND \
+        start >= %s \
+        AND \
+        start <= %s \
+        AND \
+        ( \
+        ( title LIKE \'%%"+keyword+"%%\' ) \
+        OR \
+        ( exp LIKE \'%%"+keyword+"%%\' ) \
+        OR \
+        ( longexp LIKE \'%%"+keyword+"%%\' ) \
+        )"
+        dbcmd = db[1].execute(dbexe,(btime, etime))
+        retall = []
+        if dbcmd > 0:
+            retall = db[1].fetchall()
         self.close_db(db)
-    def add_ch(self, bctype, channel, display):
-        db = self.connect_db()
-        db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
+        return retall
+    #def new_epg_ch(self, bctype):
+    #    db = self.connect_db()
+    #    try:
+    #        db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
+    #    except:
+    #        ""
+    #    try:
+    #        db[1].execute('\
+    #        CREATE TABLE epg_ch \
+    #        (\
+    #        bctype VARCHAR(20),\
+    #        channel VARCHAR(20) NOT NULL,\
+    #        display VARCHAR(100),\
+    #        UNIQUE unich(bctype,channel)\
+    #        )')
+    #    except:
+    #        ""
         #db.commit()
-        self.close_db(db)
-    def add_multi_ch(self, chlists):
-        """
-        chlists is (bctype,channel,display) lists
-        """
-        db = self.connect_db()
-        db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
-        self.close_db(db)
-    def new_bayeskey(self):
+    #    self.close_db(db)
+    #def add_epg_ch(self, bctype, channel, display):
+    #    db = self.connect_db()
+    ##    db[1].execute('INSERT IGNORE INTO epg_ch VALUES (%s,%s,%s)', (bctype, channel, display))
+        #db.commit()
+    #    self.close_db(db)
+    #def add_multi_ch(self, chlists):
+    #    """
+    #    chlists is (bctype,channel,display) lists
+    ##    """
+    #    db = self.connect_db()
+    #    db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
+    #    self.close_db(db)
+    def new_in_auto_bayes_key(self):
         db = self.connect_db()
         try:
-            db[1].execute('CREATE TABLE bayeskey \
+            db[1].execute('CREATE TABLE in_auto_bayes_key \
             (\
             keychar VARCHAR(10),\
             chtxt VARCHAR(20),\
-            ratio_rec DECIMAL(32,8),\
-            ratio_all DECIMAL(32,8)\
+            ratio_rec DECIMAL(32,14),\
+            ratio_all DECIMAL(32,14),\
+            UNIQUE unibayeskey(keychar,chtxt)\
             )')
+            db[1].execute('CREATE INDEX keycharindex ON in_auto_bayes_key(keychar)')
         except:
             ""
         self.close_db(db)
-    def add_bayeskey(self,key,chtxt,ratio_rec,ratio_all):
+
+    def add_in_auto_bayes_key(self,key,chtxt,ratio_rec,ratio_all):
         db = self.connect_db()
         ratio_rec=str(ratio_rec)
         ratio_all=str(ratio_all)
         db[1].execute('\
-        INSERT IGNORE INTO bayeskey \
+        INSERT IGNORE INTO in_auto_bayes_key \
         (keychar,chtxt,ratio_rec,ratio_all) \
         values (%s,%s,%s,%s)',\
         (key,chtxt,ratio_rec,ratio_all))
         self.close_db(db)
-    def add_num_bayeskey(self,chtxt,add_rec_num,add_all_num):
+    def add_num_in_auto_bayes_key(self,chtxt,add_rec_num,add_all_num):
         db = self.connect_db()
         add_rec_num=str(add_rec_num)
         add_all_num=str(add_all_num)
         db[1].execute("\
-        UPDATE bayeskey SET ratio_rec=ratio_rec+%s,ratio_all=ratio_all+%s WHERE keychar=\"Auto\" AND chtxt=%s",\
+        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",\
         (add_rec_num,add_all_num,chtxt))
         self.close_db(db)
-    def change_bayeskey(self,key,chtxt,new_ratio_rec,new_ratio_all):
+    def change_in_auto_bayes_key(self,key,chtxt,new_ratio_rec,new_ratio_all):
         """
         """
         db = self.connect_db()
         db[1].execute("\
-        UPDATE bayeskey SET ratio_rec=%s,ratio_all=%s WHERE keychar=%s AND chtxt=%s",(str(new_ratio_rec),str(new_ratio_all),key,chtxt)\
+        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)\
         )
         self.close_db(db)
-    def change_ratio_all_reduce_bayeskey(self,chtxt,beforenum,newnum):
+    def change_ratio_all_reduce_in_auto_bayes_key(self,chtxt,beforenum,newnum):
+        beforenum=str(beforenum)
+        newnum=str(newnum)
         db = self.connect_db()
         db[1].execute("\
-        UPDATE bayeskey SET ratio_all=ratio_all*%s/(%s+%s) WHERE chtxt=%s",(str(beforenum),str(newnum),str(beforenum),chtxt)\
+        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)\
         )
         self.close_db(db)
-    def change_ratio_all_bayeskey(self,key,chtxt,beforenum,addnum):
+    def change_ratio_all_in_auto_bayes_key(self,key,chtxt,beforenum,addnum):
         db = self.connect_db()
+        beforenumf=beforenum
         beforenum=str(beforenum)
-        addnum=str(addnum)
-        db[1].execute("\
-        UPDATE bayeskey SET ratio_all=(ratio_all*%s+%s)/%s WHERE chtxt=%s AND keychar=%s",\
-        (beforenum,addnum,beforenum,chtxt,key)\
-        )
+        db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
+        VALUES (%s,%s,%s,%s)\
+        ON DUPLICATE KEY UPDATE \
+        ratio_all=CONVERT((ratio_all*%s+%s)/%s,DECIMAL(32,14))",(key,chtxt,"0",str(Decimal(addnum)/beforenumf),beforenum,chtxt,key))
         self.close_db(db)
-    def change_many_ratio_all_bayeskey(self,chtxt,beforenum,list):
+    def change_multi_ratio_all_in_auto_bayes_key(self,chtxt,beforenum,list):
         """
-        list=[key,addnum]のリスト
+        list={key:addnum}のリスト
         """
-        retlist=[]
+        beforenumf=beforenum
         beforenum=str(beforenum)
-        addnum=str(addnum)
-        for i in list:
-            retlist.append((beforenum,i[1],beforenum,chtxt,i[0]))
         db = self.connect_db()
-        db[1].executemany("\
-        UPDATE bayeskey SET ratio_all=(ratio_all*%s+%s)/%s WHERE chtxt=%s AND keychar=%s",\
-        retlist\
-        )
+        for i,j in list.items():
+            retl=(i,chtxt,"0",str(Decimal(j)/beforenumf),beforenum,str(j),beforenum)
+            db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
+            VALUES (%s,%s,%s,%s)\
+            ON DUPLICATE KEY UPDATE \
+            ratio_all=CONVERT((ratio_all*%s+%s)/%s,DECIMAL(32,14))",retl)
         self.close_db(db)
-    def change_ratio_rec_bayeskey(self,key,chtxt,beforenum,addnum):
-        db = self.connect_db()
+    def change_ratio_rec_reduce_in_auto_bayes_key(self,chtxt,beforenum,newnum):
         beforenum=str(beforenum)
-        addnum=str(addnum)
+        newnum=str(newnum)
+        db = self.connect_db()
         db[1].execute("\
-        UPDATE bayeskey SET ratio_rec=(ratio_rec*%s+%s)/%s WHERE chtxt=%s AND keychar=%s",\
-        (beforenum,addnum,beforenum,chtxt,key)\
+        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)\
         )
         self.close_db(db)
-    def select_by_key_bayeskey(self,key,chtxt):
+    def change_ratio_rec_in_auto_bayes_key(self,key,chtxt,beforenum,addnum):
+        db = self.connect_db()
+        beforenumf=beforenum
+        beforenum=str(beforenum)
+        db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
+        VALUES (%s,%s,%s,%s)\
+        ON DUPLICATE KEY UPDATE \
+        ratio_rec=CONVERT((ratio_rec*%s+%s)/%s,DECIMAL(32,14))",(key,chtxt,str(Decimal(addnum)/beforenumf),"0",beforenum,chtxt,key))
+        self.close_db(db)
+    def change_multi_ratio_rec_in_auto_bayes_key(self,chtxt,beforenum,list):#self,key,chtxt,beforenum,addnum):
+        beforenumf=beforenum
+        beforenum=str(beforenum)
+        db = self.connect_db()
+        for i,j in list.items():
+            retl=(i,chtxt,str(Decimal(j)/beforenumf),"0",beforenum,str(j),beforenum)
+            db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
+            VALUES (%s,%s,%s,%s)\
+            ON DUPLICATE KEY UPDATE \
+            ratio_rec=CONVERT((ratio_rec*%s+%s)/%s,DECIMAL(32,14))",retl)
+        self.close_db(db)
+    def select_by_key_in_auto_bayes_key(self,key,chtxt):
         db = self.connect_db()
         dbexe = db[1].execute("\
         SELECT keychar,chtxt,ratio_rec,ratio_all \
-        FROM bayeskey \
+        FROM in_auto_bayes_key \
         WHERE keychar = %s AND chtxt = %s", \
                               (key,chtxt))
         dls = []
@@ -616,4 +738,67 @@ class DB_MySQL:
         if len(dls)>0:
             return dls[0]
         else:
-            return dls
\ No newline at end of file
+            return dls
+
+    def new_auto_timeline_keyword(self):
+        db = self.connect_db()
+        try:
+            db[1].execute('\
+            CREATE TABLE auto_timeline_keyword \
+            (\
+            id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
+            chtxt VARCHAR(40),\
+            title VARCHAR(100),\
+            btime DATETIME,\
+            etime DATETIME,\
+            UNIQUE uni (chtxt,title,btime,etime)\
+            )')
+        except:
+            ""
+    def add_auto_timeline_keyword(self,chtxt="", title="", btime="", etime=""):
+        db = self.connect_db()
+        db[1].execute('\
+        INSERT IGNORE into auto_timeline_keyword \
+        (chtxt,title,btime,etime) \
+        values (%s,%s,%s,%s)', \
+                       (chtxt, title, btime, etime))
+        ##db.commit()
+        self.close_db(db)
+    def delete_old_auto_timeline_keyword(self, dhour):
+        db = self.connect_db()
+        db[1].execute("\
+        DELETE FROM auto_timeline_keyword \
+        WHERE \
+        btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
+        self.close_db(db)
+    def new_auto_timeline_bayes(self):
+        db = self.connect_db()
+        try:
+            db[1].execute('\
+            CREATE TABLE auto_timeline_bayes \
+            (\
+            id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
+            chtxt VARCHAR(40),\
+            title VARCHAR(100),\
+            btime DATETIME,\
+            etime DATETIME,\
+            point INT,\
+            UNIQUE uni (chtxt,title,btime,etime)\
+            )')
+        except:
+            ""
+    def add_auto_timeline_bayes(self,chtxt="", title="", btime="", etime="",point=""):
+        db = self.connect_db()
+        db[1].execute('\
+        INSERT IGNORE into auto_timeline_bayes \
+        (chtxt,title,btime,etime,point) \
+        values (%s,%s,%s,%s,%s)', \
+                      (chtxt, title, btime, etime,point))
+        self.close_db(db)
+    def delete_old_auto_timeline_bayes(self, dhour):
+        db = self.connect_db()
+        db[1].execute("\
+        DELETE FROM auto_timeline_bayes \
+        WHERE \
+        btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
+        self.close_db(db)