OSDN Git Service

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