OSDN Git Service

implement BonTsDemux Audio Split option.
[rec10/rec10-git.git] / rec10 / trunk / src / dbMySQL.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2011 Yukikaze
5 import MySQLdb
6 import recdblist
7 import warnings
8 import traceback
9
10 from decimal import Decimal
11 class DB_MySQL:
12     dbname = ""
13     dbhost = ""
14     dbusr = ""
15     dbpasswd = ""
16     dbport = 0
17     def __init__(self, dbname, user, passwd, host="localhost", port=3306):
18         warnings.filterwarnings('ignore', "Data truncated for column")
19         self.dbname = dbname
20         self.dbhost = host
21         self.dbusr = user
22         self.dbpasswd = passwd
23         self.dbport = port
24         try:
25             con = MySQLdb.connect(user=user, passwd=passwd)
26             cur = con.cursor()
27             cur.execute('CREATE DATABASE ' + dbname + " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
28             cur.close()
29             con.close()
30         except Exception, inst:
31             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
32                 recdblist.addCommonlogEX("Error", "init (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
33         db = self.connect_db()
34         try:
35             db[1].execute('\
36             CREATE TABLE timeline \
37             (\
38             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
39             type VARCHAR(40),\
40             chtxt VARCHAR(40),\
41             title VARCHAR(100),\
42             btime DATETIME,\
43             etime DATETIME,\
44             deltatime VARCHAR(5),\
45             deltaday VARCHAR(5),\
46             opt VARCHAR(20),\
47             epgtitle VARCHAR(100),\
48             epgbtime DATETIME,\
49             epgetime DATETIME,\
50             epgduplicate TINYINT DEFAULT 0,\
51             epgchange TINYINT DEFAULT 0,\
52             epgexp VARCHAR(200),\
53             epgcategory VARCHAR(100),\
54             counter TINYINT DEFAULT -1,\
55             UNIQUE uni (type,chtxt,title,btime,deltaday)\
56             )')
57         except Exception, inst:
58             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
59                 recdblist.addCommonlogEX("Error", "init (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
60         self.close_db(db)
61         self.new_epg_timeline("")
62         self.new_in_timeline_log()
63         self.new_in_auto_bayes_key()
64         self.new_in_auto_jbk_key()
65         self.new_in_status()
66         self.new_in_settings()
67         self.new_auto_timeline_bayes()
68         self.new_auto_timeline_keyword()
69     def connect_db(self):
70         """
71         dbへの接続
72         """
73         con = MySQLdb.connect(db=self.dbname, host=self.dbhost, port=self.dbport, user=self.dbusr, passwd=self.dbpasswd, charset="utf8")
74         cur = con.cursor()
75         cur.execute('set names utf8;')
76         return [con, cur]
77     def close_db(self, db):
78         db[1].close()
79         db[0].close()
80     def new_epg_ch(self):
81         db = self.connect_db()
82         try:
83             db[1].execute('drop table epg_ch')
84         except Exception, inst:
85             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and (inst[0]==1050 or inst[0]==1051))):
86                 recdblist.addCommonlogEX("Error", "new_epg_ch drop (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
87
88         try:
89             db[1].execute('\
90             CREATE TABLE epg_ch \
91             (\
92             bctype VARCHAR(15),\
93             chtxt VARCHAR(20) PRIMARY KEY,\
94             ch VARCHAR(20),\
95             csch VARCHAR(20),\
96             chname VARCHAR(100),\
97             updatetime DATETIME,\
98             status TINYINT,\
99             visible TINYINT DEFAULT 1,\
100             logo0 BLOB,\
101             logo1 BLOB,\
102             logo2 BLOB,\
103             logo3 BLOB,\
104             logo4 BLOB,\
105             logo5 BLOB,\
106             logoupdate DATETIME,\
107             logostatus TINYINT DEFAULT 2,\
108             scanupdate DATETIME\
109             )')
110         except Exception, inst:
111             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
112                 recdblist.addCommonlogEX("Error", "new_epg_ch (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
113
114         self.close_db(db)
115     def add_epg_ch(self, bctype, chtxt, ch, csch, updatetime,logoupdate,scanupdate):
116         db = self.connect_db()
117         db[1].execute('\
118         INSERT INTO epg_ch (bctype,chtxt,ch,csch,chname,updatetime,status,visible,logoupdate,scanupdate)\
119         VALUES (%s,%s,%s,%s,"",%s,%s,%s,%s,%s)', \
120                       (bctype, chtxt, ch, csch, updatetime, "1","1",logoupdate,scanupdate))
121         self.close_db(db)
122     def delete_all_epg_ch(self):
123         db = self.connect_db()
124         db[1].execute('\
125         DROP TABLE epg_ch ')
126         self.close_db(db)
127     def select_by_chtxt_epg_ch(self, chtxt):
128         db = self.connect_db()
129         dbexe = db[1].execute("\
130         SELECT bctype,chtxt,ch,csch,updatetime,chname,status,visible,logoupdate,scanupdate \
131         FROM epg_ch \
132         WHERE chtxt LIKE %s", \
133                               (chtxt,))
134         ret = []
135         dls = []
136         if dbexe > 0:
137             dls = db[1].fetchall()
138         self.close_db(db)
139         for dl in dls:
140             r = list(dl)
141             r[4] = r[4].strftime("%Y-%m-%d %H:%M:%S")
142             r[8] = r[8].strftime("%Y-%m-%d %H:%M:%S")
143             r[9] = r[9].strftime("%Y-%m-%d %H:%M:%S")
144             ret.append(r)
145         return ret
146     def select_by_bctype_epg_ch(self, bctype):
147         db = self.connect_db()
148         dbexe = db[1].execute("\
149         SELECT bctype,chtxt,ch,csch,updatetime,status,chname,status,visible,logoupdate,scanupdate \
150         FROM epg_ch \
151         WHERE bctype = %s", \
152                               (bctype,))
153         ret = []
154         dls = []
155         if dbexe > 0:
156             dls = db[1].fetchall()
157         self.close_db(db)
158         for dl in dls:
159             #recdblist.printutf8(dl)
160             r = list(dl)
161             r[4] = r[4].strftime("%Y-%m-%d %H:%M:%S")
162             r[9] = r[9].strftime("%Y-%m-%d %H:%M:%S")
163             r[10] = r[10].strftime("%Y-%m-%d %H:%M:%S")
164             r[5] = str(r[5])
165             ret.append(r)
166         return ret
167     def select_by_ch_epg_ch(self, ch):
168         db = self.connect_db()
169         dbexe = db[1].execute("\
170         SELECT \
171         bctype,chtxt,ch,csch,updatetime,chname,status,visible,logoupdate,scanupdate \
172         FROM epg_ch \
173         WHERE ch = %s", \
174                               (ch,))
175         ret = []
176         dls = []
177         if dbexe > 0:
178             dls = db[1].fetchall()
179         self.close_db(db)
180         for dl in dls:
181             r = list(dl)
182             r[4] = r[4].strftime("%Y-%m-%d %H:%M:%S")
183             r[8] = r[8].strftime("%Y-%m-%d %H:%M:%S")
184             r[9] = r[9].strftime("%Y-%m-%d %H:%M:%S")
185             ret.append(r)
186         return ret
187     def select_by_csch_epg_ch(self, csch):
188         db = self.connect_db()
189         dbexe = db[1].execute("\
190         SELECT \
191         bctype,chtxt,ch,csch,updatetime,chname,status,visible,logoupdate,scanupdate \
192         FROM epg_ch \
193         WHERE csch = %s", \
194                               (csch,))
195         ret = []
196         dls = []
197         if dbexe > 0:
198             dls = db[1].fetchall()
199         self.close_db(db)
200         for dl in dls:
201             r = list(dl)
202             r[4] = r[4].strftime("%Y-%m-%d %H:%M:%S")
203             r[8] = r[8].strftime("%Y-%m-%d %H:%M:%S")
204             r[9] = r[9].strftime("%Y-%m-%d %H:%M:%S")
205             ret.append(r)
206         return ret
207     def select_all_epg_ch(self):
208         db = self.connect_db()
209         dbexe = db[1].execute("\
210         SELECT bctype,chtxt,ch,csch,updatetime,chname,status,visible,logoupdate,scanupdate \
211         FROM epg_ch \
212         ")
213         ret = []
214         dls = []
215         if dbexe > 0:
216             dls = db[1].fetchall()
217         self.close_db(db)
218         for dl in dls:
219             r = list(dl)
220             r[4] = r[4].strftime("%Y-%m-%d %H:%M:%S")
221             r[8] = r[8].strftime("%Y-%m-%d %H:%M:%S")
222             r[9] = r[9].strftime("%Y-%m-%d %H:%M:%S")
223             ret.append(r)
224         return ret
225     def change_visible_epg_ch(self,chtxt,visible):
226         db = self.connect_db()
227         db[1].execute("\
228         UPDATE epg_ch SET visible=%s WHERE chtxt=%s",(visible,chtxt))
229         self.close_db(db)
230     def change_logodata_epg_ch(self,chtxt,logonum,logodata):
231         db = self.connect_db()
232         db[1].execute("\
233         UPDATE epg_ch SET logo"+str(logonum)+"=%s WHERE chtxt=%s",(logodata,chtxt))
234         self.close_db(db)
235     def set_new_status(self,dhour):
236         db = self.connect_db()
237         dbexe = db[1].execute("UPDATE epg_ch \
238         SET status = 1 \
239         WHERE \
240         ( \
241         updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
242         AND \
243         status = 0 \
244         )"\
245         )
246     def select_get_update_epg_ch(self, dhour):
247         db = self.connect_db()
248         dbexe = db[1].execute("SELECT bctype,chtxt,status FROM epg_ch \
249         WHERE (\
250         ( \
251         updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
252         AND \
253         status = 1 \
254         ) \
255         OR \
256         status > 1 )\
257         ORDER BY status DESC")
258         ret = []
259         #recdblist.printutf8(dbexe)
260         if dbexe > 0:
261             ret = db[1].fetchall()
262         self.close_db(db)
263         return ret
264     def select_get_updatelogo_epg_ch(self, dhour):
265         db = self.connect_db()
266         dbexe = db[1].execute("SELECT bctype,chtxt,logostatus FROM epg_ch \
267         WHERE (\
268         ( \
269         updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
270         AND \
271         logostatus = 1 \
272         ) \
273         OR \
274         logostatus > 1 )\
275         ORDER BY status DESC")
276         ret = []
277         #recdblist.printutf8(dbexe)
278         if dbexe > 0:
279             ret = db[1].fetchall()
280         self.close_db(db)
281         return ret
282     def update_by_bctype_epg_ch(self, bctype):
283         db = self.connect_db()
284         db[1].execute("\
285         UPDATE epg_ch \
286         SET \
287         updatetime=now() , \
288         status = 1 \
289         WHERE bctype = %s", (bctype,))
290         self.close_db(db)
291
292     def update_by_bctype_and_chtxt_epg_ch(self, bctype, chtxt):
293         db = self.connect_db()
294         db[1].execute("\
295         UPDATE epg_ch \
296         SET \
297         updatetime=now() , \
298         status = 1\
299         WHERE bctype = %s AND chtxt = %s", (bctype, chtxt))
300         self.close_db(db)
301     def update_chname_by_chtxt_epg_ch(self,chtxt,chname):
302         db = self.connect_db()
303         db[1].execute("\
304         UPDATE epg_ch \
305         SET \
306         chname = %s \
307         WHERE chtxt = %s", (chname,chtxt))
308         self.close_db(db)
309     def update_status_by_bctype_epg_ch(self, bctype, status):
310         db = self.connect_db()
311         db[1].execute("\
312         UPDATE epg_ch \
313         SET \
314         status=%s , \
315         updatetime=now() \
316         WHERE bctype = %s", \
317                       (status, bctype)\
318                       )
319         self.close_db(db)
320     def update_logostatus_by_bctype_epg_ch(self,bctype,logostatus):
321         db = self.connect_db()
322         db[1].execute("\
323         UPDATE epg_ch \
324         SET \
325         logostatus=%s , \
326         logoupdate=now() \
327         WHERE bctype = %s", \
328                       (logostatus, bctype)\
329                       )
330         self.close_db(db)
331     def add_auto_proc_tmp(self,type,title,chtxt):
332         db = self.connect_db()
333         db[1].execute('\
334         INSERT IGNORE into auto_proc_tmp \
335         (type,title,chtxt) \
336         values (%s,%s,%s)',(type,title,chtxt))
337         ##db.commit()
338         self.close_db(db)
339     def new_auto_proc_tmp(self):
340         db = self.connect_db()
341         try:
342             db[1].execute('drop table auto_proc_tmp')
343         except Exception, inst:
344             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and (inst[0]==1050 or inst[0]==1051))):
345                 recdblist.addCommonlogEX("Error", "new_auto_proc_tmp drop (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
346
347         try:
348             db[1].execute('\
349             CREATE TABLE auto_proc_tmp \
350             (\
351             type VARCHAR(20),\
352             title VARCHAR(100) PRIMARY KEY,\
353             chtxt VARCHAR(30),\
354             UNIQUE unibayeskey(title)\
355             )')
356         except Exception, inst:
357             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
358                 recdblist.addCommonlogEX("Error", "new_auto_proc_tmp (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
359
360         self.close_db(db)
361     def update_auto_proc(self):
362         db = self.connect_db()
363         try:
364             db[1].execute('INSERT INTO auto_proc SELECT * FROM auto_proc_tmp')
365         except Exception, inst:
366             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
367                 recdblist.addCommonlogEX("Error", "update_auto_proc (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
368
369         self.close_db(db)
370     def new_auto_proc(self):
371         db = self.connect_db()
372         try:
373             db[1].execute('drop table auto_proc')
374         except Exception, inst:
375             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and (inst[0]==1050 or inst[0]==1051))):
376                 recdblist.addCommonlogEX("Error", "new_auto_proc drop (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
377
378         try:
379             db[1].execute('\
380             CREATE TABLE auto_proc \
381             (\
382             type VARCHAR(20),\
383             title VARCHAR(100) PRIMARY KEY,\
384             chtxt VARCHAR(30),\
385             UNIQUE unibayeskey(title)\
386             )')
387         except Exception, inst:
388             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
389                 recdblist.addCommonlogEX("Error", "new_auto_proc (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
390
391         self.close_db(db)
392     def add_auto_proc(self,type,title,chtxt):
393         db = self.connect_db()
394         db[1].execute('\
395         INSERT IGNORE into auto_proc \
396         (type,title,chtxt) \
397         values (%s,%s,%s)',(type,title,chtxt))
398         ##db.commit()
399         self.close_db(db)
400     def drop_in_settings(self):
401         db = self.connect_db()
402         try:
403             db[1].execute('drop table in_settings')
404         except Exception, inst:
405             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and (inst[0]==1050 or inst[0]==1051))):
406                 recdblist.addCommonlogEX("Error", "drop_in_settings (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
407         self.close_db(db)
408     def new_in_settings(self):
409         db = self.connect_db()
410         try:
411             db[1].execute('\
412             CREATE TABLE in_settings \
413             (\
414             auto_jbk TINYINT,\
415             auto_bayes TINYINT,\
416             auto_opt VARCHAR(20),\
417             auto_del_tmp TINYINT\
418             )')
419             db[1].execute("INSERT IGNORE into in_settings VALUE (0,0,\"H\",1)")
420         except Exception, inst:
421             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
422                 recdblist.addCommonlogEX("Error", "new_in_settings (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
423
424         self.close_db(db)
425     def select_all_in_settings(self):
426         db = self.connect_db()
427         dbexe = db[1].execute("\
428         SELECT auto_jbk,auto_bayes,auto_del_tmp,auto_opt \
429         FROM in_settings \
430         ")
431         ret = []
432         dls = []
433         if dbexe > 0:
434             dls = db[1].fetchall()
435         self.close_db(db)
436         for dl in dls:
437             r = list(dl)
438             r[0]=str(r[0])
439             r[1]=str(r[1])
440             r[2]=str(r[2])
441             r[3]=r[3]
442             ret.append(r)
443         return ret
444     def add_in_timeline_log(self , chtxt="", title="", btime="", etime="", opt="", exp="", longexp="", category=""):
445         db = self.connect_db()
446         db[1].execute('\
447         INSERT IGNORE into in_timeline_log \
448         (chtxt,title,btime,etime,opt,exp,longexp,category) \
449         values (%s,%s,%s,%s,%s,%s,%s,%s)', \
450                       ( chtxt, title, btime, etime, opt,exp,longexp,category))
451         ##db.commit()
452         self.close_db(db)
453     def del_in_timeline_log(self, title="", chtxt="", btime=""):
454         """
455
456         """
457         db = self.connect_db()
458         db[1].execute("\
459         DELETE FROM in_timeline_log \
460         WHERE title = %s AND chtxt = %s AND btime = %s", \
461                       (title, chtxt, btime))
462         #db.commit()
463         self.close_db(db)
464     def new_in_timeline_log(self):
465         db = self.connect_db()
466         try:
467             db[1].execute('\
468             CREATE TABLE in_timeline_log \
469             (\
470             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
471             chtxt VARCHAR(20),\
472             title VARCHAR(100),\
473             btime DATETIME,\
474             etime DATETIME,\
475             opt VARCHAR(20),\
476             exp VARCHAR(200),\
477             longexp TEXT,\
478             category VARCHAR(100),\
479             UNIQUE uni (chtxt,title,btime,category)\
480             )')
481         except Exception, inst:
482             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
483                 recdblist.addCommonlogEX("Error", "new_in_timeline_log (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
484
485         self.close_db(db)
486     def select_chtxt_by_title_timeline_log(self,title):
487         db = self.connect_db()
488         dbexe = db[1].execute("\
489         SELECT chtxt \
490         FROM in_timeline_log \
491         WHERE title LIKE \"%"+title+"%\"\
492         GROUP by chtxt\
493         ORDER by sum(1) DESC limit 1")
494         retdb=db[1].fetchall()
495         ret=""
496         if ret!=None:
497             if len(retdb)>0:
498                 ret=retdb[0][0]
499         self.close_db(db)
500         return ret
501     def add_timeline(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt="" ,counter=-1):
502         db = self.connect_db()
503         db[1].execute('\
504         INSERT IGNORE into timeline \
505         (type,chtxt,title,btime,etime,deltatime,deltaday,opt,counter) \
506         values (%s,%s,%s,%s,%s,%s,%s,%s,%s)', \
507                       (type, chtxt, title, btime, etime, deltatime, deltaday, opt ,counter))
508         ##db.commit()
509         self.close_db(db)
510     def update_epg_timeline(self,type,chtxt,title,btime,epgbtime,epgetime,epgtitle,epgexp,epgcategory):
511         db = self.connect_db()
512         db[1].execute('\
513         UPDATE timeline \
514         SET epgbtime=%s,epgetime=%s,epgtitle=%s,epgexp=%s,epgcategory=%s \
515         WHERE type=%s AND chtxt=%s AND title=%s AND btime=%s ', \
516                       (epgbtime,epgetime,epgtitle,epgexp,epgcategory,type, chtxt, title, btime))
517         ##db.commit()
518         self.close_db(db)
519     def update_status_change_timeline(self,type,chtxt,title,btime,epgchange):
520         db = self.connect_db()
521         db[1].execute('\
522         UPDATE timeline \
523         SET epgchange =%s \
524         WHERE type=%s AND chtxt=%s AND title=%s AND btime=%s ', \
525                       (epgchange , type, chtxt, title, btime))
526         ##db.commit()
527         self.close_db(db)
528     def update_status_dup_timeline(self,type,chtxt,title,btime,epgduplicate):
529         db = self.connect_db()
530         db[1].execute('\
531         UPDATE timeline \
532         SET epgduplicate =%s \
533         WHERE type=%s AND chtxt=%s AND title=%s AND btime=%s ', \
534                       (epgduplicate , type, chtxt, title, btime))
535         ##db.commit()
536         self.close_db(db)
537     def del_timeline(self, type="", title="", chtxt="", btime=""):
538         """
539
540         """
541         db = self.connect_db()
542         db[1].execute("\
543         DELETE FROM timeline \
544         WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
545                       (type, title, chtxt, btime))
546         #db.commit()
547         self.close_db(db)
548     def select_all_timeline(self):
549         db = self.connect_db()
550         recdata = []
551         dbr = db[1].execute("\
552         SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt ,epgbtime ,epgetime ,epgtitle ,epgduplicate ,epgchange ,counter\
553         FROM timeline")
554         dbl = db[1].fetchall()
555         self.close_db(db)
556         if dbr > 0:
557             recdata = self.getdic_timeline(dbl)
558         return recdata
559     def select_bytime_timeline(self, dminutes):
560         db = self.connect_db()
561         recdatum = []
562         #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 )")
563         dbr = db[1].execute("SELECT \
564         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt ,epgbtime ,epgetime ,epgtitle ,epgduplicate ,epgchange ,counter\
565         FROM timeline \
566         WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
567         DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
568         dbl = db[1].fetchall()
569         self.close_db(db)
570         #recdblist.printutf8(dbl)
571         if dbr > 0:
572             recdatum = self.getdic_timeline(dbl)
573         return recdatum
574     def select_by_name_time_timeline(self,title,btime,btime2):
575         db = self.connect_db()
576         recdatum = []
577         #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 )")
578         dbr = db[1].execute("SELECT \
579         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt ,epgbtime ,epgetime ,epgtitle ,epgduplicate ,epgchange ,counter\
580         FROM timeline \
581         WHERE btime > %s AND \
582         btime < %s AND title = %s",(btime,btime2,title))
583         dbl = db[1].fetchall()
584         self.close_db(db)
585         #recdblist.printutf8(dbl)
586         if dbr > 0:
587             recdatum = self.getdic_timeline(dbl)
588         return recdatum
589     def select_bytime_all_timeline(self,btime,etime):
590         db = self.connect_db()
591         recdatum = []
592         #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 )")
593         dbr = db[1].execute("SELECT \
594         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt ,epgbtime ,epgetime ,epgtitle ,epgduplicate ,epgchange ,counter\
595         FROM timeline \
596         WHERE btime >= %s AND \
597         etime <= %s",(btime,etime))
598         dbl = db[1].fetchall()
599         self.close_db(db)
600         #recdblist.printutf8(dbl)
601         if dbr > 0:
602             recdatum = self.getdic_timeline(dbl)
603         return recdatum
604     def select_byepgtime_all_timeline(self,epgbtime,epgetime):
605         db = self.connect_db()
606         recdatum = []
607         #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 )")
608         dbr = db[1].execute("SELECT \
609         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt ,epgbtime ,epgetime ,epgtitle ,epgduplicate ,epgchange ,counter\
610         FROM timeline \
611         WHERE epgbtime >= %s AND \
612         epgetime <= %s",(epgbtime,epgetime))
613         dbl = db[1].fetchall()
614         self.close_db(db)
615         #recdblist.printutf8(dbl)
616         if dbr > 0:
617             recdatum=self.getdic_timeline(dbl)
618         return recdatum
619     def select_byepgtime_over_timeline(self,epgbtime,epgetime):
620         db = self.connect_db()
621         recdatum = []
622         #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 )")
623         dbr = db[1].execute("SELECT \
624         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt ,epgbtime ,epgetime ,epgtitle ,epgduplicate ,epgchange ,counter\
625         FROM timeline \
626         WHERE (NOT(( timeline.epgetime <= %s )OR( timeline.epgbtime >= %s )))"\
627         ,(epgbtime,epgetime))
628         dbl = db[1].fetchall()
629         self.close_db(db)
630         #recdblist.printutf8(dbl)
631         if dbr > 0:
632             recdatum=self.getdic_timeline(dbl)
633         return recdatum
634     def count_schedule_timeline(self, btime, etime):
635         """
636         count rectasknum
637         return [te num,bs/cs num]
638         """
639         db = self.connect_db()
640         dbexe = "SELECT type,epg_ch.bctype,timeline.chtxt,title FROM timeline INNER JOIN epg_ch ON timeline.chtxt=epg_ch.chtxt"
641         dbexe = dbexe + " WHERE ((NOT(( timeline.etime <= %s )OR( timeline.btime >= %s ))) OR ((timeline.btime = %s) AND (timeline.etime = %s) ) )"
642         Srec = 0
643         Trec = 0
644         db[1].execute(dbexe, (btime, etime,btime,etime))
645         dbl=db[1].fetchall()
646         for typet, bctypet, chtxtt, titlet in dbl:
647             if (typet == recdblist.REC_RESERVE) or (typet == recdblist.REC_FINAL_RESERVE) or (typet == recdblist.REC_KEYWORD) or (typet == recdblist.REC_KEYWORD_EVERY_SOME_DAYS):
648                 if bctypet.find("cs") > -1:
649                     Srec = Srec + 1
650                 elif bctypet.find("bs") > -1:
651                     Srec = Srec + 1
652                 elif bctypet.find("te") > -1:
653                     Trec = Trec + 1
654         self.close_db(db)
655         return [Trec, Srec]
656     def search_schedule_timeline(self,btime,etime):
657         """
658         count rectasknum
659         return [(type,bctype,chtxt,title,btime,etime)]
660         """
661         db = self.connect_db()
662         dbexe = "SELECT type,epg_ch.bctype,timeline.chtxt,title,timeline.btime,timeline.etime FROM timeline INNER JOIN epg_ch ON timeline.chtxt=epg_ch.chtxt"
663         dbexe = dbexe + " WHERE ((NOT(( timeline.etime <= %s )OR( timeline.btime >= %s ))) OR ((timeline.btime = %s) AND (timeline.etime = %s) ) )"
664         ret=[]
665         db[1].execute(dbexe, (btime, etime,btime,etime))
666         dbl=db[1].fetchall()
667         for typet, bctypet, chtxtt, titlet , btimet, etimet in dbl:
668             if (typet == recdblist.REC_RESERVE) or (typet == recdblist.REC_FINAL_RESERVE) or (typet == recdblist.REC_KEYWORD) or (typet == recdblist.REC_KEYWORD_EVERY_SOME_DAYS):
669                 ret.append([typet,bctypet,chtxtt,titlet,btimet,etimet])
670         self.close_db(db)
671         return ret
672     def count_epgschedule_timeline(self, epgbtime, epgetime):
673         """
674         count rectasknum
675         return [te num,bs/cs num]
676         """
677         db = self.connect_db()
678         dbexe = "SELECT type,epg_ch.bctype,timeline.chtxt,title FROM timeline INNER JOIN epg_ch ON timeline.chtxt=epg_ch.chtxt"
679         dbexe = dbexe + " WHERE (NOT(( timeline.epgetime <= %s )OR( timeline.epgbtime >= %s )))"
680         Srec = 0
681         Trec = 0
682         db[1].execute(dbexe, (epgbtime, epgetime))
683         dbl=db[1].fetchall()
684         for typet, bctypet, chtxtt, titlet in dbl:
685             if (typet == recdblist.REC_RESERVE) or (typet == recdblist.REC_FINAL_RESERVE) or (typet == recdblist.REC_KEYWORD) or (typet == recdblist.REC_KEYWORD_EVERY_SOME_DAYS):
686                 if bctypet.find("cs") > -1:
687                     Srec = Srec + 1
688                 elif bctypet.find("bs") > -1:
689                     Srec = Srec + 1
690                 elif bctypet.find("te") > -1:
691                     Trec = Trec + 1
692         self.close_db(db)
693         return [Trec, Srec]
694     def search_epgschedule_timeline(self,epgbtime,epgetime):
695         """
696         count rectasknum
697         return [(type,bctype,chtxt,title,btime,etime)]
698         """
699         db = self.connect_db()
700         dbexe = "SELECT type,epg_ch.bctype,timeline.chtxt,title,timeline.btime,timeline.etime FROM timeline INNER JOIN epg_ch ON timeline.chtxt=epg_ch.chtxt"
701         dbexe = dbexe + " WHERE ((NOT(( timeline.epgetime <= %s )OR( timeline.epgbtime >= %s ))) OR ((timeline.epgbtime = %s) AND (timeline.epgetime = %s) ) )"
702         ret=[]
703         db[1].execute(dbexe, (epgbtime, epgetime,epgbtime,epgetime))
704         dbl=db[1].fetchall()
705         for typet, bctypet, chtxtt, titlet , btimet, etimet in dbl:
706             if (typet == recdblist.REC_RESERVE) or (typet == recdblist.REC_FINAL_RESERVE) or (typet == recdblist.REC_KEYWORD) or (typet == recdblist.REC_KEYWORD_EVERY_SOME_DAYS):
707                 ret.append([typet,bctypet,chtxtt,titlet,btimet,etimet])
708         self.close_db(db)
709         return ret
710     def select_bytime_bychtxt_all_timeline(self,btime,etime,chtxt):
711         db = self.connect_db()
712         recdatum = []
713         #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 )")
714         dbr = db[1].execute("SELECT \
715         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt ,epgbtime ,epgetime ,epgtitle ,epgduplicate ,epgchange ,counter\
716         FROM timeline \
717         WHERE btime > %s AND \
718         etime < %s\
719         AND chtxt=%s ",(btime,etime,chtxt))
720         dbl = db[1].fetchall()
721         self.close_db(db)
722         #recdblist.printutf8(dbl)
723         if dbr > 0:
724             recdatum = self.getdic_timeline(dbl)
725         return recdatum
726     def getdic_timeline(self,timelinelists):
727         recdatum=[]
728         for typet, chtxt, title, btime, etime, deltatime, deltaday, opt ,epgbtimet , epgetimet ,epgtitlet ,epgduplicatet ,epgchanget ,countert in timelinelists:
729             ret = {}
730             ret['type'] = typet
731             ret['chtxt'] = chtxt
732             ret['title'] = title
733             btime = btime.strftime("%Y-%m-%d %H:%M:%S")
734             etime = etime.strftime("%Y-%m-%d %H:%M:%S")
735             ret['btime'] = btime
736             ret['etime'] = etime
737             ret['opt'] = opt
738             try:
739                 ret['epgbtime'] = epgbtimet.strftime("%Y-%m-%d %H:%M:%S")
740                 ret['epgetime'] = epgetimet.strftime("%Y-%m-%d %H:%M:%S")
741             except:
742                 ret['epgbtime'] = "2010-01-01 00:00:00"
743                 ret['epgetime'] = "2010-01-01 00:00:00"
744             ret['epgtitle'] = epgtitlet
745             ret['epgduplicate'] = epgduplicatet
746             ret['epgchange'] = epgchanget
747             if deltatime == None or deltatime == "":
748                 deltatime = "3"
749             if deltaday == None or deltaday == "":
750                 deltaday = "7"
751             if typet == recdblist.REC_KEYWORD:
752                 ret['deltatime'] = deltatime
753             elif typet == recdblist.REC_KEYWORD_EVERY_SOME_DAYS:
754                 ret['deltatime'] = deltatime
755                 ret['deltaday'] = deltaday
756             try:
757                 ret['counter'] = int(countert)
758             except:
759                 ret['counter']=-1
760             recdatum.append(ret)
761         return recdatum
762     def delete_old_timeline(self, dhour):
763         db = self.connect_db()
764         db[1].execute("\
765         DELETE FROM timeline \
766         WHERE \
767         btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
768         #db.commit()
769         self.close_db(db)
770     def new_in_auto_jbk_key(self):
771         db = self.connect_db()
772         try:
773             db[1].execute("\
774             CREATE TABLE in_auto_jbk_key \
775             (\
776             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
777             keyword VARCHAR(200),\
778             auto TINYINT DEFAULT 0,\
779             opt VARCHAR(20),\
780             UNIQUE unijbk (keyword)\
781             )")
782         except Exception, inst:
783             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
784                 recdblist.addCommonlogEX("Error", "new_in_auto_jbk_key (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
785         self.close_db(db)
786     def add_in_auto_jbk_key(self,key):
787         db = self.connect_db()
788         db[1].execute('\
789         INSERT IGNORE into in_auto_jbk_key \
790         (keyword) \
791         values (%s)', \
792                       (key,))
793         ##db.commit()
794         self.close_db(db)
795     def select_all_in_auto_jbk_key(self):
796         db = self.connect_db()
797         dbexe = db[1].execute("\
798         SELECT keyword,auto,opt \
799         FROM in_auto_jbk_key \
800 vim         ")
801         ret = []
802         if dbexe > 0:
803             ret = db[1].fetchall()
804         self.close_db(db)
805         return ret
806     def drop_in_status(self):
807         db = self.connect_db()
808         try:
809             db[1].execute('drop table in_status')
810         except Exception, inst:
811             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and (inst[0]==1050 or inst[0]==1051))):
812                 recdblist.addCommonlogEX("Error", "drop_in_status (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
813         self.close_db(db)
814     def new_in_status(self):
815         db = self.connect_db()
816         try:
817             db[1].execute("\
818             CREATE TABLE in_status \
819             (\
820             ts2avi TINYINT DEFAULT 0,\
821             terec TINYINT DEFAULT 0,\
822             bscsrec TINYINT DEFAULT 0,\
823             b252ts TINYINT DEFAULT 0,\
824             installed TINYINT DEFAULT 0,\
825             version TINYINT\
826             )")
827             db[1].execute("INSERT IGNORE into in_status VALUE (0,0,0,0,0,0)")
828         except Exception, inst:
829             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
830                 recdblist.addCommonlogEX("Error", "new_in_status (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
831         self.close_db(db)
832     def select_all_in_status(self):
833         db = self.connect_db()
834         dbexe = db[1].execute("\
835         SELECT ts2avi,terec,bscsrec,b252ts \
836         FROM in_status \
837         ")
838         ret = []
839         dls = []
840         if dbexe > 0:
841             dls = db[1].fetchall()
842         self.close_db(db)
843         for dl in dls:
844             r = list(dl)
845             r[0]=str(r[0])
846             r[1]=str(r[1])
847             r[2]=str(r[2])
848             r[3]=str(r[3])
849             ret.append(r)
850         return ret
851     def select_version_in_status(self):
852         db = self.connect_db()
853         version=0
854         try:
855             dbexe = db[1].execute("\
856             SELECT version \
857             FROM in_status \
858             ")
859             if dbexe > 0:
860                 dls = db[1].fetchall()
861             self.close_db(db)
862             for dl in dls:
863                 r = list(dl)
864                 version=int(str(r[0]))
865         except Exception, inst:
866             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
867                 recdblist.addCommonlogEX("Error", "select_version_in_status (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
868         return version
869     def change_version_in_status(self,version):
870         db = self.connect_db()
871         db[1].execute("\
872         UPDATE in_status SET version=%s",str(version))
873         self.close_db(db)
874     def change_ts2avi_in_status(self,i):
875         """
876         statuをiだけ増減する
877         iはint
878         """
879         db = self.connect_db()
880         db[1].execute("\
881         UPDATE in_status SET ts2avi=ts2avi+%s",i)
882         self.close_db(db)
883     def change_terec_in_status(self,i):
884         """
885         statuをiだけ増減する
886         iはint
887         """
888         db = self.connect_db()
889         db[1].execute("\
890         UPDATE in_status SET terec=terec+%s",i)
891         self.close_db(db)
892     def change_bscsrec_in_status(self,i):
893         """
894         statuをiだけ増減する
895         iはint
896         """
897         db = self.connect_db()
898         db[1].execute("\
899         UPDATE in_status SET bscsrec=bscsrec+%s",i)
900         self.close_db(db)
901     def change_b252ts_in_status(self,i):
902         """
903         statuをiだけ増減する
904         iはint
905         """
906         db = self.connect_db()
907         db[1].execute("\
908         UPDATE in_status SET b252ts=b252ts+%s",i)
909         self.close_db(db)
910     def select_installed_in_status(self):
911         db = self.connect_db()
912         dbexe = db[1].execute("\
913         SELECT ts2avi,terec,bscsrec,b252ts,installed \
914         FROM in_status \
915         ")
916         ret = 0
917         dls = []
918         if dbexe > 0:
919             dls = db[1].fetchall()
920         self.close_db(db)
921         for dl in dls:
922             r = list(dl)
923             ret=r[4]
924         return ret
925     def change_chscaned_in_status(self):
926         """
927         installedを設定する
928         """
929         db = self.connect_db()
930         db[1].execute("\
931         UPDATE in_status SET installed=2")
932         self.close_db(db)
933     def change_installed_in_status(self,num=1):
934         """
935         installedを設定する
936         """
937         db = self.connect_db()
938         db[1].execute("\
939         UPDATE in_status SET installed=%s",(num,))
940         self.close_db(db)
941     def new_epg_timeline(self, bctype):
942         db = self.connect_db()
943         try:
944             db[1].execute("\
945             DELETE FROM epg_timeline \
946             WHERE bctype = %s", \
947                           (bctype,))
948         except Exception, inst:
949             if not ((type(inst)==MySQLdb.ProgrammingError and (inst[0]==1007 or inst[0]==1146))or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
950                 recdblist.addCommonlogEX("Error", "new_epg_timeline delete (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
951         try:
952             db[1].execute("\
953             CREATE TABLE epg_timeline \
954             (\
955             bctype VARCHAR(20),\
956             channel VARCHAR(100) NOT NULL,\
957             start VARCHAR(30),\
958             stop  VARCHAR(30),\
959             title VARCHAR(100),\
960             exp VARCHAR(200),\
961             longexp TEXT,\
962             category VARCHAR(100),\
963             UNIQUE unitv(bctype,channel,start,stop,title)\
964             )")
965         except Exception, inst:
966             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
967                 recdblist.addCommonlogEX("Error", "new_epg_timeline (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
968         #db.commit()
969         self.close_db(db)
970     def add_epg_timeline(self, bctype, channel, start, stop, title, desc,longdesc, category):
971         db = self.connect_db()
972         db[1].execute('\
973         INSERT IGNORE INTO epg_timeline \
974         VALUES (%s,%s,%s,%s,%s,%s,%s,%s)', \
975                       (bctype, channel, start, stop, title, desc,longdesc,category))
976         #db.commit()
977         self.close_db(db)
978     def add_multi_epg_timeline(self, tvlists):
979         """
980         tvlists is (bctype,channel,start,stop,title,desc,longdesc,category) lists.
981         """
982         db = self.connect_db()
983         db[1].executemany('\
984         INSERT IGNORE INTO epg_timeline \
985         (bctype,channel,start,stop,title,exp,longexp,category) \
986         values(%s,%s,%s,%s,%s,%s,%s,%s)', \
987                           tvlists)
988         self.close_db(db)
989     def select_by_time_ngram_epg_timeline(self, btime, etime, chtxt):
990         db = self.connect_db()
991         dbexe = "\
992         SELECT \
993         channel,title,start,stop,exp,longexp,category \
994         FROM epg_timeline \
995         WHERE start >= %s \
996         AND \
997         start <= %s \
998         AND \
999         channel LIKE %s"
1000         dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
1001         retall = []
1002         if dbcmd > 0:
1003             retall = db[1].fetchall()
1004         self.close_db(db)
1005         return retall
1006     def select_by_time_keyword_auto_suggest_epg_timeline(self,keyword,btime,etime):
1007         db = self.connect_db()
1008         dbexe = "\
1009         SELECT \
1010         epg_ch.chtxt,title,start,stop,exp,longexp,category \
1011         FROM epg_timeline \
1012         INNER JOIN epg_ch \
1013         WHERE epg_ch.chtxt=epg_timeline.channel \
1014         AND \
1015         epg_ch.visible=1 \
1016         AND \
1017         start >= %s \
1018         AND \
1019         stop <= %s \
1020         AND \
1021         ( \
1022         ( title LIKE \'%%"+keyword+"%%\' ) \
1023         OR \
1024         ( exp LIKE \'%%"+keyword+"%%\' ) \
1025         OR \
1026         ( longexp LIKE \'%%"+keyword+"%%\' ) \
1027         )"
1028         dbcmd = db[1].execute(dbexe,(btime, etime))
1029         retall = []
1030         if dbcmd > 0:
1031             retall = db[1].fetchall()
1032         self.close_db(db)
1033         return retall
1034     def new_in_auto_bayes_key(self):
1035         db = self.connect_db()
1036         try:
1037             db[1].execute('CREATE TABLE in_auto_bayes_key \
1038             (\
1039             keychar VARCHAR(10),\
1040             chtxt VARCHAR(20),\
1041             ratio_rec DECIMAL(32,14),\
1042             ratio_all DECIMAL(32,14),\
1043             UNIQUE unibayeskey(keychar,chtxt)\
1044             )')
1045             db[1].execute('CREATE INDEX keycharindex ON in_auto_bayes_key(keychar)')
1046         except Exception, inst:
1047             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1048                 recdblist.addCommonlogEX("Error", "new_in_auto_bayes_key (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1049         self.close_db(db)
1050
1051     def add_in_auto_bayes_key(self,key,chtxt,ratio_rec,ratio_all):
1052         db = self.connect_db()
1053         ratio_rec=str(ratio_rec)
1054         ratio_all=str(ratio_all)
1055         db[1].execute('\
1056         INSERT IGNORE INTO in_auto_bayes_key \
1057         (keychar,chtxt,ratio_rec,ratio_all) \
1058         values (%s,%s,%s,%s)',\
1059         (key,chtxt,ratio_rec,ratio_all))
1060         self.close_db(db)
1061     def add_num_in_auto_bayes_key(self,chtxt,add_rec_num,add_all_num):
1062         db = self.connect_db()
1063         add_rec_num=str(add_rec_num)
1064         add_all_num=str(add_all_num)
1065         db[1].execute("\
1066         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",\
1067         (add_rec_num,add_all_num,chtxt))
1068         self.close_db(db)
1069     def change_in_auto_bayes_key(self,key,chtxt,new_ratio_rec,new_ratio_all):
1070         """
1071         """
1072         db = self.connect_db()
1073         db[1].execute("\
1074         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)\
1075         )
1076         self.close_db(db)
1077     def change_ratio_all_reduce_in_auto_bayes_key(self,chtxt,beforenum,newnum):
1078         beforenum=str(beforenum)
1079         newnum=str(newnum)
1080         db = self.connect_db()
1081         db[1].execute("\
1082         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)\
1083         )
1084         self.close_db(db)
1085     def change_ratio_all_in_auto_bayes_key(self,key,chtxt,beforenum,addnum):
1086         db = self.connect_db()
1087         beforenumf=beforenum
1088         beforenum=str(beforenum)
1089         db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
1090         VALUES (%s,%s,%s,%s)\
1091         ON DUPLICATE KEY UPDATE \
1092         ratio_all=CONVERT((ratio_all*%s+%s)/%s,DECIMAL(32,14))",(key,chtxt,"0",str(Decimal(addnum)/beforenumf),beforenum,chtxt,key))
1093         self.close_db(db)
1094     def change_multi_ratio_all_in_auto_bayes_key(self,chtxt,beforenum,list):
1095         """
1096         list={key:addnum}のリスト
1097         """
1098         beforenumf=beforenum
1099         beforenum=str(beforenum)
1100         db = self.connect_db()
1101         for i,j in list.items():
1102             retl=(i,chtxt,"0",str(Decimal(j)/beforenumf),beforenum,str(j),beforenum)
1103             try:
1104                 db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
1105                 VALUES (%s,%s,%s,%s)\
1106                 ON DUPLICATE KEY UPDATE \
1107                 ratio_all=CONVERT((ratio_all*%s+%s)/%s,DECIMAL(32,14))",retl)
1108             except Exception, inst:
1109                 if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1110                     recdblist.addCommonlogEX("Error", "change_multi_ratio_all_in_auto_bayes_key (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1111         self.close_db(db)
1112     def change_ratio_rec_reduce_in_auto_bayes_key(self,chtxt,beforenum,newnum):
1113         beforenum=str(beforenum)
1114         newnum=str(newnum)
1115         db = self.connect_db()
1116         db[1].execute("\
1117         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)\
1118         )
1119         self.close_db(db)
1120     def change_ratio_rec_in_auto_bayes_key(self,key,chtxt,beforenum,addnum):
1121         db = self.connect_db()
1122         beforenumf=beforenum
1123         beforenum=str(beforenum)
1124         db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
1125         VALUES (%s,%s,%s,%s)\
1126         ON DUPLICATE KEY UPDATE \
1127         ratio_rec=CONVERT((ratio_rec*%s+%s)/%s,DECIMAL(32,14))",(key,chtxt,str(Decimal(addnum)/beforenumf),"0",beforenum,chtxt,key))
1128         self.close_db(db)
1129     def change_multi_ratio_rec_in_auto_bayes_key(self,chtxt,beforenum,list):#self,key,chtxt,beforenum,addnum):
1130         beforenumf=beforenum
1131         beforenum=str(beforenum)
1132         db = self.connect_db()
1133         for i,j in list.items():
1134             retl=(i,chtxt,str(Decimal(j)/beforenumf),"0",beforenum,str(j),beforenum)
1135             try:
1136                 db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
1137                 VALUES (%s,%s,%s,%s)\
1138                 ON DUPLICATE KEY UPDATE \
1139                 ratio_rec=CONVERT((ratio_rec*%s+%s)/%s,DECIMAL(32,14))",retl)
1140             except Exception, inst:
1141                 if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1142                     recdblist.addCommonlogEX("Error", "change_multi_ratio_rec_in_auto_bayes_key (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1143         self.close_db(db)
1144     def select_by_key_in_auto_bayes_key(self,key,chtxt):
1145         db = self.connect_db()
1146         dbexe = db[1].execute("\
1147         SELECT keychar,chtxt,ratio_rec,ratio_all \
1148         FROM in_auto_bayes_key \
1149         WHERE keychar = %s AND chtxt = %s", \
1150                               (key,chtxt))
1151         dls = []
1152         if dbexe > 0:
1153             dls = db[1].fetchall()
1154         self.close_db(db)
1155         if len(dls)>0:
1156             return dls[0]
1157         else:
1158             return dls
1159     def new_auto_timeline_keyword(self):
1160         db = self.connect_db()
1161         try:
1162             db[1].execute('\
1163             CREATE TABLE auto_timeline_keyword \
1164             (\
1165             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
1166             chtxt VARCHAR(40),\
1167             title VARCHAR(100),\
1168             btime DATETIME,\
1169             etime DATETIME,\
1170             UNIQUE uni (chtxt,title,btime,etime)\
1171             )')
1172         except Exception, inst:
1173             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1174                 recdblist.addCommonlogEX("Error", "new_auto_timeline_keyword (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1175     def add_auto_timeline_keyword(self,chtxt="", title="", btime="", etime=""):
1176         db = self.connect_db()
1177         db[1].execute('\
1178         INSERT IGNORE into auto_timeline_keyword \
1179         (chtxt,title,btime,etime) \
1180         values (%s,%s,%s,%s)', \
1181                        (chtxt, title, btime, etime))
1182         ##db.commit()
1183         self.close_db(db)
1184     def delete_old_auto_timeline_keyword(self, dhour):
1185         db = self.connect_db()
1186         db[1].execute("\
1187         DELETE FROM auto_timeline_keyword \
1188         WHERE \
1189         btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
1190         self.close_db(db)
1191     def new_auto_timeline_bayes(self):
1192         db = self.connect_db()
1193         try:
1194             db[1].execute('\
1195             CREATE TABLE auto_timeline_bayes \
1196             (\
1197             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
1198             chtxt VARCHAR(40),\
1199             title VARCHAR(100),\
1200             btime DATETIME,\
1201             etime DATETIME,\
1202             point INT,\
1203             UNIQUE uni (chtxt,title,btime,etime)\
1204             )')
1205         except Exception, inst:
1206             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1207                 recdblist.addCommonlogEX("Error", "new_auto_timeline_bayes (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1208     def add_auto_timeline_bayes(self,chtxt="", title="", btime="", etime="",point=""):
1209         db = self.connect_db()
1210         db[1].execute('\
1211         INSERT IGNORE into auto_timeline_bayes \
1212         (chtxt,title,btime,etime,point) \
1213         values (%s,%s,%s,%s,%s)', \
1214                       (chtxt, title, btime, etime,point))
1215         self.close_db(db)
1216     def delete_old_auto_timeline_bayes(self, dhour):
1217         db = self.connect_db()
1218         db[1].execute("\
1219         DELETE FROM auto_timeline_bayes \
1220         WHERE \
1221         btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
1222         self.close_db(db)
1223     def update_db_to93(self):
1224         db = self.connect_db()
1225         self.drop_in_settings()
1226         self.new_in_settings()
1227         db[1].execute("\
1228         ALTER TABLE timeline ADD epgtitle VARCHAR(100),\
1229         ADD epgbtime DATETIME,\
1230         ADD epgetime DATETIME,\
1231         ADD epgduplicate TINYINT DEFAULT 0,\
1232         ADD epgchange TINYINT DEFAULT 0")
1233         db[1].execute("\
1234         ALTER TABLE in_status ADD version TINYINT")
1235         self.close_db(db)
1236         self.change_version_in_status("93")
1237     def update_db_93to94(self):
1238         db = self.connect_db()
1239         self.drop_in_settings()
1240         self.new_in_settings()
1241         db[1].execute("\
1242         ALTER TABLE timeline ADD counter TINYINT DEFAULT -1")
1243         self.close_db(db)
1244         self.change_version_in_status("94")
1245     def update_db_94to95(self):
1246         db = self.connect_db()
1247         self.drop_in_settings()
1248         self.new_in_settings()
1249         db[1].execute("\
1250         ALTER TABLE timeline ADD epgexp VARCHAR(200)")
1251         self.close_db(db)
1252         self.change_version_in_status("95")
1253     def update_db_95to96(self):
1254         db = self.connect_db()
1255         self.drop_in_settings()
1256         self.new_in_settings()
1257         self.close_db(db)
1258         self.change_version_in_status("96")
1259     def update_db_96to98(self):
1260         db = self.connect_db()
1261         self.drop_in_settings()
1262         self.new_in_settings()
1263         self.close_db(db)
1264         self.change_version_in_status("98")
1265     def update_db_98to100(self):
1266         ###ここで前のepg_chをバックアップしてchtxtの変換をする必要がある。
1267         self.drop_in_settings()
1268         self.new_in_settings()
1269         db = self.connect_db()
1270         db[1].execute("\
1271         UPDATE timeline INNER JOIN epg_ch ON timeline.chtxt=epg_ch.chtxt SET timeline.chtxt=CONCAT(CONCAT(epg_ch.ch,'_'),epg_ch.csch) WHERE NOT (substring(epg_ch.bctype,1,2) = 'bs' OR substring(epg_ch.bctype,1,2) = 'cs')")
1272         db[1].execute("\
1273         UPDATE timeline INNER JOIN epg_ch ON timeline.chtxt=epg_ch.chtxt SET timeline.chtxt=CONCAT('BS_',epg_ch.ch) WHERE substring(epg_ch.bctype,1,2) = 'bs'")
1274         db[1].execute("\
1275         UPDATE timeline INNER JOIN epg_ch ON timeline.chtxt=epg_ch.chtxt SET timeline.chtxt=CONCAT('CS_',epg_ch.csch) WHERE substring(epg_ch.bctype,1,2) = 'cs'")
1276         try:
1277             db[1].execute("\
1278             ALTER TABLE epg_ch DROP ontv")
1279         except:
1280             ""
1281         db[1].execute("\
1282         ALTER TABLE epg_ch ADD logo0 BLOB,\
1283         ADD logo1 BLOB,\
1284         ADD logo2 BLOB,\
1285         ADD logo3 BLOB,\
1286         ADD logo4 BLOB,\
1287         ADD logo5 BLOB\
1288         ")
1289         db[1].execute("\
1290         ALTER TABLE in_auto_jbk_key ADD auto TINYINT DEFAULT 0")
1291         db[1].execute("\
1292         ALTER TABLE in_auto_jbk_key ADD opt VARCHAR(20) DEFAULT \"\"")
1293         self.close_db(db)
1294         self.change_installed_in_status(1)#チャンネルスキャンをさせる
1295         self.change_version_in_status("100")
1296     def update_db_100to101(self):
1297         self.drop_in_settings()
1298         self.new_in_settings()
1299         self.new_epg_ch()
1300         db = self.connect_db()
1301         try:
1302             db[1].execute("\
1303             ALTER TABLE timeline ADD epgcategory VARCHAR(100)\
1304             ")
1305         except:
1306             ""
1307         self.close_db(db)
1308         self.change_installed_in_status(1)
1309         self.change_version_in_status("101")
1310         #self.change_installed_in_status()#チャンネルスキャンをさせる
1311