OSDN Git Service

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