OSDN Git Service

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