3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
14 def __init__(self, dbname, user, passwd, host="localhost", port=3306):
18 self.dbpasswd = passwd
21 con = MySQLdb.connect(user=user, passwd=passwd)
23 cur.execute('CREATE DATABASE ' + dbname + " DEFAULT CHARACTER SET utf8")
28 db = self.connect_db()
31 CREATE TABLE rectime \
33 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
39 deltatime VARCHAR(5),\
42 UNIQUE uni (type,chtxt,title,btime,deltaday)\
53 con = MySQLdb.connect(db=self.dbname, host=self.dbhost, port=self.dbport, user=self.dbusr, passwd=self.dbpasswd, charset="utf8")
55 cur.execute('set names utf8;')
57 def close_db(self, db):
61 db = self.connect_db()
63 db[1].execute('drop table chdata')
71 ontv VARCHAR(30) PRIMARY KEY,\
81 def add_chdata(self, bctype, ontv, chtxt, ch, csch, updatetime):
82 db = self.connect_db()
85 VALUES (%s,%s,%s,%s,%s,%s,%s)', \
86 (bctype, ontv, chtxt, ch, csch, updatetime, "1"))
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 \
98 dls = db[1].fetchall()
102 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
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 \
115 dls = db[1].fetchall()
119 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
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 \
127 WHERE bctype = %s", \
132 dls = db[1].fetchall()
137 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
141 def select_by_ch_chdata(self, ch):
142 db = self.connect_db()
143 dbexe = db[1].execute("\
145 bctype,ontv,chtxt,ch,csch,updatetime \
152 dls = db[1].fetchall()
156 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
159 def select_all_chdata(self):
160 db = self.connect_db()
161 dbexe = db[1].execute("\
162 SELECT bctype,ontv,chtxt,ch,csch,updatetime \
168 dls = db[1].fetchall()
172 r[5] = r[5].strftime("%Y-%m-%d %H:%M:%S")
175 def select_get_update_chdata(self, dhour):
176 db = self.connect_db()
177 dbexe = db[1].execute("SELECT bctype,chtxt,status FROM chdata \
180 updatetime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR) \
186 ORDER BY status DESC")
190 ret = db[1].fetchall()
193 def update_by_bctype_chdata(self, bctype):
194 db = self.connect_db()
200 WHERE bctype = %s", (bctype,))
202 def update_by_bctype_and_chtxt_chdata(self, bctype, chtxt):
203 db = self.connect_db()
209 WHERE bctype = %s AND chtxt = %s", (bctype, chtxt))
211 def update_status_by_bctype_chdata(self, bctype, status):
212 db = self.connect_db()
217 WHERE bctype = %s", \
221 def add_rectime(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
222 db = self.connect_db()
224 INSERT IGNORE into rectime \
225 (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
226 values (%s,%s,%s,%s,%s,%s,%s,%s)', \
227 (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
230 def del_rectime(self, type="", title="", chtxt="", btime=""):
234 db = self.connect_db()
236 DELETE FROM rectime \
237 WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
238 (type, title, chtxt, btime))
241 def select_all_rectime(self):
242 db = self.connect_db()
244 dbr = db[1].execute("\
245 SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
247 dbl = db[1].fetchall()
250 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
254 ret['title'] = title.encode('utf-8')
255 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
256 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
260 ret['deltatime'] = ""
262 if deltatime == None:
267 ret['deltatime'] = deltatime
268 elif typet == 'keyevery':
269 ret['deltatime'] = deltatime
270 ret['deltaday'] = deltaday
274 def select_bytime_rectime(self, dminutes):
275 db = self.connect_db()
277 #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 )")
278 dbr = db[1].execute("SELECT \
279 type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
281 WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
282 DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
283 dbl = db[1].fetchall()
287 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
291 ret['title'] = title.encode('utf-8')
292 btime = btime.strftime("%Y-%m-%d %H:%M:%S")
293 etime = etime.strftime("%Y-%m-%d %H:%M:%S")
297 if deltatime == None or deltatime == "":
299 if deltaday == None or deltaday == "":
302 ret['deltatime'] = deltatime
303 elif typet == 'keyevery':
304 ret['deltatime'] = deltatime
305 ret['deltaday'] = deltaday
308 def delete_old_rectime(self, dhour):
309 db = self.connect_db()
311 DELETE FROM rectime \
313 btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
316 def new_status(self):
317 db = self.connect_db()
320 CREATE TABLE status \
322 ts2avi TINYINT DEFAULT 0,\
323 terec TINYINT DEFAULT 0,\
324 bscsrec TINYINT DEFAULT 0,\
325 b252ts TINYINT DEFAULT 0\
327 db[1].execute("INSERT IGNORE into status VALUE (0,0,0,0)")
332 def select_all_status(self):
333 db = self.connect_db()
334 dbexe = db[1].execute("\
335 SELECT ts2avi,terec,bscsrec,b252ts \
341 dls = db[1].fetchall()
352 def change_ts2avi_status(self,i):
357 db = self.connect_db()
359 UPDATE status SET ts2avi=ts2avi+%s",i)
361 def change_terec_status(self,i):
366 db = self.connect_db()
368 UPDATE status SET terec=terec+%s",i)
370 def change_bscsrec_status(self,i):
375 db = self.connect_db()
377 UPDATE status SET bscsrec=bscsrec+%s",i)
379 def change_b252ts_status(self,i):
384 db = self.connect_db()
386 UPDATE status SET b252ts=b252ts+%s",i)
388 def new_tv(self, bctype):
389 db = self.connect_db()
393 WHERE bctype = %s", \
402 channel VARCHAR(100) NOT NULL,\
407 UNIQUE unitv(bctype,channel,start,stop,title)\
413 def add_tv(self, bctype, channel, start, stop, title, desc):
414 db = self.connect_db()
416 INSERT IGNORE INTO tv \
417 VALUES (%s,%s,%s,%s,%s,%s)', \
418 (bctype, channel, start, stop, title, desc))
421 def add_multi_tv(self, tvlists):
423 tvlists is (bctype,channel,start,stop,title,desc) lists.
425 db = self.connect_db()
427 INSERT IGNORE INTO tv \
428 (bctype,channel,start,stop,title,exp) \
429 values(%s,%s,%s,%s,%s,%s)', \
435 def select_by_time_ngram_tv(self, btime, etime, chtxt):
436 db = self.connect_db()
439 chdata.chtxt,title,start,stop \
442 WHERE chdata.ontv=tv.channel \
449 dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
452 retall = db[1].fetchall()
455 def new_ch(self, bctype):
456 db = self.connect_db()
458 db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
466 channel VARCHAR(20) NOT NULL,\
467 display VARCHAR(100),\
468 UNIQUE unich(bctype,channel)\
474 def add_ch(self, bctype, channel, display):
475 db = self.connect_db()
476 db[1].execute('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', (bctype, channel, display))
479 def add_multi_ch(self, chlists):
481 chlists is (bctype,channel,display) lists
483 db = self.connect_db()
484 db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)
486 def import_from_SQLite(self, dbpath):
487 dbb = dbSQLite.DB_SQLite(dbpath)
488 dbl = dbb.select_all_rectime()
490 self.add_rectime(db['type'], db['chtxt'], db['title'], db['btime'], db['etime'], db['deltatime'], db['deltaday'], db['opt'])