OSDN Git Service

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