OSDN Git Service

implement to work "a bit after start resolution changing movie".
[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         print dbexe
923         dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
924         retall = []
925         if dbcmd > 0:
926             retall = db[1].fetchall()
927         self.close_db(db)
928         return retall
929     def select_by_time_keyword_auto_suggest_epg_timeline(self,keyword,btime,etime):
930         db = self.connect_db()
931         dbexe = "\
932         SELECT \
933         epg_ch.chtxt,title,start,stop,exp,longexp,category \
934         FROM epg_timeline \
935         INNER JOIN epg_ch \
936         WHERE epg_ch.chtxt=epg_timeline.channel \
937         AND \
938         start >= %s \
939         AND \
940         stop <= %s \
941         AND \
942         ( \
943         ( title LIKE \'%%"+keyword+"%%\' ) \
944         OR \
945         ( exp LIKE \'%%"+keyword+"%%\' ) \
946         OR \
947         ( longexp LIKE \'%%"+keyword+"%%\' ) \
948         )"
949         dbcmd = db[1].execute(dbexe,(btime, etime))
950         retall = []
951         if dbcmd > 0:
952             retall = db[1].fetchall()
953         self.close_db(db)
954         return retall
955     def new_in_auto_bayes_key(self):
956         db = self.connect_db()
957         try:
958             db[1].execute('CREATE TABLE in_auto_bayes_key \
959             (\
960             keychar VARCHAR(10),\
961             chtxt VARCHAR(20),\
962             ratio_rec DECIMAL(32,14),\
963             ratio_all DECIMAL(32,14),\
964             UNIQUE unibayeskey(keychar,chtxt)\
965             )')
966             db[1].execute('CREATE INDEX keycharindex ON in_auto_bayes_key(keychar)')
967         except Exception, inst:
968             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
969                 recdblist.Commonlogex("Error", "new_in_auto_bayes_key (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
970         self.close_db(db)
971
972     def add_in_auto_bayes_key(self,key,chtxt,ratio_rec,ratio_all):
973         db = self.connect_db()
974         ratio_rec=str(ratio_rec)
975         ratio_all=str(ratio_all)
976         db[1].execute('\
977         INSERT IGNORE INTO in_auto_bayes_key \
978         (keychar,chtxt,ratio_rec,ratio_all) \
979         values (%s,%s,%s,%s)',\
980         (key,chtxt,ratio_rec,ratio_all))
981         self.close_db(db)
982     def add_num_in_auto_bayes_key(self,chtxt,add_rec_num,add_all_num):
983         db = self.connect_db()
984         add_rec_num=str(add_rec_num)
985         add_all_num=str(add_all_num)
986         db[1].execute("\
987         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",\
988         (add_rec_num,add_all_num,chtxt))
989         self.close_db(db)
990     def change_in_auto_bayes_key(self,key,chtxt,new_ratio_rec,new_ratio_all):
991         """
992         """
993         db = self.connect_db()
994         db[1].execute("\
995         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)\
996         )
997         self.close_db(db)
998     def change_ratio_all_reduce_in_auto_bayes_key(self,chtxt,beforenum,newnum):
999         beforenum=str(beforenum)
1000         newnum=str(newnum)
1001         db = self.connect_db()
1002         db[1].execute("\
1003         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)\
1004         )
1005         self.close_db(db)
1006     def change_ratio_all_in_auto_bayes_key(self,key,chtxt,beforenum,addnum):
1007         db = self.connect_db()
1008         beforenumf=beforenum
1009         beforenum=str(beforenum)
1010         db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
1011         VALUES (%s,%s,%s,%s)\
1012         ON DUPLICATE KEY UPDATE \
1013         ratio_all=CONVERT((ratio_all*%s+%s)/%s,DECIMAL(32,14))",(key,chtxt,"0",str(Decimal(addnum)/beforenumf),beforenum,chtxt,key))
1014         self.close_db(db)
1015     def change_multi_ratio_all_in_auto_bayes_key(self,chtxt,beforenum,list):
1016         """
1017         list={key:addnum}のリスト
1018         """
1019         beforenumf=beforenum
1020         beforenum=str(beforenum)
1021         db = self.connect_db()
1022         for i,j in list.items():
1023             retl=(i,chtxt,"0",str(Decimal(j)/beforenumf),beforenum,str(j),beforenum)
1024             try:
1025                 db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
1026                 VALUES (%s,%s,%s,%s)\
1027                 ON DUPLICATE KEY UPDATE \
1028                 ratio_all=CONVERT((ratio_all*%s+%s)/%s,DECIMAL(32,14))",retl)
1029             except Exception, inst:
1030                 if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1031                     recdblist.Commonlogex("Error", "change_multi_ratio_all_in_auto_bayes_key (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1032         self.close_db(db)
1033     def change_ratio_rec_reduce_in_auto_bayes_key(self,chtxt,beforenum,newnum):
1034         beforenum=str(beforenum)
1035         newnum=str(newnum)
1036         db = self.connect_db()
1037         db[1].execute("\
1038         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)\
1039         )
1040         self.close_db(db)
1041     def change_ratio_rec_in_auto_bayes_key(self,key,chtxt,beforenum,addnum):
1042         db = self.connect_db()
1043         beforenumf=beforenum
1044         beforenum=str(beforenum)
1045         db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
1046         VALUES (%s,%s,%s,%s)\
1047         ON DUPLICATE KEY UPDATE \
1048         ratio_rec=CONVERT((ratio_rec*%s+%s)/%s,DECIMAL(32,14))",(key,chtxt,str(Decimal(addnum)/beforenumf),"0",beforenum,chtxt,key))
1049         self.close_db(db)
1050     def change_multi_ratio_rec_in_auto_bayes_key(self,chtxt,beforenum,list):#self,key,chtxt,beforenum,addnum):
1051         beforenumf=beforenum
1052         beforenum=str(beforenum)
1053         db = self.connect_db()
1054         for i,j in list.items():
1055             retl=(i,chtxt,str(Decimal(j)/beforenumf),"0",beforenum,str(j),beforenum)
1056             try:
1057                 db[1].execute("INSERT INTO in_auto_bayes_key (keychar,chtxt,ratio_rec,ratio_all) \
1058                 VALUES (%s,%s,%s,%s)\
1059                 ON DUPLICATE KEY UPDATE \
1060                 ratio_rec=CONVERT((ratio_rec*%s+%s)/%s,DECIMAL(32,14))",retl)
1061             except Exception, inst:
1062                 if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1063                     recdblist.Commonlogex("Error", "change_multi_ratio_rec_in_auto_bayes_key (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1064         self.close_db(db)
1065     def select_by_key_in_auto_bayes_key(self,key,chtxt):
1066         db = self.connect_db()
1067         dbexe = db[1].execute("\
1068         SELECT keychar,chtxt,ratio_rec,ratio_all \
1069         FROM in_auto_bayes_key \
1070         WHERE keychar = %s AND chtxt = %s", \
1071                               (key,chtxt))
1072         dls = []
1073         if dbexe > 0:
1074             dls = db[1].fetchall()
1075         self.close_db(db)
1076         if len(dls)>0:
1077             return dls[0]
1078         else:
1079             return dls
1080     def new_auto_timeline_keyword(self):
1081         db = self.connect_db()
1082         try:
1083             db[1].execute('\
1084             CREATE TABLE auto_timeline_keyword \
1085             (\
1086             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
1087             chtxt VARCHAR(40),\
1088             title VARCHAR(100),\
1089             btime DATETIME,\
1090             etime DATETIME,\
1091             UNIQUE uni (chtxt,title,btime,etime)\
1092             )')
1093         except Exception, inst:
1094             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1095                 recdblist.Commonlogex("Error", "new_auto_timeline_keyword (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1096     def add_auto_timeline_keyword(self,chtxt="", title="", btime="", etime=""):
1097         db = self.connect_db()
1098         db[1].execute('\
1099         INSERT IGNORE into auto_timeline_keyword \
1100         (chtxt,title,btime,etime) \
1101         values (%s,%s,%s,%s)', \
1102                        (chtxt, title, btime, etime))
1103         ##db.commit()
1104         self.close_db(db)
1105     def delete_old_auto_timeline_keyword(self, dhour):
1106         db = self.connect_db()
1107         db[1].execute("\
1108         DELETE FROM auto_timeline_keyword \
1109         WHERE \
1110         btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
1111         self.close_db(db)
1112     def new_auto_timeline_bayes(self):
1113         db = self.connect_db()
1114         try:
1115             db[1].execute('\
1116             CREATE TABLE auto_timeline_bayes \
1117             (\
1118             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
1119             chtxt VARCHAR(40),\
1120             title VARCHAR(100),\
1121             btime DATETIME,\
1122             etime DATETIME,\
1123             point INT,\
1124             UNIQUE uni (chtxt,title,btime,etime)\
1125             )')
1126         except Exception, inst:
1127             if not ((type(inst)==MySQLdb.ProgrammingError and inst[0]==1007)or(type(inst)==MySQLdb.OperationalError and inst[0]==1050)):
1128                 recdblist.Commonlogex("Error", "new_auto_timeline_bayes (dbMySQL.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
1129     def add_auto_timeline_bayes(self,chtxt="", title="", btime="", etime="",point=""):
1130         db = self.connect_db()
1131         db[1].execute('\
1132         INSERT IGNORE into auto_timeline_bayes \
1133         (chtxt,title,btime,etime,point) \
1134         values (%s,%s,%s,%s,%s)', \
1135                       (chtxt, title, btime, etime,point))
1136         self.close_db(db)
1137     def delete_old_auto_timeline_bayes(self, dhour):
1138         db = self.connect_db()
1139         db[1].execute("\
1140         DELETE FROM auto_timeline_bayes \
1141         WHERE \
1142         btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
1143         self.close_db(db)
1144     def update_db_to93(self):
1145         db = self.connect_db()
1146         self.drop_in_settings()
1147         self.new_in_settings()
1148         db[1].execute("\
1149         ALTER TABLE timeline ADD epgtitle VARCHAR(100),\
1150         ADD epgbtime DATETIME,\
1151         ADD epgetime DATETIME,\
1152         ADD epgduplicate TINYINT DEFAULT 0,\
1153         ADD epgchange TINYINT DEFAULT 0")
1154         db[1].execute("\
1155         ALTER TABLE in_status ADD version TINYINT")
1156         self.close_db(db)
1157         self.change_version_in_status("93")
1158     def update_db_93to94(self):
1159         db = self.connect_db()
1160         self.drop_in_settings()
1161         self.new_in_settings()
1162         db[1].execute("\
1163         ALTER TABLE timeline ADD counter TINYINT DEFAULT -1")
1164         self.close_db(db)
1165         self.change_version_in_status("94")
1166     def update_db_94to95(self):
1167         db = self.connect_db()
1168         self.drop_in_settings()
1169         self.new_in_settings()
1170         db[1].execute("\
1171         ALTER TABLE timeline ADD epgexp VARCHAR(200)")
1172         self.close_db(db)
1173         self.change_version_in_status("95")
1174     def update_db_95to96(self):
1175         db = self.connect_db()
1176         self.drop_in_settings()
1177         self.new_in_settings()
1178         self.close_db(db)
1179         self.change_version_in_status("96")
1180     def update_db_96to98(self):
1181         db = self.connect_db()
1182         self.drop_in_settings()
1183         self.new_in_settings()
1184         self.close_db(db)
1185         self.change_version_in_status("98")
1186     def update_db_98to100(self):
1187         ###ここで前のepg_chをバックアップしてchtxtの変換をする必要がある。
1188         db = self.connect_db()
1189         self.drop_in_settings()
1190         self.new_in_settings()
1191         db[1].execute("\
1192         UPDATE timeline INNER JOIN epg_ch ON timeline.chtxt=epg_ch.chtxt SET timeline.chtxt=CONCAT(CONCAT(epg_ch.ch,'_'),epg_ch.csch)")
1193         db[1].execute("\
1194         ALTER TABLE epg_ch DROP ontv")
1195         db[1].execute("\
1196         ALTER TABLE in_auto_jbk_key ADD auto TINYINT DEFAULT 0")
1197         db[1].execute("\
1198         ALTER TABLE in_auto_jbk_key ADD opt VARCHAR(20) DEFAULT \"\"")
1199         self.close_db(db)
1200         self.change_installed_in_status()#チャンネルスキャンをさせる
1201         self.change_version_in_status("100")