OSDN Git Service

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