OSDN Git Service

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