OSDN Git Service

8c9464fe2d78decd9d940594d245ec8e6077294d
[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 Yukikaze
5 import datetime
6 import os
7 import re
8 import sys
9 import time
10
11 import chdb
12 import checker
13 import configreader
14 import epgdb
15 import rec10d
16 import recdb
17 import status
18 import tv2avi
19 import tv2mkv
20 recpath = configreader.getpath('recpath')
21 path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
22 def task():
23     """
24     数分毎に実行されるタスク処理
25     予定によって子プロセスを生成し処理する。
26     """
27     recdb.delete_old("24")
28     tasks = recdb.getnow("2")
29     #print tasks
30     inum = recdb.countRecNow_minutes("10")
31     #inum=recdb.countRecNow("1")
32     print "番組表更新処理"+ datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
33     print "inum:" + str(inum) + "bscsrec:" + str(status.getBSCSRecording()) + "terec:" + str(status.getTERecording()) 
34
35     #if inum+status.getBSCSRecording()+status.getTERecording() < 2:
36     encodenum=0
37     b25num=0
38     update = chdb.update()
39     print update
40     if len(update) > 0:
41         pid = os.fork()
42         if pid != 0:
43             time.sleep(1)
44         else:
45             i = 0
46             for bctype in update:
47                 recnum = 0
48                 if bctype.find('te') > -1:
49                     recnum = status.getTERecording() + recdb.countRecNow_minutes_TE("10")
50                     print bctype + "//ing:" + str(status.getTERecording()) + ":task:" + str(recdb.countRecNow_minutes_TE("10"))
51                 else:
52                     recnum = status.getBSCSRecording() + recdb.countRecNow_minutes_BSCS("10")
53                     print bctype + "//ing:" + str(status.getBSCSRecording()) + ":task:" + str(recdb.countRecNow_minutes_BSCS("10"))
54                 if recnum < 2:
55                     print update
56                     rec10d.rec10db.update_status_by_bctype_chdata(bctype, "0")
57                     epgdb.updatebc(bctype)
58                     update = chdb.update()
59                     i = i + 1
60                     #rec10d.rec10db.update_by_bctype_chdata(bctype)
61                 if i > 0:
62                     break
63             sys.exit()
64     for task in tasks:
65         #print task
66         try:
67             chtxt = task['chtxt']
68         except:
69             chtxt = ""
70         try:
71             title = task['title']
72         except:
73             title = ""
74         try:
75             btime = task['btime']
76             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
77         except:
78             btime = ""
79             bt = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
80         try:
81             etime = task['etime']
82             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
83         except:
84             etime = ""
85             et = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
86         try:
87             opt = task['opt']
88         except:
89             opt = ""
90         #print title
91         #print task
92         tnow = datetime.datetime.now()
93         dtt = bt-tnow
94         dt = dtt.days * 24 * 60 * 60 + dtt.seconds
95         #print title+str(dt)
96         if task["type"] == recdb.REC_RESERVE:#"res,"+chtxt+","+title+","+btime+","+etime+","+opt
97             if (dt < 58 * 60 and dt > 20 * 60):
98                 pid = os.fork()
99                 if pid != 0:#親プロセスの場合
100                     ""
101                 else:#子プロセスの場合 アップデートを行って終了
102                     bctypet = chdb.chtxtsearch(chtxt)['bctype']
103                     chdatat = rec10d.rec10db.select_by_bctype_chdata(bctypet)
104                     dt1 = datetime.datetime.strptime(chdatat[0][5], "%Y-%m-%d %H:%M:%S")-datetime.datetime.now()
105                     dt1 = dt1.days * 24 * 60 * 60 + dt1.seconds
106                     #print "oikake search:"+str(dt1)
107                     if dt1 < 60 * 60:
108                         recdata = epgdb.searchtime2(title.decode('utf-8'), btime, "5", chtxt)
109                         chtxtn = recdata[0]
110                         titlen = recdata[1]
111                         btimen = recdata[2]
112                         etimen = recdata[3]
113                         bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
114                         et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
115                         btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
116                         etimen = et.strftime("%Y-%m-%d %H:%M:%S")
117                         if chtxt != "":
118                             try:
119                                 recdb.del_reckey(recdb.REC_RESERVE, title, chtxt, btime)
120                                 recdb.rec_reckey(recdb.REC_FINAL_RESERVE, titlen, chtxtn, btimen, etimen, opt)
121                                 print "Oikake "+title+" : "+titlen+" "+btimen+" "+etimen
122                             except Exception, inst:
123                                 print type(inst)
124                                 print inst
125                         else:
126                             print "nothing match"
127                     else:
128                         #print rec10d.rec10db.select_by_bctype_chdata(bctypet)
129                         if rec10d.rec10db.select_by_bctype_chdata(bctypet)[0][6] != "0":
130                             rec10d.rec10db.update_status_by_bctype_chdata(bctypet, "3")
131                     sys.exit()
132             elif dt <= 20 * 60:
133                 try:
134                     recdb.del_reckey(recdb.REC_RESERVE, title, chtxt, btime)
135                     recdb.rec_reckey(recdb.REC_FINAL_RESERVE, title, chtxt, btime, etime, opt)
136                 except Exception, inst:
137                     print type(inst)
138                     print inst
139         elif task["type"] == recdb.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
140             deltatime = task['deltatime']
141             if dt < 90 * 60:
142                 recdata = epgdb.searchtime2(title.decode('utf-8'), btime, deltatime, chtxt)
143                 if recdata[1] != "":
144                     chtxtt = recdata[0]
145                     titlet = recdata[1]
146                     btimet = recdata[2]
147                     etimet = recdata[3]
148                     bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
149                     et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
150                     btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
151                     etimet = et.strftime("%Y-%m-%d %H:%M:%S")
152                     try:
153                         recdb.del_reckey(recdb.REC_KEYWORD, title, chtxt, btime)
154                         recdb.rec_reckey(recdb.REC_RESERVE, titlet, chtxtt, btimet, etimet, opt)
155                         print "key "+title+" : "+titlet+" "+btimet+" "+etimet
156                     except Exception, inst:
157                         print type(inst)
158                         print inst
159                 else:
160                     print "nothing match"
161         elif task["type"] == recdb.REC_KEYWORD_EVERY_SOME_DAYS:#"keyevery,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt+","+deltaday
162             deltatime = task['deltatime']
163             deltaday = task['deltaday']
164             dd = datetime.timedelta(days=int(deltaday))
165             if dtt.days < 0:
166                 recdb.del_reckey(recdb.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
167                 bt = bt + dd
168                 et = et + dd
169                 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
170                 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
171                 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
172             elif dt < 120 * 60:
173                 recdb.keyreserv(title, chtxt, btime, etime, deltatime, opt)
174                 recdb.del_reckey(recdb.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
175                 bt = bt + dd
176                 et = et + dd
177                 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
178                 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
179                 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
180             
181         elif task["type"] == recdb.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
182             print dt
183             if dt < 6 * 60 and dt > 0:
184                 recdb.del_reckey(recdb.REC_FINAL_RESERVE, title, chtxt, btime)
185                 recdb.rec_reckey(recdb.REC_TS_RECORDING, title, chtxt, btime, etime, opt)
186                 print "録画開始 "+title+" "+btime+" "+etime
187                 pid = os.fork()
188                 if pid != 0:#親プロセスの場合
189                     ""
190                 else:
191                     ttitle=title
192                     #print title
193                     testpath=[os.path.join(recpath,title+".ts.b25")]
194                     testpath.append(os.path.join(recpath,title+".ts"))
195                     testpath.append(os.path.join(recpath,title+".avi"))
196                     testpath.append(os.path.join(recpath,title+".mkv"))
197                     testpath.append(os.path.join(recpath,title+".log"))
198                     tcheck=0
199                     for ti in testpath:
200                         if os.path.exists(ti):
201                             tcheck=tcheck+1
202                     if tcheck>0:
203                         ttitle=title+"_"+datetime.datetime.now().strftime("%Y%m%d%H%M")
204                     tv2avi.timetv2b25(recpath + "/" + ttitle + ".avi", chtxt, btime, etime, opt)
205                     recdb.del_reckey(recdb.REC_TS_RECORDING, title, chtxt, btime)
206                     tnow = datetime.datetime.now()
207                     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
208                     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
209                     dt = tnow-bt
210                     bt = tnow + datetime.timedelta(seconds=600)
211                     et = et + dt + datetime.timedelta(seconds=600)
212                     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
213                     etime = et.strftime("%Y-%m-%d %H:%M:%S")
214                     recdb.rec_reckey(recdb.REC_TS_DECODE_QUE, ttitle, chtxt, btime, etime, opt)
215                     sys.exit()
216         elif task["type"] == recdb.REC_TS_DECODE_QUE:
217             if dt < 10 * 60:
218                 b25num=b25num+1
219                 pid = os.fork()
220                 if pid != 0:#親プロセスの場合
221                     ""
222                 else:
223                     if b25num>1:
224                         time.sleep(5*b25num)
225                     if status.getB25Decoding() < 2:
226                         pin = recpath + "/" + title
227                         recdb.del_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime)
228                         recdb.rec_reckey(recdb.REC_TS_DECODING, title, chtxt, btime, etime, opt)
229                         tv2avi.b252ts(pin, chtxt, btime, etime, opt)
230                         recdb.del_reckey(recdb.REC_TS_DECODING, title, chtxt, btime)
231                         if not os.access(recpath + "/" + title + ".ts", os.F_OK):
232                             recdb.del_reckey(recdb.REC_TS_DECODING, title, chtxt, btime)
233                             recdb.rec_reckey(recdb.REC_MISS_DECODE, title, chtxt, btime, etime, opt)
234                         bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
235                         et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
236                         dt = et-bt
237                         tnow = datetime.datetime.now()
238                         bt = tnow + datetime.timedelta(seconds=900)
239                         et = bt + dt
240                         btime = bt.strftime("%Y-%m-%d %H:%M:%S")
241                         etime = et.strftime("%Y-%m-%d %H:%M:%S")
242                         recdb.rec_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime, etime, opt)
243                         print checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts")
244                         #if checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts") == 1:
245                             #os.remove(recpath + "/" + title + ".ts.b25")
246                         sys.exit()
247                     else:
248                         recdb.del_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime)
249                         bt = bt + datetime.timedelta(seconds=600)
250                         et = et + datetime.timedelta(seconds=600)
251                         btime = bt.strftime("%Y-%m-%d %H:%M:%S")
252                         etime = et.strftime("%Y-%m-%d %H:%M:%S")
253                         recdb.rec_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime, etime, opt)
254                         sys.exit()
255         elif task["type"] == recdb.REC_ENCODE_QUE:
256             if dt < 10 * 60:
257                 encodenum=encodenum+1
258                 pid = os.fork()
259                 if pid > 0:#親プロセスの場合
260                     ""
261                 else:
262                     if encodenum>1:
263                         time.sleep(5*encodenum)
264                     if status.getEncoding() < int(configreader.getenv("enc_max")):
265                         print opt
266                         pin = recpath + "/" + title + ".ts"
267                         if re.search("d", opt):
268                             pin = recpath + "/" + title + ".sa.avi"
269                         if re.search("5", opt):
270                             pin = recpath + "/" + title + ".sa.avi"
271                         #pout = recpath + "/" + title + ".avi"
272                         pout = recpath + "/" + title + ".mkv"
273                         print [pin, pout, opt]
274                         print pin
275                         #print pin+":"+pout+":"+opt
276                         recdb.del_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime)
277                         recdb.rec_reckey(recdb.REC_ENCODE_LOCAL, title, chtxt, btime, etime, opt)
278                         tv2mkv.ts2mkv(pin, pout, opt)
279                         #tv2avi.ts2avi(pin, pout, opt)
280                         recdb.del_reckey(recdb.REC_ENCODE_LOCAL, title, chtxt, btime)
281                         recdb.rec_reckey(recdb.REC_FIN_LOCAL, title, chtxt, btime, etime, opt)
282                         sys.exit()
283                     else:
284                         recdb.del_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime)
285                         bt = bt + datetime.timedelta(seconds=600)
286                         et = et + datetime.timedelta(seconds=600)
287                         btime = bt.strftime("%Y-%m-%d %H:%M:%S")
288                         etime = et.strftime("%Y-%m-%d %H:%M:%S")
289                         recdb.rec_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime, etime, opt)
290                         sys.exit()
291     sys.exit()