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     for task in tasks:
105         typetxt = task["type"]
106         try:
107             chtxt = task['chtxt']
108         except:
109             chtxt = ""
110         try:
111             title = task['title']
112         except:
113             title = ""
114         try:
115             btime = task['btime']
116             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
117         except:
118             btime = ""
119             bt = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
120         try:
121             etime = task['etime']
122             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
123         except:
124             etime = ""
125             et = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
126         try:
127             opt = task['opt']
128         except:
129             opt = ""
130         tnow = datetime.datetime.now()
131         dtt = bt-tnow
132         dt = dtt.days * 24 * 60 * 60 + dtt.seconds
133         if task["type"] == recdblist.REC_RESERVE:#"res,"+chtxt+","+title+","+btime+","+etime+","+opt
134             pid = os.fork()
135             if pid != 0:#親プロセスの場合
136                 ""
137             else:#子プロセスの場合 アップデートを行って終了
138                 type_reserve(task["type"],chtxt,title,bt,et,opt)
139                 sys.exit()
140         elif task["type"] == recdblist.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
141             deltatime = task['deltatime']
142             pid = os.fork()
143             if pid != 0:#親プロセスの場合
144                 ""
145             else:#子プロセスの場合 アップデートを行って終了
146                 type_keyword(task["type"],chtxt,title, bt, et, opt, deltatime)
147                 sys.exit()
148         elif task["type"] == recdblist.REC_KEYWORD_EVERY_SOME_DAYS:#"keyevery,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt+","+deltaday
149             deltatime = task['deltatime']
150             deltaday = task['deltaday']
151             try:
152                 keyeverycounter=task['counter']
153             except:
154                 keyeverycounter=-1
155             pid = os.fork()
156             if pid != 0:#親プロセスの場合
157                 ""
158             else:#子プロセスの場合 アップデートを行って終了
159                 type_keyword_every_day(task["type"],chtxt, title, bt, et, opt, deltatime, deltaday,keyeverycounter)
160                 sys.exit()
161         elif task["type"] == recdblist.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
162             pid = os.fork()
163             if pid != 0:#親プロセスの場合
164                 ""
165             else:#子プロセスの場合 アップデートを行って終了
166                 type_final(task["type"],chtxt, title, bt, et, opt)
167                 sys.exit()
168         elif task["type"] == recdblist.REC_TS_DECODE_QUE:
169             b25num=b25num+1
170             pid = os.fork()
171             if pid != 0:#親プロセスの場合
172                 ""
173             else:#子プロセスの場合 アップデートを行って終了
174                 time.sleep(5*b25num)
175                 type_decode_que(task["type"],chtxt, title, bt, et, opt)
176                 sys.exit()
177         elif task["type"] == recdblist.REC_ENCODE_QUE:
178             encodenum=encodenum+1
179             pid = os.fork()
180             if pid != 0:#親プロセスの場合
181                 ""
182             else:#子プロセスの場合 アップデートを行って終了
183                 time.sleep(5*encodenum)
184                 type_encode_que(task["type"],chtxt, title, bt, et, opt)
185                 sys.exit()
186         elif task["type"] == recdblist.REC_AVI_TO_MKV:
187             if dt < 10 * 60:
188                 pid = os.fork()
189                 if pid > 0:#親プロセスの場合
190                     ""
191                 else:
192                     makeMP4=0
193                     try:
194                         if configreader.getenv("make_mp4")==1:
195                             makeMP4=1
196                     except:
197                         ""
198                     if re.search("m", opt):
199                         makeMP4=0
200                     if re.search("4", opt):
201                         makeMP4=1
202                     recdb.del_reckey(recdblist.REC_AVI_TO_MKV, title, chtxt, btime)
203                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
204                     if makeMP4==1:
205                         tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
206                     else:
207                         tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
208                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
209                     sys.exit()
210         elif task["type"] == recdblist.REC_AVI_TO_MP4:
211             if dt < 10 * 60:
212                 pid = os.fork()
213                 if pid > 0:#親プロセスの場合
214                     ""
215                 else:
216                     makeMP4=0
217                     try:
218                         if configreader.getenv("make_mp4")==1:
219                             makeMP4=1
220                     except:
221                         ""
222                     if re.search("m", opt):
223                         makeMP4=0
224                     if re.search("4", opt):
225                         makeMP4=1
226                     recdb.del_reckey(recdblist.REC_AVI_TO_MP4, title, chtxt, btime)
227                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
228                     if makeMP4==1:
229                         tv2mp4.ts2mp4(pin, pout, opt)
230                         tv2mp4.raw2mp4(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mp4"),opt)
231                     else:
232                         tv2mkv.raw2mkv(os.path.join(recpath,title+".264"),os.path.join(recpath,title+".mkv"),opt)
233                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
234                     sys.exit()
235         elif task["type"] == recdblist.REC_MKV_TO_MP4:
236             if dt < 10 * 60:
237                 pid = os.fork()
238                 if pid > 0:#親プロセスの場合
239                     ""
240                 else:
241                     recdb.del_reckey(recdblist.REC_MKV_TO_MP4, title, chtxt, btime)
242                     recdb.rec_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime, etime, opt)
243                     tv2mp4.mkv2mp4(os.path.join(recpath,title+".mkv"),os.path.join(recpath,title+".mp4"))
244                     recdb.del_reckey(recdblist.REC_CHANGING_CANTAINER, title, chtxt, btime)
245                     sys.exit()
246     sys.exit()
247 def search_keyword(key):
248     tnow = datetime.datetime.now()
249     nows =tnow.strftime("%Y-%m-%d %H:%M:%S")
250     for k in key:
251         recdatum = epgdb.searchtime_auto(k,nows, "144")
252         for recdata in recdatum:
253             if recdata[1] != "":
254                 chtxtt = recdata[0]
255                 titlet = recdata[1]
256                 btimet = recdata[2]
257                 etimet = recdata[3]
258                 btt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
259                 ett = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
260                 btimet = btt.strftime("%Y-%m-%d %H:%M:%S")
261                 etimet = ett.strftime("%Y-%m-%d %H:%M:%S")
262                 if status.getSettings_auto_jbk()==1:
263                     topt=status.getSettings_auto_opt()
264                     try:
265                         maxnum=0
266                         if len(ch['ch'])>2:
267                             maxnum=epgdb.count_schedule_timeline(btimet, etimet)[1]
268                             maxnum=int(configreader.getenv("bscs_max"))-maxnum
269                         else:
270                             maxnum=epgdb.count_schedule_timeline(btimet, etimet)[0]
271                             maxnum=int(configreader.getenv("te_max"))-maxnum
272                         if maxnum>0:
273                             topt=status.getSettings_auto_opt()
274                             recdb.recreserv(titlet,chtxtt, btimet, etimet,topt)
275                     except Exception, inst:
276                         recdblist.printutf8("error occures in search_keyword_auto_jbk")
277                         recdblist.printutf8(str(type(inst)))
278                         recdblist.printutf8(str(inst))
279                 else:
280                     try:
281                         recdb.add_auto_keyword(chtxtt, titlet, btimet, etimet)
282                     except Exception, inst:
283                         recdblist.printutf8("error occures in search_keyword")
284                         recdblist.printutf8(str(type(inst)))
285                         recdblist.printutf8(str(inst))
286 def type_reserve(typetxt,chtxt,title,bt,et,opt):
287     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
288     etime = et.strftime("%Y-%m-%d %H:%M:%S")
289     typetxtnow=typetxt
290     typetxtfinal=""
291     if typetxt==recdblist.REC_RESERVE:
292         typetxtfinal=recdblist.REC_FINAL_RESERVE
293     tnow = datetime.datetime.now()
294     dtt = bt-tnow
295     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
296     if (dt < 58 * 60 and dt > 30 * 60):
297         bctypet = chdb.chtxtsearch(chtxt)['bctype']
298         chdatat = rec10d.rec10db.select_by_bctype_epg_ch(bctypet)
299         dt1 = datetime.datetime.strptime(chdatat[0][5], "%Y-%m-%d %H:%M:%S")-datetime.datetime.now()
300         dt1 = dt1.days * 24 * 60 * 60 + dt1.seconds
301         if dt1 < 60 * 60:
302             recdata = epgdb.searchtime2(title, btime, "5", chtxt)
303             chtxtn = recdata[0]
304             titlen = recdata[1]
305             btimen = recdata[2]
306             etimen = recdata[3]
307             exp = recdata[4]
308             longexp = recdata[5]
309             category=recdata[6]
310             bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
311             et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
312             btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
313             etimen = et.strftime("%Y-%m-%d %H:%M:%S")
314             if chtxt != "":
315                 try:
316                     recdb.del_reckey(typetxtnow, title, chtxt, btime)
317                     recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
318                     recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
319                     auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
320                     auto_rec.add_key("ALL", titlen,exp+" "+longexp)
321                     recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
322                 except Exception, inst:
323                     recdblist.printutf8("Error happened in Oikake DB")
324                     recdblist.printutf8(str(type(inst)))
325                     recdblist.printutf8(str(inst))
326             else:
327                 recdblist.printutf8(u"追いかけ機能エラー:番組データが見付かりません。")
328         else:
329             if rec10d.rec10db.select_by_bctype_epg_ch(bctypet)[0][6] != "0":
330                 rec10d.rec10db.update_status_by_bctype_epg_ch(bctypet, "3")
331         sys.exit()
332     elif (dt <= 30 * 60 and dt > 20 * 60):
333         recdata = epgdb.searchtime2(title, btime, "5", chtxt)
334         chtxtn = recdata[0]
335         titlen = recdata[1]
336         btimen = recdata[2]
337         etimen = recdata[3]
338         exp = recdata[4]
339         longexp = recdata[5]
340         category=recdata[6]
341         bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
342         et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
343         btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
344         etimen = et.strftime("%Y-%m-%d %H:%M:%S")
345         if chtxt != "":
346             try:
347                 recdb.del_reckey(typetxtnow, title, chtxt, btime)
348                 recdb.rec_reckey(typetxtfinal, titlen, chtxtn, btimen, etimen, opt)
349                 recdb.rec_reclog(titlen, chtxtn, btimen, etimen, opt, exp, longexp, category)
350                 auto_rec.add_key(chtxt, titlen,exp+" "+longexp)
351                 auto_rec.add_key("ALL", titlen,exp+" "+longexp)
352                 recdblist.printutf8(u"追いかけ機能実行中: "+title+" : "+titlen+" "+btimen+" "+etimen)
353             except Exception, inst:
354                 recdblist.printutf8("Error happened in Oikake DB")
355                 recdblist.printutf8(str(type(inst)))
356                 recdblist.printutf8(str(inst))
357     elif dt <= 20 * 60:
358         try:
359             recdb.del_reckey(typetxtnow, title, chtxt, btime)
360             recdb.rec_reckey(typetxtfinal, title, chtxt, btime, etime, opt)
361         except Exception, inst:
362             recdblist.printutf8(str(type(inst)))
363             recdblist.printutf8(str(inst))
364 def type_final(typetxt,chtxt,title,bt,et,opt):
365     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
366     etime = et.strftime("%Y-%m-%d %H:%M:%S")
367     typetxtnow=typetxt
368     typetxting=""
369     typetxtdecque=""
370     if typetxt==recdblist.REC_FINAL_RESERVE:
371         typetxting=recdblist.REC_TS_RECORDING
372         typetxtdecque=recdblist.REC_TS_DECODE_QUE
373     tnow = datetime.datetime.now()
374     dtt = bt-tnow
375     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
376     if dt < 6 * 60 and dt > 0:
377         newtitle=title
378         recdb.del_reckey(typetxtnow, title, chtxt, btime)
379         testpath=[os.path.join(recpath,title+".ts.b25")]
380         testpath.append(os.path.join(recpath,title+".ts"))
381         testpath.append(os.path.join(recpath,title+".avi"))
382         testpath.append(os.path.join(recpath,title+".mp4"))
383         testpath.append(os.path.join(recpath,title+".log"))
384         tcheck=0
385         for ti in testpath:
386             if os.path.exists(ti):
387                 tcheck=tcheck+1
388         if re.search("N", opt) or tcheck>0:
389             iff=""
390             try:
391                 iff=u"("+configreader.getenv("iff")+u")_"
392             except:
393                 iff=""
394             newtime=bt
395             newtitle=newtitle+u"_"+iff+newtime.strftime("%Y-%m-%dT%H-%M-%S")
396         recdb.rec_reckey(typetxting, newtitle, chtxt, btime, etime, opt)
397         recdblist.printutf8(u"録画開始 "+newtitle+" "+btime+" "+etime)
398         tv2avi.timetv2b25(recpath + "/" + newtitle + ".avi", chtxt, btime, etime, opt)
399         recdb.del_reckey(typetxting, newtitle, chtxt, btime)
400         if not re.search("R", opt):
401             tnow = datetime.datetime.now()
402             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
403             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
404             dt = tnow-bt
405             bt = tnow + datetime.timedelta(seconds=600)
406             et = et + dt + datetime.timedelta(seconds=600)
407             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
408             etime = et.strftime("%Y-%m-%d %H:%M:%S")
409             recdb.rec_reckey(typetxtdecque, newtitle, chtxt, btime, etime, opt)
410         else:
411             try:
412                 shutil.copy(os.path.join(recpath,title+".ts.b25"), os.path.join(movepath,title+".ts.b25"))
413             except:
414                 ""
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     print dt
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.printutf8(u"key "+title+u" : "+titlet+u" "+btimet+u" "+etimet)
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                 cstch=-1
525                 try:
526                     cstch=int(chdb.chtxtsearch(chtxt)['csch'])
527                 except:
528                     cstch=-1
529                 if cstch>-1:
530                     ch=checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts",cstch)
531                 #print ch
532                 if ch == 1:
533                     os.remove(recpath + "/" + title + ".ts.b25")
534             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
535             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
536             dt = et-bt
537             if not re.search("D", opt):
538                 tnow = datetime.datetime.now()
539                 bt = tnow + datetime.timedelta(seconds=600)
540                 et = bt + dt
541                 btime = bt.strftime("%Y-%m-%d %H:%M:%S")
542                 etime = et.strftime("%Y-%m-%d %H:%M:%S")
543                 recdb.rec_reckey(typetxtencque, title, chtxt, btime, etime, opt)
544             else:
545                 try:
546                     shutil.copy(os.path.join(recpath,title+".ts"), os.path.join(movepath,title+".ts"))
547                 except Exception, inst:
548                     errtxt="Error happened in type_decode_que in timerec.\n"
549                     errtxt=errtxt+"move ts error.\n"
550                     errtxt=errtxt+str(type(inst))+"\n"
551                     errtxt=errtxt+str(inst)
552                     recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Decode) error")
553                 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
554         else:
555             recdb.del_reckey(typetxtnow, title, chtxt, btime)
556             bt = bt + datetime.timedelta(seconds=600)
557             et = et + datetime.timedelta(seconds=600)
558             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
559             etime = et.strftime("%Y-%m-%d %H:%M:%S")
560             recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
561     sys.exit()
562
563 def type_encode_que(typetxt,chtxt,title,bt,et,opt):
564     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
565     etime = et.strftime("%Y-%m-%d %H:%M:%S")
566     tnow = datetime.datetime.now()
567     dtt = bt-tnow
568     dt = dtt.days * 24 * 60 * 60 + dtt.seconds
569     typetxtnow=typetxt
570     typetxting=""
571     typetxtfin=""
572     if typetxt==recdblist.REC_ENCODE_QUE:
573         typetxting=recdblist.REC_ENCODE_LOCAL
574         typetxtfin=recdblist.REC_FIN_LOCAL
575     if dt < 10 * 60:
576         if status.getEncoding() < int(configreader.getenv("enc_max")):
577             recdb.del_reckey(typetxtnow, title, chtxt, btime)
578             recdb.rec_reckey(typetxting, title, chtxt, btime, etime, opt)
579             recdblist.printutf8(opt)
580             pin = recpath + "/" + title + ".ts"
581             if re.search("d", opt):
582                 pin = recpath + "/" + title + ".m2v"
583                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
584                     paac2 = recpath + "/" + title + "_2.aac"
585                     pmp32 = recpath + "/" + title + "_2.mp3"
586                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
587                         tv2avi.dualaudio2sep(os.path.join(recpath, title+".ts"),"167")
588                 time.sleep(3)
589             elif re.search("5", opt):
590                 pin = recpath + "/" + title + ".m2v"
591                 if not os.path.exists(pin) or os.path.getsize(pin)<100*1000:
592                     paac2 = recpath + "/" + title + "_2.aac"
593                     pmp32 = recpath + "/" + title + "_2.mp3"
594                     if not os.path.exists(paac2) and not os.path.exists(pmp32):
595                         tv2avi.pentaaudio2sep(os.path.join(recpath, title+".ts"))
596                 time.sleep(3)
597             makeMP4=0
598             try:
599                 if configreader.getenv("make_mp4")=="1":
600                     makeMP4=1
601             except:
602                 ""
603             if re.search("m", opt):
604                 makeMP4=0
605             if re.search("4", opt):
606                 makeMP4=1
607             recdblist.printutf8(pin)
608             if not re.search("0", opt):
609                 if makeMP4==1:
610                     pout = recpath + "/" + title + ".mp4"
611                     tv2mp4.ts2mp4(pin, pout, opt)
612                 else:
613                     pout = recpath + "/" + title + ".mkv"
614                     tv2mkv.ts2mkv(pin, pout, opt)
615                 if re.search("8", opt) or re.search("9", opt):
616                     optt=opt
617                     optt=optt.replace("8","MW8")
618                     optt=optt.replace("9","MW9")
619                     poutt = recpath + "/" + "m_"+title + ".mp4"
620                     if re.search("d", opt) or re.search("5", opt):
621                         if os.path.exists(os.path.join(recpath, "m_"+title+".m2v")):
622                             shutil.move(os.path.join(recpath, title+".m2v"),os.path.join(recpath, "m_"+title+".m2v"))
623                         if os.path.exists(os.path.join(recpath, "m_"+title+"_1.aac")):
624                             shutil.move(os.path.join(recpath, title+"_1.aac"),os.path.join(recpath, "m_"+title+"_1.aac"))
625                         if os.path.exists(os.path.join(recpath, "m_"+title+"_1.mp3")):
626                             shutil.move(os.path.join(recpath, title+"_1.mp3"),os.path.join(recpath, "m_"+title+"_1.mp3"))
627                         if os.path.exists(os.path.join(recpath, "m_"+title+"_2.aac")):
628                             shutil.move(os.path.join(recpath, title+"_2.aac"),os.path.join(recpath, "m_"+title+"_2.aac"))
629                         if os.path.exists(os.path.join(recpath, "m_"+title+"_2.mp3")):
630                             shutil.move(os.path.join(recpath, title+"_2.mp3"),os.path.join(recpath, "m_"+title+"_2.mp3"))
631                     shutil.move(os.path.join(recpath, title+".ts"),os.path.join(recpath, "m_"+title+".ts"))
632                     tv2mp4.ts2mp4(os.path.join(recpath, "m_"+title+".ts"), poutt, optt)
633                     shutil.move(os.path.join(recpath, "m_"+title+".ts"),os.path.join(recpath, title+".ts"))
634             else:
635                 optt=opt
636                 optt=optt.replace("8","MW8")
637                 optt=optt.replace("9","MW9")
638                 poutt = recpath + "/" + "m_"+title + ".mp4"
639                 shutil.move(os.path.join(recpath, title+".ts"),os.path.join(recpath, "m_"+title+".ts"))
640                 if re.search("d", opt) or re.search("5", opt):
641                     if os.path.exists(os.path.join(recpath, "m_"+title+".m2v")):
642                         shutil.move(os.path.join(recpath, title+".m2v"),os.path.join(recpath, "m_"+title+".m2v"))
643                     if os.path.exists(os.path.join(recpath, "m_"+title+"_1.aac")):
644                         shutil.move(os.path.join(recpath, title+"_1.aac"),os.path.join(recpath, "m_"+title+"_1.aac"))
645                     if os.path.exists(os.path.join(recpath, "m_"+title+"_1.mp3")):
646                         shutil.move(os.path.join(recpath, title+"_1.mp3"),os.path.join(recpath, "m_"+title+"_1.mp3"))
647                     if os.path.exists(os.path.join(recpath, "m_"+title+"_2.aac")):
648                         shutil.move(os.path.join(recpath, title+"_2.aac"),os.path.join(recpath, "m_"+title+"_2.aac"))
649                     if os.path.exists(os.path.join(recpath, "m_"+title+"_2.mp3")):
650                         shutil.move(os.path.join(recpath, title+"_2.mp3"),os.path.join(recpath, "m_"+title+"_2.mp3"))
651                 tv2mp4.ts2mp4(os.path.join(recpath, "m_"+title+".ts"), poutt, optt)
652                 shutil.move(os.path.join(recpath, "m_"+title+".ts"),os.path.join(recpath, title+".ts"))
653             recdb.del_reckey(typetxting, title, chtxt, btime)
654             time.sleep(10)
655             if re.search("E", opt):
656                 try:
657                     if os.path.exists(os.path.join(recpath,title+".mp4")):
658                         shutil.copy(os.path.join(recpath,title+".mp4"), os.path.join(movepath,title+".mp4"))
659                     elif os.path.exists(os.path.join(recpath,title+".mkv")):
660                         shutil.copy(os.path.join(recpath,title+".mkv"), os.path.join(movepath,title+".mkv"))
661                 except Exception, inst:
662                     errtxt="Error happened in type_encode_que in timerec.\n"
663                     errtxt=errtxt+"move mkv/mp4 error.\n"
664                     errtxt=errtxt+str(type(inst))+"\n"
665                     errtxt=errtxt+str(inst)
666                     recdblist.addlog(os.path.join(recpath,title+".ts"), errtxt, "timerec-move(Encode) error")
667                 recdb.rec_reckey(recdblist.REC_MOVE_END, title, chtxt, btime, etime, opt)
668             recdb.rec_reckey(typetxtfin, title, chtxt, btime, etime, opt)
669             sys.exit()
670         else:
671             recdb.del_reckey(typetxtnow, title, chtxt, btime)
672             bt = bt + datetime.timedelta(seconds=600)
673             et = et + datetime.timedelta(seconds=600)
674             btime = bt.strftime("%Y-%m-%d %H:%M:%S")
675             etime = et.strftime("%Y-%m-%d %H:%M:%S")
676             recdb.rec_reckey(typetxtnow, title, chtxt, btime, etime, opt)
677             sys.exit()