OSDN Git Service

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