OSDN Git Service

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