OSDN Git Service

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