OSDN Git Service

add reclog table.
[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 Yukikaze
5 import MySQLdb
6
7 class DB_MySQL:
8     dbname = ""
9     dbhost = ""
10     dbusr = ""
11     dbpasswd = ""
12     dbport = 0
13     def __init__(self, dbname, user, passwd, host="localhost", port=3306):
14         self.dbname = dbname
15         self.dbhost = host
16         self.dbusr = user
17         self.dbpasswd = passwd
18         self.dbport = port
19         try:
20             con = MySQLdb.connect(user=user, passwd=passwd)
21             cur = con.cursor()
22             cur.execute('CREATE DATABASE ' + dbname + " DEFAULT CHARACTER SET utf8")
23             cur.close()
24             con.close()
25         except:
26             ""
27         db = self.connect_db()
28         try:
29             db[1].execute('\
30             CREATE TABLE rectime \
31             (\
32             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
33             type VARCHAR(20),\
34             chtxt VARCHAR(20),\
35             title VARCHAR(100),\
36             btime DATETIME,\
37             etime DATETIME,\
38             deltatime VARCHAR(5),\
39             deltaday VARCHAR(5),\
40             opt VARCHAR(20),\
41             UNIQUE uni (type,chtxt,title,btime,deltaday)\
42             )')
43         except:
44             ""
45         self.close_db(db)
46         self.new_tv("")
47         self.new_ch("")
48         self.new_reclog()
49     def connect_db(self):
50         """
51         dbへの接続
52         """
53         con = MySQLdb.connect(db=self.dbname, host=self.dbhost, port=self.dbport, user=self.dbusr, passwd=self.dbpasswd, charset="utf8")
54         cur = con.cursor()
55         cur.execute('set names utf8;')
56         return [con, cur]
57     def close_db(self, db):
58         db[1].close()
59         db[0].close()
60     def new_chdata(self):
61         db = self.connect_db()
62         try:
63             db[1].execute('drop table chdata')
64         except:
65             ""
66         try:
67             db[1].execute('\
68             CREATE TABLE chdata \
69             (\
70             bctype VARCHAR(15),\
71             ontv VARCHAR(30) PRIMARY KEY,\
72             chtxt VARCHAR(15),\
73             ch VARCHAR(20),\
74             csch VARCHAR(20),\
75             updatetime DATETIME,\
76             status TINYINT\
77             )')
78         except:
79             ""
80         self.close_db(db)
81     def add_chdata(self, bctype, ontv, chtxt, ch, csch, updatetime):
82         db = self.connect_db()
83         db[1].execute('\
84         INSERT INTO chdata \
85         VALUES (%s,%s,%s,%s,%s,%s,%s)', \
86                       (bctype, ontv, chtxt, ch, csch, updatetime, "1"))
87         self.close_db(db)
88     def select_by_ontv_chdata(self, ontv):
89         db = self.connect_db()
90         dbexe = db[1].execute("\
91         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
92         FROM chdata \
93         WHERE ontv = %s", \
94                               (ontv,))
95         ret = []
96         dls = []
97         if dbexe > 0:
98             dls = db[1].fetchall()
99         self.close_db(db)
100         for dl in dls:
101             r = list(dl)
102             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
103             ret.append(r)
104         return ret
105     def select_by_chtxt_chdata(self, chtxt):
106         db = self.connect_db()
107         dbexe = db[1].execute("\
108         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
109         FROM chdata \
110         WHERE chtxt = %s", \
111                               (chtxt,))
112         ret = []
113         dls = []
114         if dbexe > 0:
115             dls = db[1].fetchall()
116         self.close_db(db)
117         for dl in dls:
118             r = list(dl)
119             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
120             ret.append(r)
121         return ret
122     def select_by_bctype_chdata(self, bctype):
123         db = self.connect_db()
124         dbexe = db[1].execute("\
125         SELECT bctype,ontv,chtxt,ch,csch,updatetime,status \
126         FROM chdata \
127         WHERE bctype = %s", \
128                               (bctype,))
129         ret = []
130         dls = []
131         if dbexe > 0:
132             dls = db[1].fetchall()
133         self.close_db(db)
134         for dl in dls:
135             #print dl
136             r = list(dl)
137             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
138             r[6] = str(r[6])
139             ret.append(r)
140         return ret
141     def select_by_ch_chdata(self, ch):
142         db = self.connect_db()
143         dbexe = db[1].execute("\
144         SELECT \
145         bctype,ontv,chtxt,ch,csch,updatetime \
146         FROM chdata \
147         WHERE ch = %s", \
148                               (ch,))
149         ret = []
150         dls = []
151         if dbexe > 0:
152             dls = db[1].fetchall()
153         self.close_db(db)
154         for dl in dls:
155             r = list(dl)
156             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
157             ret.append(r)
158         return ret
159     def select_all_chdata(self):
160         db = self.connect_db()
161         dbexe = db[1].execute("\
162         SELECT bctype,ontv,chtxt,ch,csch,updatetime \
163         FROM chdata \
164         ")
165         ret = []
166         dls = []
167         if dbexe > 0:
168             dls = db[1].fetchall()
169         self.close_db(db)
170         for dl in dls:
171             r = list(dl)
172             r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
173             ret.append(r)
174         return ret
175     def select_get_update_chdata(self, dhour):
176         db = self.connect_db()
177         dbexe = db[1].execute("SELECT bctype,chtxt,status FROM chdata \
178         WHERE \
179         ( \
180         updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
181         AND \
182         status = 1 \
183         ) \
184         OR \
185         status > 1 \
186         ORDER BY status DESC")
187         ret = []
188         #print dbexe
189         if dbexe > 0:
190             ret = db[1].fetchall()
191         self.close_db(db)
192         return ret
193     def update_by_bctype_chdata(self, bctype):
194         db = self.connect_db()
195         db[1].execute("\
196         UPDATE chdata \
197         SET \
198         updatetime=now() , \
199         status = 1 \
200         WHERE bctype = %s", (bctype,))
201         self.close_db(db)
202     def update_by_bctype_and_chtxt_chdata(self, bctype, chtxt):
203         db = self.connect_db()
204         db[1].execute("\
205         UPDATE chdata \
206         SET \
207         updatetime=now() , \
208         status = 1\
209         WHERE bctype = %s AND chtxt = %s", (bctype, chtxt))
210         self.close_db(db)
211     def update_status_by_bctype_chdata(self, bctype, status):
212         db = self.connect_db()
213         db[1].execute("\
214         UPDATE chdata \
215         SET \
216         status=%s \
217         WHERE bctype = %s", \
218                       (status, bctype)\
219                       )
220         self.close_db(db)
221     def add_reclog(self , chtxt="", title="", btime="", etime="", opt="",category=""):
222         db = self.connect_db()
223         db[1].execute('\
224         INSERT IGNORE into reclog \
225         (chtxt,title,btime,etime,opt,category) \
226         values (%s,%s,%s,%s,%s,%s)', \
227                       ( chtxt, title, btime, etime, opt,category))
228         ##db.commit()
229         self.close_db(db)
230     def del_reclog(self, title="", chtxt="", btime=""):
231         """
232
233         """
234         db = self.connect_db()
235         db[1].execute("\
236         DELETE FROM reclog \
237         WHERE title = %s AND chtxt = %s AND btime = %s", \
238                       (title, chtxt, btime))
239         #db.commit()
240         self.close_db(db)
241     def new_reclog(self):
242         db = self.connect_db()
243         try:
244             db[1].execute('\
245             CREATE TABLE reclog \
246             (\
247             id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
248             chtxt VARCHAR(20),\
249             title VARCHAR(100),\
250             btime DATETIME,\
251             etime DATETIME,\
252             opt VARCHAR(20),\
253             category VARCHAR(100),\
254             UNIQUE uni (chtxt,title,btime,category)\
255             )')
256         except:
257             ""
258         self.close_db(db)
259     def add_rectime(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
260         db = self.connect_db()
261         db[1].execute('\
262         INSERT IGNORE into rectime \
263         (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
264         values (%s,%s,%s,%s,%s,%s,%s,%s)', \
265                       (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
266         ##db.commit()
267         self.close_db(db)
268     def del_rectime(self, type="", title="", chtxt="", btime=""):
269         """
270
271         """
272         db = self.connect_db()
273         db[1].execute("\
274         DELETE FROM rectime \
275         WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
276                       (type, title, chtxt, btime))
277         #db.commit()
278         self.close_db(db)
279     def select_all_rectime(self):
280         db = self.connect_db()
281         recdata = []
282         dbr = db[1].execute("\
283         SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
284         FROM rectime")
285         dbl = db[1].fetchall()
286         self.close_db(db)
287         if dbr > 0:
288             for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
289                 ret = {}
290                 ret['type'] = typet
291                 ret['chtxt'] = chtxt
292                 ret['title'] = title.encode('utf-8')
293                 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
294                 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
295                 ret['btime'] = btime
296                 ret['etime'] = etime
297                 ret['opt'] = opt
298                 ret['deltatime'] = ""
299                 ret['deltaday'] = ""
300                 if deltatime == None:
301                     deltatime = "3"
302                 if deltaday == None:
303                     deltaday = "7"
304                 if typet == 'key':
305                     ret['deltatime'] = deltatime
306                 elif typet == 'keyevery':
307                     ret['deltatime'] = deltatime
308                     ret['deltaday'] = deltaday
309                 recdata.append(ret)
310         self.close_db(db)
311         return recdata
312     def select_bytime_rectime(self, dminutes):
313         db = self.connect_db()
314         recdatum = []
315         #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 )")
316         dbr = db[1].execute("SELECT \
317         type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
318         FROM rectime \
319         WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
320         DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
321         dbl = db[1].fetchall()
322         self.close_db(db)
323         #print dbl
324         if dbr > 0:
325             for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
326                 ret = {}
327                 ret['type'] = typet
328                 ret['chtxt'] = chtxt
329                 ret['title'] = title.encode('utf-8')
330                 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
331                 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
332                 ret['btime'] = btime
333                 ret['etime'] = etime
334                 ret['opt'] = opt
335                 if deltatime == None or deltatime == "":
336                     deltatime = "3"
337                 if deltaday == None or deltaday == "":
338                     deltaday = "7"
339                 if typet == 'key':
340                     ret['deltatime'] = deltatime
341                 elif typet == 'keyevery':
342                     ret['deltatime'] = deltatime
343                     ret['deltaday'] = deltaday
344                 recdatum.append(ret)
345         return recdatum
346     def delete_old_rectime(self, dhour):
347         db = self.connect_db()
348         db[1].execute("\
349         DELETE FROM rectime \
350         WHERE \
351         btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
352         #db.commit()
353         self.close_db(db)
354     def new_status(self):
355         db = self.connect_db()
356         try:
357             db[1].execute("\
358             CREATE TABLE status \
359             (\
360             ts2avi TINYINT DEFAULT 0,\
361             terec TINYINT DEFAULT 0,\
362             bscsrec TINYINT DEFAULT 0,\
363             b252ts TINYINT DEFAULT 0\
364             )")
365             db[1].execute("INSERT IGNORE into status VALUE (0,0,0,0)")
366         except:
367             ""
368
369         self.close_db(db)
370     def select_all_status(self):
371         db = self.connect_db()
372         dbexe = db[1].execute("\
373         SELECT ts2avi,terec,bscsrec,b252ts \
374         FROM status \
375         ")
376         ret = []
377         dls = []
378         if dbexe > 0:
379             dls = db[1].fetchall()
380         self.close_db(db)
381         for dl in dls:
382             r = list(dl)
383             r[0]=str(r[0])
384             r[1]=str(r[1])
385             r[2]=str(r[2])
386             r[3]=str(r[3])
387             ret.append(r)
388         return ret
389         self.close_db(db)
390     def change_ts2avi_status(self,i):
391         """
392         statuをiだけ増減する
393         iはint
394         """
395         db = self.connect_db()
396         db[1].execute("\
397         UPDATE status SET ts2avi=ts2avi+%s",i)
398         self.close_db(db)
399     def change_terec_status(self,i):
400         """
401         statuをiだけ増減する
402         iはint
403         """
404         db = self.connect_db()
405         db[1].execute("\
406         UPDATE status SET terec=terec+%s",i)
407         self.close_db(db)
408     def change_bscsrec_status(self,i):
409         """
410         statuをiだけ増減する
411         iはint
412         """
413         db = self.connect_db()
414         db[1].execute("\
415         UPDATE status SET bscsrec=bscsrec+%s",i)
416         self.close_db(db)
417     def change_b252ts_status(self,i):
418         """
419         statuをiだけ増減する
420         iはint
421         """
422         db = self.connect_db()
423         db[1].execute("\
424         UPDATE status SET b252ts=b252ts+%s",i)
425         self.close_db(db)
426     def new_tv(self, bctype):
427         db = self.connect_db()
428         try:
429             db[1].execute("\
430             DELETE FROM tv \
431             WHERE bctype = %s", \
432                           (bctype,))
433         except:
434             ""
435         try:
436             db[1].execute("\
437             CREATE TABLE tv \
438             (\
439             bctype VARCHAR(20),\
440             channel VARCHAR(100) NOT NULL,\
441             start VARCHAR(30),\
442             stop  VARCHAR(30),\
443             title VARCHAR(100),\
444             exp VARCHAR(200),\
445             category VARCHAR(100),\
446             UNIQUE unitv(bctype,channel,start,stop,title)\
447             )")
448         except:
449             ""
450         #db.commit()
451         self.close_db(db)
452     def add_tv(self, bctype, channel, start, stop, title, desc, category):
453         db = self.connect_db()
454         db[1].execute('\
455         INSERT IGNORE INTO tv \
456         VALUES (%s,%s,%s,%s,%s,%s,%s)', \
457                       (bctype, channel, start, stop, title, desc, category))
458         #db.commit()
459         self.close_db(db)
460     def add_multi_tv(self, tvlists):
461         """
462         tvlists is (bctype,channel,start,stop,title,desc,category) lists.
463         """
464         db = self.connect_db()
465         db[1].executemany('\
466         INSERT IGNORE INTO tv \
467         (bctype,channel,start,stop,title,exp,category) \
468         values(%s,%s,%s,%s,%s,%s,%s)', \
469                           tvlists)
470         #db.commit()
471         self.close_db(db)
472         
473
474     def select_by_time_ngram_tv(self, btime, etime, chtxt):
475         db = self.connect_db()
476         dbexe = "\
477         SELECT \
478         chdata.chtxt,title,start,stop,category \
479         FROM tv \
480         INNER JOIN chdata \
481         WHERE chdata.ontv=tv.channel \
482         AND \
483         start >= %s \
484         AND \
485         start <= %s \
486         AND \
487         chdata.chtxt=%s"
488         dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
489         retall = []
490         if dbcmd > 0:
491             retall = db[1].fetchall()
492         self.close_db(db)
493         return retall
494     def new_ch(self, bctype):
495         db = self.connect_db()
496         try:
497             db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
498         except:
499             ""
500         try:
501             db[1].execute('\
502             CREATE TABLE ch \
503             (\
504             bctype VARCHAR(20),\
505             channel VARCHAR(20) NOT NULL,\
506             display VARCHAR(100),\
507             UNIQUE unich(bctype,channel)\
508             )')
509         except:
510             ""
511         #db.commit()
512         self.close_db(db)
513     def add_ch(self, bctype, channel, display):
514         db = self.connect_db()
515         db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
516         #db.commit()
517         self.close_db(db)
518     def add_multi_ch(self, chlists):
519         """
520         chlists is (bctype,channel,display) lists
521         """
522         db = self.connect_db()
523         db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
524         self.close_db(db)