3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2011 Yukikaze
14 import simplejson as json
20 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
21 confp = ConfigParser.SafeConfigParser()
24 if (os.path.exists(os.path.join(path,Conf))):
25 cpath=os.path.join(path,Conf)
26 elif (os.path.exists(os.path.join("/etc/rec10",Conf))):
27 cpath=os.path.join("/etc/rec10",Conf)
28 elif (os.path.exists(os.path.join("/etc",Conf))):
29 cpath=os.path.join("/etc",Conf)
33 return confp.get('path', string)
34 def getdbpath(string):
36 return confp.get('db', string)
39 if getdbpath('db') == 'MySQL':
40 dbn = getdbpath("mysql_dbname")
41 dbh = getdbpath("mysql_host")
42 dbu = getdbpath("mysql_user")
43 dbpwd = getdbpath("mysql_passwd")
44 dbport = int(getdbpath("mysql_port"))
45 #print [dbn,dbh,dbu,dbpwd,dbport]
46 retdb = DB_MySQL(dbname=dbn, host=dbh, user=dbu, passwd=dbpwd, port=dbport)
54 def __init__(self, dbname, user, passwd, host="localhost", port=3306):
58 self.dbpasswd = passwd
64 con = MySQLdb.connect(db=self.dbname, host=self.dbhost, port=self.dbport,\
65 user=self.dbusr, passwd=self.dbpasswd, charset="utf8")
67 cur.execute('set names utf8;')
69 def close_db(self, db):
74 def timeline2dic(self,timeline):
77 ret['type']=timeline[1]
78 ret['chtxt']=timeline[2]
79 ret['title']=timeline[3]
80 ret['btime']=timeline[4].strftime("%m/%d %H:%M")
81 ret['etime']=timeline[5].strftime("%H:%M")
85 ret['deltatime']=timeline[6]
89 ret['deltaday']=timeline[7]
92 ret['opt']=timeline[8]
93 ret['epgtitle']=timeline[9]
94 ret['epgbtime']=timeline[10]
95 ret['epgetime']=timeline[11]
96 ret['epgduplicate']=timeline[12]
97 ret['epgchange']=timeline[13]
98 ret['epgexp']=timeline[14]
99 ret['counter']=timeline[15]
101 def select_all_timeline(self):
102 db = self.connect_db()
104 dl = db[1].execute("SELECT id,type,chtxt,title,btime,etime,deltatime,deltaday,opt,\
105 epgtitle,epgbtime,epgetime,epgduplicate,epgchange,epgexp,counter FROM timeline ORDER BY btime")
106 dls = db[1].fetchall()
108 recdata.append(self.timeline2dic(line))
111 def select_by_chtxt_timeline(self, chtxt):
112 db = self.connect_db()
114 dl = db[1].execute("SELECT id,type,chtxt,title,btime,etime,deltatime,deltaday,opt,\
115 epgtitle,epgbtime,epgetime,epgduplicate,epgchange,epgexp,counter FROM timeline WHERE chtxt = %s ", (chtxt, ))
118 recdata.append(self.timeline2dic(line))
121 def select_by_id_timeline(self, idt):
122 db = self.connect_db()
124 dl = db[1].execute("SELECT id,type,chtxt,title,btime,etime,deltatime,deltaday,opt,\
125 epgtitle,epgbtime,epgetime,epgduplicate,epgchange,epgexp,counter FROM timeline WHERE id = %s ", (idt, ))
128 recdata.append(self.timeline2dic(line))
131 def select_by_btime_etime_chtxt_timeline(self,btime,etime,chtxt):
132 db = self.connect_db()
134 dl = db[1].execute("SELECT id,type,chtxt,title,btime,etime,deltatime,deltaday,opt,\
135 epgtitle,epgbtime,epgetime,epgduplicate,epgchange,epgexp,counter FROM timeline \
136 WHERE btime >= %s AND etime <= %s AND chtxt = %s", (btime,etime,chtxt ))
137 dls = db[1].fetchall()
139 recdata.append(self.timeline2dic(line))
142 def add_timeline(self, type="", chtxt="", title="", btime="", etime="", deltatime="", deltaday="", opt=""):
143 db = self.connect_db()
144 db[1].execute('INSERT IGNORE INTO timeline (type,chtxt,title,btime,etime,deltatime,deltaday,opt) \
145 values (%s,%s,%s,%s,%s,%s,%s,%s)', (type, chtxt, title, btime, etime, deltatime, deltaday, opt))
150 def epg_ch2dic(self,epg_ch):
152 ret['bctype']=epg_ch[0]
153 ret['chtxt']=epg_ch[1]
155 ret['csch']=epg_ch[3]
156 ret['chname']=epg_ch[4]
157 ret['updatetime']=epg_ch[5]
158 ret['status']=epg_ch[6]
159 ret['visible']=epg_ch[7]
160 if ret["chtxt"].split("_")[0]=="BS":
162 elif ret["chtxt"].split("_")[0]=="CS":
167 def select_all_epg_ch(self):
168 db = self.connect_db()
169 r1 = db[1].execute("SELECT bctype,chtxt,ch,csch,chname,updatetime,status,visible FROM epg_ch")
170 lines = db[1].fetchall()
173 ret.append(self.epg_ch2dic(ls))
176 def select_by_chtxt_epg_ch(self, chtxt):
177 db = self.connect_db()
178 r1 = db[1].execute("SELECT bctype,chtxt,ch,csch,chname,updatetime,status,visible FROM epg_ch WHERE chtxt=%s", (chtxt, ))
179 lines = db[1].fetchall()
182 ret.append(epg_ch2dic(ls))
186 ########epg_timeline系
187 def epg_timeline2dic(self,epg_timeline):
189 ret['bctype']=epg_timeline[0]
190 ret['channel']=epg_timeline[1]
191 ret['chtxt']=epg_timeline[1]
192 ret['start']=datetime.datetime(*time.strptime(epg_timeline[2], "%Y%m%d%H%M%S")[:-3])
193 ret['stop']=datetime.datetime(*time.strptime(epg_timeline[3], "%Y%m%d%H%M%S")[:-3])
194 ret['bt']=ret['start']
195 ret['et']=ret['stop']
196 ret['btime']=ret['bt'].strftime("%Y-%m-%d %H:%M:%S")
197 ret['etime']=ret['et'].strftime("%Y-%m-%d %H:%M:%S")
198 ret['title']=epg_timeline[4]
199 ret['exp']=epg_timeline[5]
200 ret['longexp']=epg_timeline[6]
201 ret['category']=epg_timeline[7]
203 def select_all_epg_timeline(self):
204 db = self.connect_db()
205 r1 = db[1].execute("SELECT bctype,channel,start,stop,title,exp,longexp,category FROM epg_timeline")
206 lines = db[1].fetchall()
209 ret.append(self.epg_timeline2dic(ls))
212 def select_by_chtxt_epg_timeline(self, chtxt):
213 db = self.connect_db()
214 r1 = db[1].execute("SELECT bctype,channel,start,stop,title,exp,longexp,category FROM epg_timeline WHERE channel=%s", (chtxt, ))
215 lines = db[1].fetchall()
218 ret.append(self.epg_timeline2dic(ls))
221 def select_by_btime_etime_chtxt_epg_timeline(self,btime,etime,chtxt):
222 db = self.connect_db()
223 start=datetime.datetime(*time.strptime(btime, "%Y-%m-%d %H:%M:%S")[:-3]).strftime("%Y%m%d%H%M%S")
224 stop=datetime.datetime(*time.strptime(etime, "%Y-%m-%d %H:%M:%S")[:-3]).strftime("%Y%m%d%H%M%S")
226 dl = db[1].execute("SELECT bctype,channel,start,stop,title,exp,longexp,category FROM epg_timeline \
227 WHERE start >= %s AND stop <= %s AND channel = %s", (start,stop,chtxt ))
228 dls = db[1].fetchall()
230 recdata.append(self.epg_timeline2dic(line))
233 def select_by_keyword_title_epg_timeline(self,keyword="",chtxt="",btime="",etime=""):
239 keyt=" AND title LIKE '%"+keyt+"%' "
241 start=datetime.datetime(*time.strptime(btime, "%Y-%m-%d %H:%M:%S")[:-3]).strftime("%Y%m%d%H%M%S")
242 stop=datetime.datetime(*time.strptime(etime, "%Y-%m-%d %H:%M:%S")[:-3]).strftime("%Y%m%d%H%M%S")
243 btimet=" AND start >= "+start+" "
244 etimet=" AND stop <= "+stop+" "
246 chtxtt=" AND channel = "+chtxt+" "
247 db = self.connect_db()
249 #print "SELECT bctype,channel,start,stop,title,exp,longexp,category FROM epg_timeline \
250 #WHERE bctype LIKE '%' "+keyt+btimet+etimet+chtxtt+""
251 dl = db[1].execute("SELECT bctype,channel,start,stop,title,exp,longexp,category FROM epg_timeline \
252 WHERE bctype LIKE '%' "+keyt+btimet+etimet+chtxtt+"")
253 dls = db[1].fetchall()
255 recdata.append(self.epg_timeline2dic(line))
258 #####ここまでDBの読み込み/書き込み系
261 jsonHeader="Content-Type: application/json;charset=utf-8\n"
262 htmlHeader="Content-Type: text/html; charset=utf-8\n"
264 f = cgi.FieldStorage()
270 if f.getfirst('mode',""):
271 mode=f.getfirst('mode', "")
272 if mode=="chtypelist":
273 #print "Content-Type: application/json;charset=utf-8\n"
275 typest={"identifier":"id","items":[
277 "id":"search_everyday",
283 "id":"reserve_flexible",
286 "id":"reserve_fixed",
289 "id":"convert_b25_ts",
292 "id":"convert_ts_mp4",
296 print json.dumps(typest)
298 chls=db.select_all_epg_ch()
299 chll={"identifier":"id","items":[]}
301 chll["items"].append({"id":chl["chtxt"],"type":chl["type"],"label":chl["chname"]})
303 print json.dumps(chll)
304 if mode=="timeline":####録画一覧用
305 tlll={"identifier":"id","items":[]}
307 if f.getfirst("chtxt"):
308 dbl=db.select_by_btime_etime_chtxt_timeline(btime,etime,chtxt)
310 dbl=db.select_all_timeline()
313 tlll["items"].append({"id":dbi["id"],"btime":dbi["btime"],"etime":dbi["etime"],
314 "chtxt":dbi["chtxt"],"title":dbi["title"],"type":dbi["type"],
315 "deltatime":dbi["deltatime"],"deltaday":dbi["deltaday"],
319 print json.dumps(tlll,encoding="utf-8")
320 if mode=="epg-timeline":
322 btime=f.getfirst("btime")
323 etime=f.getfirst("etime")
325 btime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
327 et=datetime.datetime.now()+datetime.timedelta(days=8)
328 etime=et.strftime("%Y-%m-%d %H:%M:%S")
329 chtxt=f.getfirst("chtxt")
333 dbl=db.select_by_btime_etime_chtxt_epg_timeline(btime,etime,chtxt)
335 tlll["items"].append({"id":dbi["btime"],"btime":dbi["btime"],
336 "chtxt":dbi["chtxt"],"title":dbi["title"],
337 "exp":dbi["exp"],"longexp":dbi["longexp"],
338 "category":dbi["category"]})
341 print json.dumps({"data":tlll},encoding="utf-8")
342 if mode=="epg-search":
344 btime=f.getfirst("btime")
345 etime=f.getfirst("etime")
347 bt=datetime.datetime.now()
348 btime=bt.strftime("%Y-%m-%d %H:%M:%S")
350 et=datetime.datetime.now()+datetime.timedelta(days=8)
351 etime=et.strftime("%Y-%m-%d %H:%M:%S")
352 chtxt=f.getfirst("chtxt")
355 keyword=f.getfirst("keyword")
358 dbl=db.select_by_keyword_title_epg_timeline(keyword=keyword,chtxt=chtxt,btime=btime,etime=etime)
360 tlll["items"].append({"id":dbi["btime"],"btime":dbi["btime"],
361 "chtxt":dbi["chtxt"],"title":dbi["title"],
362 "exp":dbi["exp"],"longexp":dbi["longexp"],
363 "category":dbi["category"]})
366 print json.dumps({"data":tlll},encoding="utf-8")