OSDN Git Service

implement recque system.
[rec10/rec10-git.git] / rec10 / trunk / src / timerec.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5 import datetime
6 import os
7 import os.path
8 import re
9 import sys
10 import time
11 import shutil
12 import traceback
13
14 import auto_rec
15 import auto_process
16 import chdb
17 import configreader
18 import epgdb
19 import rec10d
20 import recdb
21 import status
22 import tv2audio
23 import tv2avi
24 import tv2mkv
25 import tv2mp4
26 import install
27 import recdblist
28 import recque
29 recpath = configreader.getpath('recpath')
30 movepath = configreader.getpath('move_destpath')
31 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
32 def task():
33     """
34     数分毎に実行されるタスク処理
35     予定によって子プロセスを生成し処理する。
36     """
37     try:
38         if rec10d.rec10db.select_installed_in_status()==0 or rec10d.rec10db.select_version_in_status()<recdblist.version:
39             install.install()
40     except:
41         install.install()
42     recdb.delete_old("24")
43     recdb.delete_old_auto_bayes("1")
44     recdb.delete_old_auto_keyword("1")
45     tasks = recdb.getnow("3")
46     inum = recdb.countRecNow_minutes("10")
47     recdblist.printutf8(u"rec10処理開始"+ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),verbose_level=800)
48     recdblist.printutf8(u"直近録画予約件数:" + str(inum) + u"BS/CS録画中件数:" + str(status.getBSCSRecording()) + u"TE録画中件数:" + str(status.getTERecording()) ,verbose_level=800)
49     encodenum=0
50     b25num=0
51     dnow=datetime.datetime.now()
52     if dnow.minute%10<5:
53         pid = os.fork()
54         if pid != 0:
55             ""
56         else:
57             for k in recdb.get_key():
58                 search_keyword(k)
59             recque.searchRecQue(recpath)
60             sys.exit()
61     else:
62         pid = os.fork()
63         if pid!=0:
64             ""
65         else:
66             time.sleep(10)
67             auto_process.auto_check(recpath)
68             time.sleep(10)
69             auto_process.kill_dead_encode(recpath)
70             sys.exit()
71     update = chdb.update()
72     if len(update) > 0:
73         pid = os.fork()
74         if pid != 0:
75             ""
76         else:
77             i = 0
78             for bctype in update:
79                 recnum = 0
80                 if bctype.find('te') > -1:
81                     recnum = status.getTERecording() + recdb.countRecNow_minutes_TE("10")
82                     recdblist.printutf8(u"放送種別:"+bctype + u"||該当チューナー実行中件数:" + str(status.getTERecording()) + u":直近予約件数:" + str(recdb.countRecNow_minutes_TE("10")),verbose_level=800)
83                     recnum = int(configreader.getenv("te_max")) -recnum
84                 else:
85                     recnum = status.getBSCSRecording() + recdb.countRecNow_minutes_BSCS("10")
86                     recdblist.printutf8(u"放送種別:"+bctype + u"||該当チューナー実行中件数:" + str(status.getBSCSRecording()) + u":直近予約件数:" + str(recdb.countRecNow_minutes_BSCS("10")),verbose_level=800)
87                     recnum = int(configreader.getenv("bscs_max")) -recnum
88                 if recnum >0 :
89                     recdblist.printutf8(str(update),verbose_level=750)
90                     rec10d.rec10db.update_status_by_bctype_epg_ch(bctype, "0")
91                     epgdb.updatebc(bctype)
92                     update = chdb.update()
93                     i = i + 1
94                 if i > 0:
95                     break
96             time.sleep(5)
97             auto_process.update_all_timeline_epg()
98             sys.exit()
99     else:
100         pid = os.fork()
101         if pid != 0:
102             ""
103         else:
104             auto_process.update_all_timeline_epg()
105             sys.exit()
106     for task in tasks:
107         typetxt = task["type"]
108         try:
109             chtxt = task['chtxt']
110         except:
111             chtxt = ""
112         try:
113             title = task['title']
114         except:
115             title = ""
116         try:
117             btime = task['btime']
118             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
119         except:
120             btime = ""
121             bt = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
122         try:
123             etime = task['etime']
124             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
125         except:
126             etime = ""
127             et = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
128         try:
129             opt = task['opt']
130         except:
131             opt = ""
132         tnow = datetime.datetime.now()
133         dtt = bt-tnow
134         dt = dtt.days * 24 * 60 * 60 + dtt.seconds
135         if task["type"] == recdblist.REC_RESERVE:#"res,"+chtxt+","+title+","+btime+","+etime+","+opt
136             pid = os.fork()
137             if pid != 0:#親プロセスの場合
138                 ""
139             else:#子プロセスの場合 アップデートを行って終了
140                 type_reserve(task["type"],chtxt,title,bt,et,opt)
141                 sys.exit()
142         elif task["type"] == recdblist.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
143             deltatime = task['deltatime']
144             pid = os.fork()
145             if pid != 0:#親プロセスの場合
146                 ""
147             else:#子プロセスの場合 アップデートを行って終了
148                 type_keyword(task["type"],chtxt,title, bt, et, opt, deltatime)
149                 sys.exit()
150         elif task["type"] == recdblist.REC_KEYWORD_EVERY_SOME_DAYS:#"keyevery,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt+","+deltaday
151             deltatime = task['deltatime']
152             deltaday = task['deltaday']
153             try:
154                 keyeverycounter=task['counter']
155             except:
156                 keyeverycounter=-1
157             pid = os.fork()
158             if pid != 0:#親プロセスの場合
159                 ""
160             else:#子プロセスの場合 アップデートを行って終了
161                 type_keyword_every_day(task["type"],chtxt, title, bt, et, opt, deltatime, deltaday,keyeverycounter)
162                 sys.exit()
163         elif task["type"] == recdblist.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
164             pid = os.fork()
165             if pid != 0:#親プロセスの場合
166                 ""
167             else:#子プロセスの場合 アップデートを行って終了
168                 type_final(task["type"],chtxt, title, bt, et, opt)
169                 sys.exit()
170         elif task["type"] == recdblist.REC_TS_DECODE_QUE:
171             b25num=b25num+1
172             pid = os.fork()
173             if pid != 0:#親プロセスの場合
174                 ""
175             else:#子プロセスの場合 アップデートを行って終了
176                 time.sleep(5*b25num)
177                 type_decode_que(task["type"],chtxt, title, bt, et, opt)
178                 sys.exit()
179         elif task["type"] == recdblist.REC_ENCODE_QUE:
180             encodenum=encodenum+1
181             pid = os.fork()
182             if pid != 0:#親プロセスの場合
183                 ""
184             else:#子プロセスの場合 アップデートを行って終了
185                 time.sleep(5*encodenum)
186                 type_encode_que(task["type"],chtxt, title, bt, et, opt)
187                 sys.exit()
188         elif task["type"] == recdblist.REC_AVI_TO_MKV:
189             if dt < 10 * 60:
190                 pid = os.fork()
191                 if pid > 0:#親プロセスの場合
192                     ""
193                 else:
194                     makeMP4=0
195                     try:
196                         if configreader.getenv("make_mp4")==1:
197                             makeMP4=1
198                     except:
199                         ""
200                     if re.search("m", opt):
201                         makeMP4=0
202                     if re.search("4", opt):
203                         makeMP4=1
204                     recdb.del_reckey(recdblist.REC_AVI_TO_MKV, title, chtxt, btime)
205                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
206                     if makeMP4==1:
207                         tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
208                     else:
209                         tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
210                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
211                     sys.exit()
212         elif task["type"] == recdblist.REC_AVI_TO_MP4:
213             if dt < 10 * 60:
214                 pid = os.fork()
215                 if pid > 0:#親プロセスの場合
216                     ""
217                 else:
218                     makeMP4=0
219                     try:
220                         if configreader.getenv("make_mp4")==1:
221                             makeMP4=1
222                     except:
223                         ""
224                     if re.search("m", opt):
225                         makeMP4=0
226                     if re.search("4", opt):
227                         makeMP4=1
228                     recdb.del_reckey(recdblist.REC_AVI_TO_MP4, title, chtxt, btime)
229                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
230                     if makeMP4==1:
231                         tv2mp4.ts2mp4(pin, pout, opt)
232                         tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
233                     else:
234                         tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
235                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
236                     sys.exit()
237         elif task["type"] == recdblist.REC_MKV_TO_MP4:
238             if dt < 10 * 60:
239                 pid = os.fork()
240                 if pid > 0:#親プロセスの場合
241                     ""
242                 else:
243                     recdb.del_reckey(recdblist.REC_MKV_TO_MP4, title, chtxt, btime)
244                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
245                     tv2mp4.mkv2mp4(os.path.join(recpath,title+".mkv"),os.path.join(recpath,title+".mp4"))
246                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
247                     sys.exit()
248     sys.exit()
249 def search_keyword(key):
250     tnow = datetime.datetime.now()
251     nows =tnow.strftime("%Y-%m-%d %H:%M:%S")
252     for k in key:
253         recdatum = epgdb.searchtime_auto(k,nows, "144")
254         for recdata in recdatum:
255             if recdata[1] != "":
256                 chtxtt = recdata[0]
257                 titlet = recdata[1]
258                 btimet = recdata[2]
259                 etimet = recdata[3]
260                 btt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
261                 ett = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
262                 btimet = btt.strftime("%Y-%m-%d %H:%M:%S")
263                 etimet = ett.strftime("%Y-%m-%d %H:%M:%S")
264                 if status.getSettings_auto_jbk()==1:
265                     topt=status.getSettings_auto_opt()
266                     try:
267                         maxnum=0
268                         if len(ch['ch'])>2:
269                             maxnum=epgdb.count_schedule_timeline(btimet, etimet)[1]
270                             maxnum=int(configreader.getenv("bscs_max"))-maxnum
271                         else:
272                             maxnum=epgdb.count_schedule_timeline(btimet, etimet)[0]
273                             maxnum=int(configreader.getenv("te_max"))-maxnum
274                         if maxnum>0:
275                             topt=status.getSettings_auto_opt()
276                             recdb.recreserv(titlet,chtxtt, btimet, etimet,topt)
277                     except Exception, inst:
278                         recdblist.Commonlogex("Error", "search_keyword_auto_jbk(timerec.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
279                 else:
280                     try:
281                         recdb.add_auto_keyword(chtxtt, titlet, btimet, etimet)
282                     except Exception, inst:
283                         recdblist.Commonlogex("Error", "search_keyword(timerec.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
284 def type_reserve(typetxt,chtxt,title,bt,et,opt):
285     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
286     etime = et.strftime("%Y-%m-%d %H:%M:%S")
287     typetxtnow=typetxt
288     typetxtfinal=""
289     if typetxt==recdblist.REC_RESERVE:
290         typetxtfinal=recdblist.REC_FINAL_RESERVE
291     tnow = datetime.datetime.now()
292     dtt = bt-tnow
293     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
294     if (dt < 58 * 60 and dt > 30 * 60):
295         bctypet = chdb.chtxtsearch(chtxt)['bctype']
296         chdatat = rec10d.rec10db.select_by_bctype_epg_ch(bctypet)
297         dt1 = datetime.datetime.strptime(chdatat[0][5], "%Y-%m-%d %H:%M:%S")-datetime.datetime.now()
298         dt1 = dt1.days * 24 * 60 * 60 + dt1.seconds
299         if dt1 < 60 * 60:
300             recdata = epgdb.searchtime2(title, btime, "5", chtxt)
301             chtxtn = recdata[0]
302             titlen = recdata[1]
303             btimen = recdata[2]
304             etimen = recdata[3]
305             exp = recdata[4]
306             longexp = recdata[5]
307             category=recdata[6]
308             bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
309             et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
310             btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
311             etimen = et.strftime("%Y-%m-%d %H:%M:%S")
312             if chtxt != "":
313                 try:
314                     recdb.del_reckey(typetxtnow, title, chtxt, btime)
315                     recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
316                     recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
317                     auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
318                     auto_rec.add_key("ALL", titlen,exp+" "+longexp)
319                     recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
320                 except Exception, inst:
321                     recdblist.Commonlogex("Error", "Oikake DB(timerec.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
322             else:
323                 recdblist.printutf8(u"追いかけ機能エラー:番組データが見付かりません。")
324         else:
325             if rec10d.rec10db.select_by_bctype_epg_ch(bctypet)[0][6] != "0":
326                 rec10d.rec10db.update_status_by_bctype_epg_ch(bctypet, "3")
327         sys.exit()
328     elif (dt <= 30 * 60 and dt > 20 * 60):
329         recdata = epgdb.searchtime2(title, btime, "5", chtxt)
330         chtxtn = recdata[0]
331         titlen = recdata[1]
332         btimen = recdata[2]
333         etimen = recdata[3]
334         exp = recdata[4]
335         longexp = recdata[5]
336         category=recdata[6]
337         bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
338         et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
339         btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
340         etimen = et.strftime("%Y-%m-%d %H:%M:%S")
341         if chtxt != "":
342             try:
343                 recdb.del_reckey(typetxtnow, title, chtxt, btime)
344                 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
345                 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
346                 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
347                 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
348                 #recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
349                 recdblist.Commonlogex(u"通常", "Oikake (timerec.py)",u"追いかけ機能実行中",u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen,log_level=500)
350             except Exception, inst:
351                 recdblist.Commonlogex("Error", "Oikake DB(timerec.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
352     elif dt <= 20 * 60:
353         try:
354             recdb.del_reckey(typetxtnow, title, chtxt, btime)
355             recdb.rec_reckey(typetxtfinal, title, chtxt, btime, etime, opt)
356         except Exception, inst:
357             recdblist.Commonlogex("Error", "Oikake DB(timerec.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
358 def type_final(typetxt,chtxt,title,bt,et,opt):
359     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
360     etime = et.strftime("%Y-%m-%d %H:%M:%S")
361     typetxtnow=typetxt
362     typetxting=""
363     typetxtdecque=""
364     if typetxt==recdblist.REC_FINAL_RESERVE:
365         typetxting=recdblist.REC_TS_RECORDING
366         typetxtdecque=recdblist.REC_TS_DECODE_QUE
367     tnow = datetime.datetime.now()
368     dtt = bt-tnow
369     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
370     if dt < 6 * 60 and dt > 0:
371         newtitle=title
372         recdb.del_reckey(typetxtnow, title, chtxt, btime)
373         testpath=[os.path.join(recpath,title+".ts.b25")]
374         testpath.append(os.path.join(recpath,title+".ts"))
375         testpath.append(os.path.join(recpath,title+".avi"))
376         testpath.append(os.path.join(recpath,title+".mp4"))
377         testpath.append(os.path.join(recpath,title+".log"))
378         tcheck=0
379         for ti in testpath:
380             if os.path.exists(ti):
381                 tcheck=tcheck+1
382         if re.search("N", opt) or tcheck>0:
383             iff=""
384             try:
385                 iff=u"("+configreader.getenv("iff")+u")_"
386             except:
387                 iff=""
388             newtime=bt
389             newtitle=newtitle+u"_"+iff+newtime.strftime("%Y-%m-%dT%H-%M-%S")
390         recdb.rec_reckey(typetxting, newtitle, chtxt, btime, etime, opt)
391         recdblist.Commonlogex(u"通常","timerec.py",u"録画開始 "+newtitle+" "+btime+" "+etime,"",log_level=500)
392         tv2avi.timetv2b25(recpath + "/" + newtitle + ".avi", chtxt, btime, etime, opt)
393         recdb.del_reckey(typetxting, newtitle, chtxt, btime)
394         if not re.search("R", opt):
395             tnow = datetime.datetime.now()
396             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
397             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
398             dt = tnow-bt
399             bt = tnow + datetime.timedelta(seconds=600)
400             et = et + dt + datetime.timedelta(seconds=600)
401             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
402             etime = et.strftime("%Y-%m-%d %H:%M:%S")
403             recdb.rec_reckey(typetxtdecque, newtitle, chtxt, btime, etime, opt)
404         else:
405             try:
406                 try:
407                     shutil.copy(os.path.join(recpath,title+".ts.b25"), os.path.join(movepath,title+".ts.b25"))
408                 except:
409                     ""
410                 os.chmod(os.path.join(movepath,title+".ts.b25"),0777)
411                 recque.writeRecQue(movepath, chtxt, title, opt)
412                 os.chmod(os.path.join(movepath,title+".recq"),0777)
413             except Exception, inst:
414                 recdblist.Commonlogex("Error", "Move option(timerec.py)", str(type(inst)),str(inst)+traceback.format_exc(),log_level=200)
415             recdb.rec_reckey(recdblist.REC_MOVE_END, newtitle, chtxt, btime, etime, opt)
416         sys.exit()
417 def type_keyword(typetxt,chtxt,title,bt,et,opt,deltatime):
418     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
419     etime = et.strftime("%Y-%m-%d %H:%M:%S")
420     typetxtnow=typetxt
421     typetxtres=""
422     if typetxt==recdblist.REC_KEYWORD:
423         typetxtres=recdblist.REC_RESERVE
424     tnow = datetime.datetime.now()
425     dtt = bt-tnow
426     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
427     recdblist.printutf8(str(dt), verbose_level=800)
428     if dt < 90 * 60:
429         recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
430         if recdata[1] != "":
431             chtxtt = recdata[0]
432             titlet = recdata[1]
433             btimet = recdata[2]
434             etimet = recdata[3]
435             exp = recdata[4]
436             longexp = recdata[5]
437             category=recdata[6]
438             bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
439             et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
440             btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
441             etimet = et.strftime("%Y-%m-%d %H:%M:%S")
442             #try:
443             recdb.del_reckey(typetxtnow, title, chtxt, btime)
444             recdb.rec_reckey(typetxtres, titlet, chtxtt, btimet, etimet, opt)
445             recdb.rec_reclog(titlet, chtxtt, btimet, etimet, opt, exp,longexp,category)
446             auto_rec.add_key(chtxt, titlet,exp+" "+longexp)
447             auto_rec.add_key("ALL", titlet,exp+" "+longexp)
448             recdblist.Commonlogex(u"通常","timerec.py",u"key "+title+u" : "+titlet+u" "+btimet+u" "+etimet,"",log_level=500)
449             #except Exception, inst:
450             #    recdblist.printutf8("Error happened in REC_KEYWORD DB")
451             #    recdblist.printutf8(type(inst))
452             #    recdblist.printutf8(inst)
453         else:
454             recdblist.printutf8("nothing match")
455 def type_keyword_every_day(type,chtxt,title,bt,et,opt,deltatime,deltaday,counter):
456     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
457     etime = et.strftime("%Y-%m-%d %H:%M:%S")
458     tnow = datetime.datetime.now()
459     dtt = bt-tnow
460     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
461     dd = datetime.timedelta(days=int(deltaday))
462     if dtt.days < 0:
463         recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
464         bt = bt + dd
465         et = et + dd
466         btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
467         etxt = et.strftime("%Y-%m-%d %H:%M:%S")
468         recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
469     elif dt < 120 * 60:
470         recdb.del_reckey(recdblist.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
471         bt = bt + dd
472         et = et + dd
473         btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
474         etxt = et.strftime("%Y-%m-%d %H:%M:%S")
475         if counter>0:
476             recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday,counter-1)
477         elif counter==-1:
478             recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday,-1)
479         recdata = epgdb.searchtime2(title, btime, deltatime, chtxt)
480         if recdata[1] != "":
481             chtxtt = recdata[0]
482             titlet = recdata[1]
483             btimet = recdata[2]
484             etimet = recdata[3]
485             exp = recdata[4]
486             longexp = recdata[5]
487             category=recdata[6]
488             bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
489             et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
490             btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
491             etimet = et.strftime("%Y-%m-%d %H:%M:%S")
492             #try:
493             recdb.keyreserv(titlet, chtxtt, btimet, etimet, deltatime, opt)
494         else:
495             recdb.keyreserv(title, chtxt, btime, etime, deltatime, opt)
496 def type_decode_que(typetxt,chtxt,title,bt,et,opt):
497     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
498     etime = et.strftime("%Y-%m-%d %H:%M:%S")
499     tnow = datetime.datetime.now()
500     dtt = bt-tnow
501     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
502     typetxtnow=typetxt
503     typetxting=""
504     typetxtmiss=""
505     typetxtencque=""
506     if typetxt== recdblist.REC_TS_DECODE_QUE:
507         typetxting=recdblist.REC_TS_DECODING
508         typetxtmiss=recdblist.REC_MISS_DECODE
509         typetxtencque=recdblist.REC_ENCODE_QUE
510     tnow = datetime.datetime.now()
511     dtt = bt-tnow
512     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
513     if dt < 10 * 60:
514         if status.getB25Decoding() < 2:
515             pin = recpath + "/" + title
516             recdb.del_reckey(typetxtnow, title, chtxt, btime)
517             recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
518             tv2avi.b252ts(pin, chtxt, btime, etime, opt)
519             recdb.del_reckey(typetxting, title, chtxt, btime)
520             if not os.access(recpath + "/" + title + ".ts", os.F_OK):
521                 recdb.del_reckey(typetxting, title, chtxt, btime)
522                 recdb.rec_reckey(typetxtmiss, title, chtxt, btime, etime, opt)
523             else:
524                 auto_process.deltmpfile(recpath, title, ".ts")
525             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
526             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
527             dt = et-bt
528             if not re.search("D", opt):
529                 tnow = datetime.datetime.now()
530                 bt = tnow + datetime.timedelta(seconds=600)
531                 et = bt + dt
532                 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
533                 etime = et.strftime("%Y-%m-%d %H:%M:%S")
534                 recdb.rec_reckey(typetxtencque, title, chtxt, btime, etime, opt)
535             else:
536                 try:
537                     try:
538                         shutil.copy(os.path.join(recpath,title+".ts"), os.path.join(movepath,title+".ts"))
539                     except:
540                         ""
541                     os.chmod(os.path.join(movepath,title+".ts"),0777)
542                     recque.writeRecQue(movepath, chtxt, title, opt)
543                     os.chmod(os.path.join(movepath,title+".recq"),0777)
544                     time.sleep(5)
545                     if os.path.getsize(os.path.join(recpath,title+".ts"))==os.path.getsize(os.path.join(movepath,title+".ts")):
546                         os.remove(os.path.join(recpath,title+".ts"))
547                 except Exception, inst:
548                     errtxt1="move ts error.\n"
549                     errtxt2=str(type(inst))+"\n"
550                     errtxt2=errtxt2+str(inst)
551                     recdblist.Commonlogex("Error", "type_decode_que(timerec.py)", errtxt1,errtxt2+traceback.format_exc(),log_level=200)
552                 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
553         else:
554             recdb.del_reckey(typetxtnow, title, chtxt, btime)
555             bt = bt + datetime.timedelta(seconds=600)
556             et = et + datetime.timedelta(seconds=600)
557             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
558             etime = et.strftime("%Y-%m-%d %H:%M:%S")
559             recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
560     sys.exit()
561
562 def type_encode_que(typetxt,chtxt,title,bt,et,opt):
563     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
564     etime = et.strftime("%Y-%m-%d %H:%M:%S")
565     tnow = datetime.datetime.now()
566     dtt = bt-tnow
567     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
568     typetxtnow=typetxt
569     typetxting=""
570     typetxtfin=""
571     if typetxt==recdblist.REC_ENCODE_QUE:
572         typetxting=recdblist.REC_ENCODE_LOCAL
573         typetxtfin=recdblist.REC_FIN_LOCAL
574     if dt < 10 * 60:
575         if status.getEncoding() < int(configreader.getenv("enc_max")):
576             recdb.del_reckey(typetxtnow, title, chtxt, btime)
577             recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
578             recdblist.printutf8(opt)
579             pin = recpath + "/" + title + ".ts"
580             if re.search("d", opt):
581                 pin = recpath + "/" + title + ".m2v"
582                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
583                     paac2 = recpath + "/" + title + "_2.aac"
584                     pmp32 = recpath + "/" + title + "_2.mp3"
585                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
586                         tv2audio.ts2dualaudio_BonTsDemux(os.path.join(recpath, title+".ts"),recdblist.BONTSDEMUX_DELAY,opt)
587                 time.sleep(3)
588             elif re.search("5", opt):
589                 pin = recpath + "/" + title + ".m2v"
590                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
591                     paac2 = recpath + "/" + title + "_2.aac"
592                     pmp32 = recpath + "/" + title + "_2.mp3"
593                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
594                         tv2audio.ts2pentaaudio_BonTsDemux(os.path.join(recpath, title+".ts"), recdblist.BONTSDEMUX_DELAY, opt)
595                 time.sleep(3)
596             makeMP4=0
597             try:
598                 if configreader.getenv("make_mp4")=="1":
599                     makeMP4=1
600             except:
601                 ""
602             if re.search("m", opt):
603                 makeMP4=0
604             if re.search("4", opt):
605                 makeMP4=1
606             recdblist.printutf8(pin)
607             if not re.search("0", opt):
608                 if makeMP4==1:
609                     pout = recpath + "/" + title + ".mp4"
610                     tv2mp4.ts2mp4(pin, pout, opt)
611                 else:
612                     pout = recpath + "/" + title + ".mkv"
613                     tv2mkv.ts2mkv(pin, pout, opt)
614                 if re.search("8", opt) or re.search("9", opt):
615                     optt=opt
616                     optt=optt.replace("8","MW8")
617                     optt=optt.replace("9","MW9")
618                     poutt = recpath + "/" + "m_"+title + ".mp4"
619                     if re.search("d", opt) or re.search("5", opt):
620                         if os.path.exists(os.path.join(recpath, "m_"+title+".m2v")):
621                             shutil.move(os.path.join(recpath, title+".m2v"),os.path.join(recpath, "m_"+title+".m2v"))
622                         if os.path.exists(os.path.join(recpath, "m_"+title+"_1.aac")):
623                             shutil.move(os.path.join(recpath, title+"_1.aac"),os.path.join(recpath, "m_"+title+"_1.aac"))
624                         if os.path.exists(os.path.join(recpath, "m_"+title+"_1.mp3")):
625                             shutil.move(os.path.join(recpath, title+"_1.mp3"),os.path.join(recpath, "m_"+title+"_1.mp3"))
626                         if os.path.exists(os.path.join(recpath, "m_"+title+"_2.aac")):
627                             shutil.move(os.path.join(recpath, title+"_2.aac"),os.path.join(recpath, "m_"+title+"_2.aac"))
628                         if os.path.exists(os.path.join(recpath, "m_"+title+"_2.mp3")):
629                             shutil.move(os.path.join(recpath, title+"_2.mp3"),os.path.join(recpath, "m_"+title+"_2.mp3"))
630                     shutil.move(os.path.join(recpath, title+".ts"),os.path.join(recpath, "m_"+title+".ts"))
631                     tv2mp4.ts2mp4(os.path.join(recpath, "m_"+title+".ts"), poutt, optt)
632                     shutil.move(os.path.join(recpath, "m_"+title+".ts"),os.path.join(recpath, title+".ts"))
633             else:
634                 optt=opt
635                 optt=optt.replace("8","MW8")
636                 optt=optt.replace("9","MW9")
637                 poutt = recpath + "/" + "m_"+title + ".mp4"
638                 shutil.move(os.path.join(recpath, title+".ts"),os.path.join(recpath, "m_"+title+".ts"))
639                 if re.search("d", opt) or re.search("5", opt):
640                     if os.path.exists(os.path.join(recpath, "m_"+title+".m2v")):
641                         shutil.move(os.path.join(recpath, title+".m2v"),os.path.join(recpath, "m_"+title+".m2v"))
642                     if os.path.exists(os.path.join(recpath, "m_"+title+"_1.aac")):
643                         shutil.move(os.path.join(recpath, title+"_1.aac"),os.path.join(recpath, "m_"+title+"_1.aac"))
644                     if os.path.exists(os.path.join(recpath, "m_"+title+"_1.mp3")):
645                         shutil.move(os.path.join(recpath, title+"_1.mp3"),os.path.join(recpath, "m_"+title+"_1.mp3"))
646                     if os.path.exists(os.path.join(recpath, "m_"+title+"_2.aac")):
647                         shutil.move(os.path.join(recpath, title+"_2.aac"),os.path.join(recpath, "m_"+title+"_2.aac"))
648                     if os.path.exists(os.path.join(recpath, "m_"+title+"_2.mp3")):
649                         shutil.move(os.path.join(recpath, title+"_2.mp3"),os.path.join(recpath, "m_"+title+"_2.mp3"))
650                 tv2mp4.ts2mp4(os.path.join(recpath, "m_"+title+".ts"), poutt, optt)
651                 shutil.move(os.path.join(recpath, "m_"+title+".ts"),os.path.join(recpath, title+".ts"))
652             recdb.del_reckey(typetxting, title, chtxt, btime)
653             time.sleep(10)
654             if re.search("E", opt):
655                 try:
656                     if os.path.exists(os.path.join(recpath,title+".mp4")):
657                         try:
658                             shutil.copy(os.path.join(recpath,title+".mp4"), os.path.join(movepath,title+".mp4"))
659                         except:
660                             ""
661                         os.chmod(os.path.join(movepath,title+".mp4"),0777)
662                         recque.writeRecQue(movepath, chtxt, title, opt)
663                         os.chmod(os.path.join(movepath,title+".recq"),0777)
664                     elif os.path.exists(os.path.join(recpath,title+".mkv")):
665                         try:
666                             shutil.copy(os.path.join(recpath,title+".mkv"), os.path.join(movepath,title+".mkv"))
667                         except:
668                             ""
669                         os.chmod(os.path.join(movepath,title+".mkv"),0777)
670                         recque.writeRecQue(movepath, chtxt, title, opt)
671                         os.chmod(os.path.join(movepath,title+".recq"),0777)
672                 except Exception, inst:
673                     errtxt1="move mkv/mp4 error."
674                     errtxt2=str(type(inst))+"\n"
675                     errtxt2=errtxt2+str(inst)
676                     recdblist.Commonlogex("Error", "type_encode_que(timerec.py)", errtxt1,errtxt2+traceback.format_exc(),log_level=200)
677                 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
678             recdb.rec_reckey(typetxtfin, title, chtxt, btime, etime, opt)
679             sys.exit()
680         else:
681             recdb.del_reckey(typetxtnow, title, chtxt, btime)
682             bt = bt + datetime.timedelta(seconds=600)
683             et = et + datetime.timedelta(seconds=600)
684             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
685             etime = et.strftime("%Y-%m-%d %H:%M:%S")
686             recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
687             sys.exit()