OSDN Git Service

change status logging in DB.
[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 > 1:
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                             except Exception, inst:
119                                 print type(inst)
120                                 print inst
121                         else:
122                             print "nothing match"
123                     else:
124                         print rec10d.rec10db.select_by_bctype_chdata(bctypet)
125                         if rec10d.rec10db.select_by_bctype_chdata(bctypet)[0][6] != "0":
126                             rec10d.rec10db.update_status_by_bctype_chdata(bctypet, "3")
127                     sys.exit()
128             elif dt <= 20 * 60:
129                 try:
130                     recdb.del_reckey(recdb.REC_RESERVE, title, chtxt, btime)
131                     recdb.rec_reckey(recdb.REC_FINAL_RESERVE, title, chtxt, btime, etime, opt)
132                 except Exception, inst:
133                     print type(inst)
134                     print inst
135         elif task["type"] == recdb.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
136             deltatime = task['deltatime']
137             if dt < 90 * 60:
138                 recdata = epgdb.searchtime2(title.decode('utf-8'), btime, deltatime, chtxt)
139                 if recdata[1] != "":
140                     chtxtt = recdata[0]
141                     titlet = recdata[1]
142                     btimet = recdata[2]
143                     etimet = recdata[3]
144                     bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
145                     et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
146                     btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
147                     etimet = et.strftime("%Y-%m-%d %H:%M:%S")
148                     try:
149                         recdb.del_reckey(recdb.REC_KEYWORD, title, chtxt, btime)
150                         recdb.rec_reckey(recdb.REC_RESERVE, titlet, chtxtt, btimet, etimet, opt)
151                     except Exception, inst:
152                         print type(inst)
153                         print inst
154                 else:
155                     print "nothing match"
156         elif task["type"] == recdb.REC_KEYWORD_EVERY_SOME_DAYS:#"keyevery,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt+","+deltaday
157             deltatime = task['deltatime']
158             deltaday = task['deltaday']
159             dd = datetime.timedelta(days=int(deltaday))
160             if dtt.days < 0:
161                 recdb.del_reckey(recdb.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
162                 bt = bt + dd
163                 et = et + dd
164                 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
165                 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
166                 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
167             elif dt < 120 * 60:
168                 recdb.keyreserv(title, chtxt, btime, etime, deltatime, opt)
169                 recdb.del_reckey(recdb.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
170                 bt = bt + dd
171                 et = et + dd
172                 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
173                 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
174                 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
175             
176         elif task["type"] == recdb.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
177             print dt
178             if dt < 6 * 60 and dt > 0:
179                 recdb.del_reckey(recdb.REC_FINAL_RESERVE, title, chtxt, btime)
180                 recdb.rec_reckey(recdb.REC_TS_RECORDING, title, chtxt, btime, etime, opt)
181                 print "録画開始"
182                 pid = os.fork()
183                 if pid != 0:#親プロセスの場合
184                     ""
185                 else:
186                     #print title
187                     tv2avi.timetv2b25(recpath + "/" + title + ".avi", chtxt, btime, etime, opt)
188                     recdb.del_reckey(recdb.REC_TS_RECORDING, title, chtxt, btime)
189                     tnow = datetime.datetime.now()
190                     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
191                     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
192                     dt = tnow-bt
193                     bt = tnow + datetime.timedelta(seconds=600)
194                     et = et + dt + datetime.timedelta(seconds=600)
195                     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
196                     etime = et.strftime("%Y-%m-%d %H:%M:%S")
197                     recdb.rec_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime, etime, opt)
198                     sys.exit()
199         elif task["type"] == recdb.REC_TS_DECODE_QUE:
200             if dt < 10 * 60:
201                 if status.getB25Decoding() < 2:
202                     pid = os.fork()
203                     if pid != 0:#親プロセスの場合
204                         ""
205                     else:
206                         pin = recpath + "/" + title
207                         recdb.del_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime)
208                         recdb.rec_reckey(recdb.REC_TS_DECODING, title, chtxt, btime, etime, opt)
209                         tv2avi.b252ts(pin, chtxt, btime, etime, opt)
210                         recdb.del_reckey(recdb.REC_TS_DECODING, title, chtxt, btime)
211                         if not os.access(recpath + "/" + title + ".ts", os.F_OK):
212                             recdb.del_reckey(recdb.REC_TS_DECODING, title, chtxt, btime)
213                             recdb.rec_reckey(recdb.REC_MISS_DECODE, title, chtxt, btime, etime, opt)
214                         tnow = datetime.datetime.now()
215                         bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
216                         et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
217                         dt = tnow-bt
218                         bt = tnow + datetime.timedelta(seconds=600)
219                         et = et + dt + datetime.timedelta(seconds=600)
220                         btime = bt.strftime("%Y-%m-%d %H:%M:%S")
221                         etime = et.strftime("%Y-%m-%d %H:%M:%S")
222                         recdb.rec_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime, etime, opt)
223                         print checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts")
224                         if checker.checkB25Decode(recpath + "/" + title + ".ts.b25", recpath + "/" + title + ".ts") == 1:
225                             os.remove(recpath + "/" + title + ".ts.b25")
226                         sys.exit()
227                 else:
228                     recdb.del_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime)
229                     bt = bt + datetime.timedelta(seconds=600)
230                     et = et + datetime.timedelta(seconds=600)
231                     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
232                     etime = et.strftime("%Y-%m-%d %H:%M:%S")
233                     recdb.rec_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime, etime, opt)
234         elif task["type"] == recdb.REC_ENCODE_QUE:
235             if dt < 10 * 60:
236                 if status.getEncoding() < int(configreader.getenv("enc_max")):
237                     pid = os.fork()
238                     if pid > 0:#親プロセスの場合
239                         ""
240                     else:
241                         pin = recpath + "/" + title + ".ts"
242                         if re.search("d", opt):
243                             pin = recpath + "/" + title + "sa.avi"
244                         if re.search("5", opt):
245                             pin = recpath + "/" + title + "sa.avi"
246                         pout = recpath + "/" + title + ".avi"
247                         print [pin, pout, opt]
248                         #print pin+":"+pout+":"+opt
249                         recdb.del_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime)
250                         recdb.rec_reckey(recdb.REC_ENCODE_LOCAL, title, chtxt, btime, etime, opt)
251                         tv2avi.ts2avi(pin, pout, opt)
252                         recdb.del_reckey(recdb.REC_ENCODE_LOCAL, title, chtxt, btime)
253                         recdb.rec_reckey(recdb.REC_FIN_LOCAL, title, chtxt, btime, etime, opt)
254                         sys.exit()
255                 else:
256                     recdb.del_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime)
257                     bt = bt + datetime.timedelta(seconds=600)
258                     et = et + datetime.timedelta(seconds=600)
259                     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
260                     etime = et.strftime("%Y-%m-%d %H:%M:%S")
261                     recdb.rec_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime, etime, opt)
262     sys.exit()