3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
13 def __init__(self, dbname, user, passwd, host="localhost", port=3306):
17 self.dbpasswd = passwd
20 con = MySQLdb.connect(user=user, passwd=passwd)
22 cur.execute('CREATE DATABASE ' + dbname + " DEFAULT CHARACTER SET utf8")
27 db = self.connect_db()
30 CREATE TABLE rectime \
32 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
38 deltatime VARCHAR(5),\
41 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_reclog(self , chtxt="", title="", btime="", etime="", opt="",category=""):
222 db = self.connect_db()
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))
230 def del_reclog(self, title="", chtxt="", btime=""):
234 db = self.connect_db()
237 WHERE title = %s AND chtxt = %s AND btime = %s", \
238 (title, chtxt, btime))
241 def new_reclog(self):
242 db = self.connect_db()
245 CREATE TABLE reclog \
247 id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,\
253 category VARCHAR(100),\
254 UNIQUE uni (chtxt,title,btime,category)\
259 def add_rectime(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
260 db = self.connect_db()
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))
268 def del_rectime(self, type="", title="", chtxt="", btime=""):
272 db = self.connect_db()
274 DELETE FROM rectime \
275 WHERE type = %s AND title = %s AND chtxt = %s AND btime = %s", \
276 (type, title, chtxt, btime))
279 def select_all_rectime(self):
280 db = self.connect_db()
282 dbr = db[1].execute("\
283 SELECT type, chtxt, title, btime, etime, deltatime ,deltaday ,opt \
285 dbl = db[1].fetchall()
288 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
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")
298 ret['deltatime'] = ""
300 if deltatime == None:
305 ret['deltatime'] = deltatime
306 elif typet == 'keyevery':
307 ret['deltatime'] = deltatime
308 ret['deltaday'] = deltaday
312 def select_bytime_rectime(self, dminutes):
313 db = self.connect_db()
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 \
319 WHERE btime BETWEEN DATE_SUB(now(),INTERVAL " + dminutes + " MINUTE ) AND \
320 DATE_ADD(now(),INTERVAL " + dminutes + " MINUTE )")
321 dbl = db[1].fetchall()
325 for typet, chtxt, title, btime, etime, deltatime, deltaday, opt in dbl:
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")
335 if deltatime == None or deltatime == "":
337 if deltaday == None or deltaday == "":
340 ret['deltatime'] = deltatime
341 elif typet == 'keyevery':
342 ret['deltatime'] = deltatime
343 ret['deltaday'] = deltaday
346 def delete_old_rectime(self, dhour):
347 db = self.connect_db()
349 DELETE FROM rectime \
351 btime < DATE_SUB(now(),INTERVAL " + dhour + " HOUR )")
354 def new_status(self):
355 db = self.connect_db()
358 CREATE TABLE status \
360 ts2avi TINYINT DEFAULT 0,\
361 terec TINYINT DEFAULT 0,\
362 bscsrec TINYINT DEFAULT 0,\
363 b252ts TINYINT DEFAULT 0\
365 db[1].execute("INSERT IGNORE into status VALUE (0,0,0,0)")
370 def select_all_status(self):
371 db = self.connect_db()
372 dbexe = db[1].execute("\
373 SELECT ts2avi,terec,bscsrec,b252ts \
379 dls = db[1].fetchall()
390 def change_ts2avi_status(self,i):
395 db = self.connect_db()
397 UPDATE status SET ts2avi=ts2avi+%s",i)
399 def change_terec_status(self,i):
404 db = self.connect_db()
406 UPDATE status SET terec=terec+%s",i)
408 def change_bscsrec_status(self,i):
413 db = self.connect_db()
415 UPDATE status SET bscsrec=bscsrec+%s",i)
417 def change_b252ts_status(self,i):
422 db = self.connect_db()
424 UPDATE status SET b252ts=b252ts+%s",i)
426 def new_tv(self, bctype):
427 db = self.connect_db()
431 WHERE bctype = %s", \
440 channel VARCHAR(100) NOT NULL,\
445 category VARCHAR(100),\
446 UNIQUE unitv(bctype,channel,start,stop,title)\
452 def add_tv(self, bctype, channel, start, stop, title, desc, category):
453 db = self.connect_db()
455 INSERT IGNORE INTO tv \
456 VALUES (%s,%s,%s,%s,%s,%s,%s)', \
457 (bctype, channel, start, stop, title, desc, category))
460 def add_multi_tv(self, tvlists):
462 tvlists is (bctype,channel,start,stop,title,desc,category) lists.
464 db = self.connect_db()
466 INSERT IGNORE INTO tv \
467 (bctype,channel,start,stop,title,exp,category) \
468 values(%s,%s,%s,%s,%s,%s,%s)', \
474 def select_by_time_ngram_tv(self, btime, etime, chtxt):
475 db = self.connect_db()
478 chdata.chtxt,title,start,stop,category \
481 WHERE chdata.ontv=tv.channel \
488 dbcmd = db[1].execute(dbexe, (btime, etime, chtxt))
491 retall = db[1].fetchall()
494 def new_ch(self, bctype):
495 db = self.connect_db()
497 db[1].execute("DELETE FROM ch WHERE bctype = %s", (bctype,))
505 channel VARCHAR(20) NOT NULL,\
506 display VARCHAR(100),\
507 UNIQUE unich(bctype,channel)\
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))
518 def add_multi_ch(self, chlists):
520 chlists is (bctype,channel,display) lists
522 db = self.connect_db()
523 db[1].executemany('INSERT IGNORE INTO ch VALUES (%s,%s,%s)', chlists)