OSDN Git Service

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