OSDN Git Service

simplify many codes.
[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 "now-rectask"
32     print "inum:" + str(inum) + "bscsrec:" + str(status.getBSCSRecording()) + "terec:" + str(status.getTERecording()) + " epg:" + str(status.getEPGUpdating())
33
34     #if inum+status.getBSCSRecording()+status.getTERecording() < 2:
35     update = chdb.update()
36     print update
37     print "番組表更新処理"
38     if len(update) > 0:
39         pid = os.fork()
40         if pid != 0:
41             time.sleep(1)
42         else:
43             i = 0
44             for bctype in update:
45                 recnum = 0
46                 if bctype.find('te') != -1:
47                     recnum = status.getTERecording() + recdb.countRecNow_minutes_TE("10")
48                     print bctype + "//ing:" + str(status.getTERecording()) + ":task:" + str(recdb.countRecNow_minutes_TE("10"))
49                 else:
50                     recnum = status.getBSCSRecording() + recdb.countRecNow_minutes_BSCS("10")
51                     print bctype + "//ing:" + str(status.getBSCSRecording()) + ":task:" + str(recdb.countRecNow_minutes_BSCS("10"))
52                 if recnum < 2:
53                     print update
54                     rec10d.rec10db.update_status_by_bctype_chdata(bctype, "0")
55                     epgdb.updatebc(bctype)
56                     update = chdb.update()
57                     #rec10d.rec10db.update_by_bctype_chdata(bctype)
58                 i = i + 1
59                 if i > 1:
60                     break
61             sys.exit()
62     for task in tasks:
63         #print task
64         try:
65             chtxt = task['chtxt']
66         except:
67             chtxt = ""
68         try:
69             title = task['title']
70         except:
71             title = ""
72         try:
73             btime = task['btime']
74             bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
75         except:
76             btime = ""
77             bt = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
78         try:
79             etime = task['etime']
80             et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
81         except:
82             etime = ""
83             et = datetime.datetime.strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
84         try:
85             opt = task['opt']
86         except:
87             opt = ""
88         tnow = datetime.datetime.now()
89         dtt = bt-tnow
90         dt = dtt.days * 24 * 60 * 60 + dtt.seconds
91         #print title+str(dt)
92         if task["type"] == recdb.REC_RESERVE:#"res,"+chtxt+","+title+","+btime+","+etime+","+opt
93             if (dt < 58 * 60 and dt > 20 * 60):
94                 pid = os.fork()
95                 if pid != 0:#親プロセスの場合
96                     ""
97                 else:#子プロセスの場合 アップデートを行って終了
98                     bctypet = chdb.chtxtsearch(chtxt)['bctype']
99                     chdatat = rec10d.rec10db.select_by_bctype_chdata(bctypet)
100                     dt1 = datetime.datetime.strptime(chdatat[0][5], "%Y-%m-%d %H:%M:%S")-datetime.datetime.now()
101                     dt1 = dt1.days * 24 * 60 * 60 + dt1.seconds
102                     #print "oikake search:"+str(dt1)
103                     if dt1 < 60 * 60:
104                         recdata = epgdb.searchtime2(title.decode('utf-8'), btime, "5", chtxt)
105                         chtxtn = recdata[0]
106                         titlen = recdata[1]
107                         btimen = recdata[2]
108                         etimen = recdata[3]
109                         bt = datetime.datetime.strptime(btimen, "%Y-%m-%d %H:%M:%S")
110                         et = datetime.datetime.strptime(etimen, "%Y-%m-%d %H:%M:%S")
111                         btimen = bt.strftime("%Y-%m-%d %H:%M:%S")
112                         etimen = et.strftime("%Y-%m-%d %H:%M:%S")
113                         if chtxt != "":
114                             try:
115                                 recdb.del_reckey(recdb.REC_RESERVE, title, chtxt, btime)
116                                 recdb.rec_reckey(recdb.REC_FINAL_RESERVE, titlen, chtxtn, btimen, etimen, opt)
117                             except Exception, inst:
118                                 print type(inst)
119                                 print inst
120                         else:
121                             print "nothing match"
122                     else:
123                         print rec10d.rec10db.select_by_bctype_chdata(bctypet)
124                         if rec10d.rec10db.select_by_bctype_chdata(bctypet)[0][6] != "0":
125                             rec10d.rec10db.update_status_by_bctype_chdata(bctypet, "3")
126                     sys.exit()
127             elif dt <= 20 * 60:
128                 try:
129                     recdb.del_reckey(recdb.REC_RESERVE, title, chtxt, btime)
130                     recdb.rec_reckey(recdb.REC_FINAL_RESERVE, title, chtxt, btime, etime, opt)
131                 except Exception, inst:
132                     print type(inst)
133                     print inst
134         elif task["type"] == recdb.REC_KEYWORD:#"key,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt
135             deltatime = task['deltatime']
136             if dt < 90 * 60:
137                 recdata = epgdb.searchtime2(title.decode('utf-8'), btime, deltatime, chtxt)
138                 if recdata[1] != "":
139                     chtxtt = recdata[0]
140                     titlet = recdata[1]
141                     btimet = recdata[2]
142                     etimet = recdata[3]
143                     bt = datetime.datetime.strptime(btimet, "%Y-%m-%d %H:%M:%S")
144                     et = datetime.datetime.strptime(etimet, "%Y-%m-%d %H:%M:%S")
145                     btimet = bt.strftime("%Y-%m-%d %H:%M:%S")
146                     etimet = et.strftime("%Y-%m-%d %H:%M:%S")
147                     try:
148                         recdb.del_reckey(recdb.REC_KEYWORD, title, chtxt, btime)
149                         recdb.rec_reckey(recdb.REC_RESERVE, titlet, chtxtt, btimet, etimet, opt)
150                     except Exception, inst:
151                         print type(inst)
152                         print inst
153                 else:
154                     print "nothing match"
155         elif task["type"] == recdb.REC_KEYWORD_EVERY_SOME_DAYS:#"keyevery,"+chtxt+","+keyword+","+btime+","+deltatime+","+opt+","+deltaday
156             deltatime = task['deltatime']
157             deltaday = task['deltaday']
158             dd = datetime.timedelta(days=int(deltaday))
159             if dtt.days < 0:
160                 recdb.del_reckey(recdb.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
161                 bt = bt + dd
162                 et = et + dd
163                 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
164                 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
165                 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
166             elif dt < 120 * 60:
167                 recdb.keyreserv(title, chtxt, btime, etime, deltatime, opt)
168                 recdb.del_reckey(recdb.REC_KEYWORD_EVERY_SOME_DAYS, title, chtxt, btime)
169                 bt = bt + dd
170                 et = et + dd
171                 btxt = bt.strftime("%Y-%m-%d %H:%M:%S")
172                 etxt = et.strftime("%Y-%m-%d %H:%M:%S")
173                 recdb.everyreserv(title, chtxt, btxt, etxt, deltatime, opt, deltaday)
174             
175         elif task["type"] == recdb.REC_FINAL_RESERVE:#"rec,"+chtxt+","+title+","+btime+","+etime+","+opt
176             print dt
177             if dt < 6 * 60 and dt > 0:
178                 recdb.del_reckey(recdb.REC_FINAL_RESERVE, title, chtxt, btime)
179                 recdb.rec_reckey(recdb.REC_TS_RECORDING, title, chtxt, btime, etime, opt)
180                 print "録画開始"
181                 pid = os.fork()
182                 if pid != 0:#親プロセスの場合
183                     ""
184                 else:
185                     #print title
186                     tv2avi.timetv2b25(recpath + "/" + title + ".avi", chtxt, btime, etime, opt)
187                     recdb.del_reckey(recdb.REC_TS_RECORDING, title, chtxt, btime)
188                     tnow = datetime.datetime.now()
189                     bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
190                     et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
191                     dt = tnow-bt
192                     bt = tnow + datetime.timedelta(seconds=600)
193                     et = et + dt + datetime.timedelta(seconds=600)
194                     btime = bt.strftime("%Y-%m-%d %H:%M:%S")
195                     etime = et.strftime("%Y-%m-%d %H:%M:%S")
196                     recdb.rec_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime, etime, opt)
197                     sys.exit()
198         elif task["type"] == recdb.REC_TS_DECODE_QUE:
199             if dt < 10 * 60:
200                 if status.getB25Decoding() < 2:
201                     pid = os.fork()
202                     if pid != 0:#親プロセスの場合
203                         ""
204                     else:
205                         pin = recpath + "/" + title
206                         recdb.del_reckey(recdb.REC_TS_DECODE_QUE, title, chtxt, btime)
207                         recdb.rec_reckey(recdb.REC_TS_DECODING, title, chtxt, btime, etime, opt)
208                         tv2avi.b252ts(pin, chtxt, btime, etime, opt)
209                         recdb.del_reckey(recdb.REC_TS_DECODING, title, chtxt, btime)
210                         if not os.access(recpath + "/" + title + ".ts", os.F_OK):
211                             recdb.del_reckey(recdb.REC_TS_DECODING, title, chtxt, btime)
212                             recdb.rec_reckey(recdb.REC_MISS_DECODE, title, chtxt, btime, etime, opt)
213                         tnow = datetime.datetime.now()
214                         bt = datetime.datetime.strptime(btime, "%Y-%m-%d %H:%M:%S")
215                         et = datetime.datetime.strptime(etime, "%Y-%m-%d %H:%M:%S")
216                         dt = tnow-bt
217                         bt = tnow + datetime.timedelta(seconds=600)
218                         et = et + dt + datetime.timedelta(seconds=600)
219                         btime = bt.strftime("%Y-%m-%d %H:%M:%S")
220                         etime = et.strftime("%Y-%m-%d %H:%M:%S")
221                         recdb.rec_reckey(recdb.REC_ENCODE_QUE, title, chtxt, btime, etime, opt)
222                         print "b25check " + recpath + "/" + title + ".ts.b25 " + recpath + "/" + title + ".ts"
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)