3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
27 recpath = configreader.getpath('recpath')
28 movepath = configreader.getpath('move_destpath')
29 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
36 if rec10d.rec10db.select_installed_in_status()==0:
40 recdb.delete_old("24")
41 recdb.delete_old_auto_bayes("1")
42 recdb.delete_old_auto_keyword("1")
43 tasks = recdb.getnow("3")
44 inum = recdb.countRecNow_minutes("10")
45 recdblist.printutf8(u"rec10処理開始"+ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
46 recdblist.printutf8(u"直近録画予約件数:" + str(inum) + u"BS/CS録画中件数:" + str(status.getBSCSRecording()) + u"TE録画中件数:" + str(status.getTERecording()) )
49 dnow=datetime.datetime.now()
55 for k in recdb.get_key():
64 auto_process.auto_check(recpath)
66 update = chdb.update()
75 if bctype.find('te') > -1:
76 recnum = status.getTERecording() + recdb.countRecNow_minutes_TE("10")
77 recdblist.printutf8(u"放送種別:"+bctype + u"||該当チューナー実行中件数:" + str(status.getTERecording()) + u":直近予約件数:" + str(recdb.countRecNow_minutes_TE("10")))
78 recnum = int(configreader.getenv("te_max")) -recnum
80 recnum = status.getBSCSRecording() + recdb.countRecNow_minutes_BSCS("10")
81 recdblist.printutf8(u"放送種別:"+bctype + u"||該当チューナー実行中件数:" + str(status.getBSCSRecording()) + u":直近予約件数:" + str(recdb.countRecNow_minutes_BSCS("10")))
82 recnum = int(configreader.getenv("bscs_max")) -recnum
85 rec10d.rec10db.update_status_by_bctype_epg_ch(bctype, "0")
86 epgdb.updatebc(bctype)
87 update = chdb.update()
93 typetxt = task["type"]
103 btime = task['btime']
104 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
107 bt = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
109 etime = task['etime']
110 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
113 et = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
118 tnow = datetime.datetime.now()
120 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
121 if task["type"] == recdblist.REC_RESERVE:#"res,"+chtxt+","+title+","+btime+","+etime+","+opt
123 if pid != 0:#親プロセスの場合
125 else:#子プロセスの場合 アップデートを行って終了
126 type_reserve(task["type"],chtxt,title,bt,et,opt)
128 elif task["type"] == recdblist.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
129 deltatime = task['deltatime']
131 if pid != 0:#親プロセスの場合
133 else:#子プロセスの場合 アップデートを行って終了
134 type_keyword(task["type"],chtxt,title, bt, et, opt, deltatime)
136 elif task["type"] == recdblist.REC_KEYWORD_EVERY_SOME_DAYS:#"keyevery,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt+","+deltaday
137 deltatime = task['deltatime']
138 deltaday = task['deltaday']
140 if pid != 0:#親プロセスの場合
142 else:#子プロセスの場合 アップデートを行って終了
143 type_keyword_every_day(task["type"],chtxt, title, bt, et, opt, deltatime, deltaday)
145 elif task["type"] == recdblist.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
147 if pid != 0:#親プロセスの場合
149 else:#子プロセスの場合 アップデートを行って終了
150 type_final(task["type"],chtxt, title, bt, et, opt)
152 elif task["type"] == recdblist.REC_TS_DECODE_QUE:
155 if pid != 0:#親プロセスの場合
157 else:#子プロセスの場合 アップデートを行って終了
159 type_decode_que(task["type"],chtxt, title, bt, et, opt)
161 elif task["type"] == recdblist.REC_ENCODE_QUE:
162 encodenum=encodenum+1
164 if pid != 0:#親プロセスの場合
166 else:#子プロセスの場合 アップデートを行って終了
167 time.sleep(5*encodenum)
168 type_encode_que(task["type"],chtxt, title, bt, et, opt)
170 elif task["type"] == recdblist.REC_AVI_TO_MKV:
176 recdb.del_reckey(recdblist.REC_AVI_TO_MKV, title, chtxt, btime)
177 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
178 #tv2mp4.avi2mp4(os.path.join(recpath,title+".avi"),os.path.join(recpath,title+".mkv"),opt)
179 tv2mkv.avi2mkv(os.path.join(recpath,title+".avi"),os.path.join(recpath,title+".mkv"),opt)
180 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
182 elif task["type"] == recdblist.REC_AVI_TO_MP4:
188 recdb.del_reckey(recdblist.REC_AVI_TO_MP4, title, chtxt, btime)
189 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
190 tv2mp4.avi2mp4(os.path.join(recpath,title+".avi"),os.path.join(recpath,title+".mp4"),opt)
191 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
193 elif task["type"] == recdblist.REC_MKV_TO_MP4:
199 recdb.del_reckey(recdblist.REC_MKV_TO_MP4, title, chtxt, btime)
200 recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
201 tv2mp4.mkv2mp4(os.path.join(recpath,title+".mkv"),os.path.join(recpath,title+".mp4"))
202 recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
205 def search_keyword(key):
206 tnow = datetime.datetime.now()
207 nows =tnow.strftime("%Y-%m-%d %H:%M:%S")
209 recdatum = epgdb.searchtime_auto(k,nows, "144")
210 for recdata in recdatum:
216 btt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
217 ett = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
218 btimet = btt.strftime("%Y-%m-%d %H:%M:%S")
219 etimet = ett.strftime("%Y-%m-%d %H:%M:%S")
220 if status.getSettings_auto_jbk()==1:
221 topt=status.getSettings_auto_opt()
225 maxnum=epgdb.count_schedule_timeline(btimet, etimet)[1]
226 maxnum=int(configreader.getenv("bscs_max"))-maxnum
228 maxnum=epgdb.count_schedule_timeline(btimet, etimet)[0]
229 maxnum=int(configreader.getenv("te_max"))-maxnum
231 topt=status.getSettings_auto_opt()
232 recdb.recreserv(titlet,chtxtt, btimet, etimet,topt)
233 except Exception, inst:
234 print "error occures in search_keyword_auto_jbk"
235 print str(type(inst))
239 recdb.add_auto_keyword(chtxtt, titlet, btimet, etimet)
240 except Exception, inst:
241 print "error occures in search_keyword"
242 print str(type(inst))
244 def type_reserve(typetxt,chtxt,title,bt,et,opt):
245 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
246 etime = et.strftime("%Y-%m-%d %H:%M:%S")
249 if typetxt==recdblist.REC_RESERVE:
250 typetxtfinal=recdblist.REC_FINAL_RESERVE
251 tnow = datetime.datetime.now()
253 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
254 if (dt < 58 * 60 and dt > 30 * 60):
255 bctypet = chdb.chtxtsearch(chtxt)['bctype']
256 chdatat = rec10d.rec10db.select_by_bctype_epg_ch(bctypet)
257 dt1 = datetime.datetime.strptime(chdatat[0][5], "%Y-%m-%d %H:%M:%S")-datetime.datetime.now()
258 dt1 = dt1.days * 24 * 60 * 60 + dt1.seconds
260 recdata = epgdb.searchtime2(title, btime, "5", chtxt)
268 bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
269 et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
270 btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
271 etimen = et.strftime("%Y-%m-%d %H:%M:%S")
274 recdb.del_reckey(typetxtnow, title, chtxt, btime)
275 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
276 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
277 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
278 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
279 recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
280 except Exception, inst:
281 print "Error happened in Oikake DB"
282 print str(type(inst))
285 recdblist.printutf8(u"追いかけ機能エラー:番組データが見付かりません。")
287 if rec10d.rec10db.select_by_bctype_epg_ch(bctypet)[0][6] != "0":
288 rec10d.rec10db.update_status_by_bctype_epg_ch(bctypet, "3")
290 elif (dt <= 30 * 60 and dt > 20 * 60):
291 recdata = epgdb.searchtime2(title, btime, "5", chtxt)
299 bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
300 et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
301 btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
302 etimen = et.strftime("%Y-%m-%d %H:%M:%S")
305 recdb.del_reckey(typetxtnow, title, chtxt, btime)
306 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
307 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
308 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
309 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
310 recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
311 except Exception, inst:
312 print "Error happened in Oikake DB"
317 recdb.del_reckey(typetxtnow, title, chtxt, btime)
318 recdb.rec_reckey(typetxtfinal, title, chtxt, btime, etime, opt)
319 except Exception, inst:
322 def type_final(typetxt,chtxt,title,bt,et,opt):
323 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
324 etime = et.strftime("%Y-%m-%d %H:%M:%S")
328 if typetxt==recdblist.REC_FINAL_RESERVE:
329 typetxting=recdblist.REC_TS_RECORDING
330 typetxtdecque=recdblist.REC_TS_DECODE_QUE
331 tnow = datetime.datetime.now()
333 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
335 if dt < 6 * 60 and dt > 0:
337 recdb.del_reckey(typetxtnow, title, chtxt, btime)
338 testpath=[os.path.join(recpath,title+".ts.b25")]
339 testpath.append(os.path.join(recpath,title+".ts"))
340 testpath.append(os.path.join(recpath,title+".avi"))
341 testpath.append(os.path.join(recpath,title+".mp4"))
342 testpath.append(os.path.join(recpath,title+".log"))
345 if os.path.exists(ti):
347 if re.search("N", opt) or tcheck>0:
350 iff=u"("+configreader.getenv("iff")+u")_"
354 newtime=newtime+datetime.timedelta(seconds=10)
355 newtitle=newtitle+u"_"+iff+newtime.strftime("%Y-%m-%dT%H-%M-%S")
356 recdb.rec_reckey(typetxting, newtitle, chtxt, btime, etime, opt)
357 recdblist.printutf8(u"録画開始 "+newtitle+" "+btime+" "+etime)
358 tv2avi.timetv2b25(recpath + "/" + newtitle + ".avi", chtxt, btime, etime, opt)
359 recdb.del_reckey(typetxting, newtitle, chtxt, btime)
360 if not re.search("R", opt):
361 tnow = datetime.datetime.now()
362 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
363 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
365 bt = tnow + datetime.timedelta(seconds=600)
366 et = et + dt + datetime.timedelta(seconds=600)
367 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
368 etime = et.strftime("%Y-%m-%d %H:%M:%S")
369 recdb.rec_reckey(typetxtdecque, newtitle, chtxt, btime, etime, opt)
372 shutil.copy(os.path.join(recpath,title+".ts.b25"), os.path.join(movepath,title+".ts.b25"))
375 recdb.rec_reckey(recdblist.REC_MOVE_END, newtitle, chtxt, btime, etime, opt)
377 def type_keyword(typetxt,chtxt,title,bt,et,opt,deltatime):
378 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
379 etime = et.strftime("%Y-%m-%d %H:%M:%S")
382 if typetxt==recdblist.REC_KEYWORD:
383 typetxtres=recdblist.REC_RESERVE
384 tnow = datetime.datetime.now()
386 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
389 recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
398 bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
399 et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
400 btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
401 etimet = et.strftime("%Y-%m-%d %H:%M:%S")
403 recdb.del_reckey(typetxtnow, title, chtxt, btime)
404 recdb.rec_reckey(typetxtres, titlet, chtxtt, btimet, etimet, opt)
405 recdb.rec_reclog(titlet, chtxtt, btimet, etimet, opt, exp,longexp,category)
406 auto_rec.add_key(chtxt, titlet,exp+" "+longexp)
407 auto_rec.add_key("ALL", titlet,exp+" "+longexp)
408 recdblist.printutf8(u"key "+title+u" : "+titlet+u" "+btimet+u" "+etimet)
409 #except Exception, inst:
410 # recdblist.printutf8("Error happened in REC_KEYWORD DB")
411 # recdblist.printutf8(type(inst))
412 # recdblist.printutf8(inst)
414 recdblist.printutf8("nothing match")
416 def type_keyword_every_day(type,chtxt,title,bt,et,opt,deltatime,deltaday):
417 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
418 etime = et.strftime("%Y-%m-%d %H:%M:%S")
419 tnow = datetime.datetime.now()
421 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
422 dd = datetime.timedelta(days=int(deltaday))
424 recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
427 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
428 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
429 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
431 recdb.keyreserv(title, chtxt, btime, etime, deltatime, opt)
432 recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
435 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
436 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
437 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
440 def type_decode_que(typetxt,chtxt,title,bt,et,opt):
441 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
442 etime = et.strftime("%Y-%m-%d %H:%M:%S")
443 tnow = datetime.datetime.now()
445 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
450 if typetxt== recdblist.REC_TS_DECODE_QUE:
451 typetxting=recdblist.REC_TS_DECODING
452 typetxtmiss=recdblist.REC_MISS_DECODE
453 typetxtencque=recdblist.REC_ENCODE_QUE
454 tnow = datetime.datetime.now()
456 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
458 if status.getB25Decoding() < 2:
459 pin = recpath + "/" + title
460 recdb.del_reckey(typetxtnow, title, chtxt, btime)
461 recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
462 tv2avi.b252ts(pin, chtxt, btime, etime, opt)
463 recdb.del_reckey(typetxting, title, chtxt, btime)
464 if not os.access(recpath + "/" + title + ".ts", os.F_OK):
465 recdb.del_reckey(typetxting, title, chtxt, btime)
466 recdb.rec_reckey(typetxtmiss, title, chtxt, btime, etime, opt)
468 ch=checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts")
471 os.remove(recpath + "/" + title + ".ts.b25")
472 bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
473 et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
475 if not re.search("D", opt):
476 tnow = datetime.datetime.now()
477 bt = tnow + datetime.timedelta(seconds=600)
479 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
480 etime = et.strftime("%Y-%m-%d %H:%M:%S")
481 recdb.rec_reckey(typetxtencque, title, chtxt, btime, etime, opt)
484 shutil.copy(os.path.join(recpath,title+".ts"), os.path.join(movepath,title+".ts"))
485 except Exception, inst:
486 errtxt="Error happened in type_decode_que in timerec.\n"
487 errtxt=errtxt+"move ts error.\n"
488 errtxt=errtxt+str(type(inst))+"\n"
489 errtxt=errtxt+str(inst)
490 recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Decode) error")
491 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
493 recdb.del_reckey(typetxtnow, title, chtxt, btime)
494 bt = bt + datetime.timedelta(seconds=600)
495 et = et + datetime.timedelta(seconds=600)
496 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
497 etime = et.strftime("%Y-%m-%d %H:%M:%S")
498 recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
501 def type_encode_que(typetxt,chtxt,title,bt,et,opt):
502 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
503 etime = et.strftime("%Y-%m-%d %H:%M:%S")
504 tnow = datetime.datetime.now()
506 dt = dtt.days * 24 * 60 * 60 + dtt.seconds
510 if typetxt==recdblist.REC_ENCODE_QUE:
511 typetxting=recdblist.REC_ENCODE_LOCAL
512 typetxtfin=recdblist.REC_FIN_LOCAL
514 if status.getEncoding() < int(configreader.getenv("enc_max")):
515 recdb.del_reckey(typetxtnow, title, chtxt, btime)
516 recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
517 recdblist.printutf8(opt)
518 pin = recpath + "/" + title + ".ts"
519 if re.search("d", opt):
520 pin = recpath + "/" + title + ".m2v"
521 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
522 paac2 = recpath + "/" + title + "_2.aac"
523 pmp32 = recpath + "/" + title + "_2.mp3"
524 if not os.path.exists(paac2) and not os.path.exists(pmp32):
525 tv2avi.dualaudio2sep(os.path.join(recpath, title+".ts"),"167")
527 elif re.search("5", opt):
528 pin = recpath + "/" + title + ".m2v"
529 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
530 paac2 = recpath + "/" + title + "_2.aac"
531 pmp32 = recpath + "/" + title + "_2.mp3"
532 if not os.path.exists(paac2) and not os.path.exists(pmp32):
533 tv2avi.pentaaudio2sep(os.path.join(recpath, title+".ts"))
535 pout = recpath + "/" + title + ".mkv"
536 recdblist.printutf8(pin)
537 tv2mkv.ts2mkv(pin, pout, opt)
538 recdb.del_reckey(typetxting, title, chtxt, btime)
540 if re.search("E", opt):
542 if os.path.exists(os.path.join(recpath,title+".mp4")):
543 shutil.copy(os.path.join(recpath,title+".mp4"), os.path.join(movepath,title+".mp4"))
544 elif os.path.exists(os.path.join(recpath,title+".mkv")):
545 shutil.copy(os.path.join(recpath,title+".mkv"), os.path.join(movepath,title+".mkv"))
546 except Exception, inst:
547 errtxt="Error happened in type_encode_que in timerec.\n"
548 errtxt=errtxt+"move mkv/mp4 error.\n"
549 errtxt=errtxt+str(type(inst))+"\n"
550 errtxt=errtxt+str(inst)
551 recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Encode) error")
552 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
553 recdb.rec_reckey(typetxtfin, title, chtxt, btime, etime, opt)
556 recdb.del_reckey(typetxtnow, title, chtxt, btime)
557 bt = bt + datetime.timedelta(seconds=600)
558 et = et + datetime.timedelta(seconds=600)
559 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
560 etime = et.strftime("%Y-%m-%d %H:%M:%S")
561 recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)