OSDN Git Service

change db mode.
[rec10/rec10-git.git] / rec10 / trunk / src / auto_process.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5 import os
6 import glob
7 import time
8 import datetime
9
10 import chdb
11 import status
12 import recdblist
13 import rec10d
14 import os.path
15 import tv2mp4
16 import epgdb
17 def Update_to_MP4(path):
18     """
19     録画一時フォルダ内mp4ファイルを検索
20     """
21     avilist = glob.glob(path + "/*.avi")
22     mkvlist = glob.glob(path+"/*.mkv")
23     avilist=avilist+mkvlist
24     #recdblist.printutf8(avilist)
25     for avif in avilist:
26         if avif.rfind("sa.avi")==-1:
27             ##b25f is title.ts.b25  avi is title.avi
28             dir = os.path.split(avif)[0]
29             title = os.path.split(avif)[1]
30             title = title.replace(".avi", "")
31             title = title.replace(".mkv", "")
32             avipath = os.path.join(dir, title + ".avi")
33             mkvpath = os.path.join(dir, title + ".mkv")
34             mp4path = os.path.join(dir, title + ".mp4")
35             if not os.path.exists(mp4path):
36                 if os.path.exists(avipath):
37                     avidtime = int(time.time()-os.path.getmtime(avipath))
38                     if avidtime>300:
39                         if os.path.exists(mkvpath):
40                             mkvdtime = int(time.time()-os.path.getmtime(mkvpath))
41                             if mkvdtime>300:
42                                 recdblist.printutf8(mkvpath+":"+mp4path)
43                                 tv2mp4.mkv2mp4(mkvpath,mp4path)
44                             else:
45                                 recdblist.printutf8(avipath+":"+mp4path)
46                                 tv2mp4.avi2mp4(avipath,mp4path)
47                         else:
48                             recdblist.printutf8(avipath+":"+mp4path)
49                             tv2mp4.avi2mp4(avipath,mp4path)
50                 else:
51                     if not os.path.exists(avipath):
52                         if os.path.exists(mkvpath):
53                             mkvdtime = int(time.time()-os.path.getmtime(mkvpath))
54                             if mkvdtime>300:
55                                 recdblist.printutf8(mkvpath+":"+mp4path)
56                                 tv2mp4.mkv2mp4(mkvpath,mp4path)
57 def auto_check(path):
58     avilist = glob.glob(path + "/*.avi")
59     mkvlist = glob.glob(path+"/*.mkv")
60     mp4list = glob.glob(path+"/*.mp4")
61     tslist = glob.glob(path+"/*.ts")
62     b25list = glob.glob(path+"/*.ts.b25")
63     filelist=avilist+mkvlist+tslist+b25list+mp4list
64     add=[]
65     tbtime=datetime.datetime.now()+datetime.timedelta(seconds=60)
66     tbtime2=datetime.datetime.now()+datetime.timedelta(seconds=1200)
67     for fn in filelist:
68         if fn.rfind("sa.avi")==-1 and fn.rfind(".120.avi")==-1 and os.path.getsize(fn)>1*1000*1000:
69             dir = os.path.split(fn)[0]
70             file = os.path.split(fn)[1]
71             title = os.path.splitext(file)[0]
72             title = unicode(title,'utf-8')
73             
74             #print dbs
75             ext = os.path.splitext(file)[1]
76             if ext == ".b25":
77                 title=title.replace(".ts","")
78             dbs=rec10d.rec10db.select_by_name_time_timeline(title,tbtime,tbtime2)
79             if len(dbs)==0:
80                 proc=check_process(dir, title)
81                 chtxtt=rec10d.rec10db.select_chtxt_by_title_timeline_log(title)
82                 nchtxt=""
83                 if chtxtt!=None:
84                     nchtxt=chtxtt
85                 if proc=="b25":
86                     """chtxtt=rec10d.rec10db.select_chtxt_by_title_timeline_log(title)
87                     nchtxt=""
88                     if chtxtt!=None:
89                         nchtxt=chtxtt"""
90                     add.append([recdblist.REC_AUTO_SUGGEST_DECODE,title,nchtxt])
91                 elif proc=="ts":
92                     add.append([recdblist.REC_AUTO_SUGGEST_ENCODE,title,nchtxt])
93                 elif proc =="264":
94                     add.append([recdblist.REC_AUTO_SUGGEST_AVI2FP,title,nchtxt])
95                 #elif proc =="mp4":
96                 #    add.append([recdblist.REC_AUTO_SUGGEST_AP2FP,title,nchtxt])
97     #print add
98     if len(add)>0:
99         rec10d.rec10db.new_auto_proc()
100         for a in add:
101             rec10d.rec10db.add_auto_proc(a[0],a[1],a[2])
102         time.sleep(1)
103 """
104     処理がどの段階まで言ったのかを調査し返す。
105     return
106     recording
107     b25
108     b25decoding
109     tssplitting
110     ts
111     encoding
112     avi
113     mp4making
114     mp4
115 """
116 def check_process(path,title):
117     path1 = os.path.join(path,title+".mkv")
118     if os.path.exists(path1):
119         if int(time.time()-os.path.getmtime(path1))>300:
120             return "mkv"
121         else:
122             return "mkvmaking"
123     elif os.path.exists(os.path.join(path,title+".mp4")):
124         if int(time.time()-os.path.getmtime(os.path.join(path,title+".mp4")))>300:
125             return "mp4"
126         else:
127             return "mp4making"
128     elif os.path.exists(os.path.join(path,title+".264")):
129         if int(time.time()-os.path.getmtime(os.path.join(path,title+".264")))>300:
130             return "264"
131         else:
132             return "encoding"
133     elif os.path.exists(os.path.join(path,title+".ts.log")):
134         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts")))<300:
135             return "encoding"
136         else:
137             return "ts"
138     elif os.path.exists(os.path.join(path,title+".ts")):
139         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts")))>300:
140             return "ts"
141         else:
142             return "tssplitting"
143     elif os.path.exists(os.path.join(path,title+".sa.avi")):
144         if int(time.time()-os.path.getmtime(os.path.join(path,title+".sa.avi")))>300:
145             return "ts"
146         else:
147             return "tssplitting"
148     elif os.path.exists(os.path.join(path,title+".ts.b25")):
149         if int(time.time()-os.path.getmtime(os.path.join(path,title+".ts.b25")))>300:
150             return "b25"
151         else:
152             return "recording"
153 def update_timeline_epg(btime,etime):
154     update_timeline_epg_schedule(btime,etime)
155     update_timeline_dup(btime,etime)
156     
157 def update_timeline_epg_schedule(btime,etime):
158     dbl=rec10d.rec10db.select_bytime_all_timeline(btime, etime)
159     print dbl
160     for db in dbl:
161         ret=[]
162         #[chtxtt, title, btime, etime,exp,longexp,category]
163         if db['type']==recdblist.REC_KEYWORD or db['type']==recdblist.REC_KEYWORD_EVERY_SOME_DAYS:
164             ret=epgdb.searchtime2(db['title'], db['btime'], db['deltatime'], db['chtxt'])
165             rec10d.rec10db.update_epg_timeline(db['type'], db['chtxt'], db['title'], db['btime'], ret[2],ret[3],ret[1])
166             if not (db['btime'] == ret[2] and  db['etime']==ret[3]):
167                 rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
168         elif db['type']==recdblist.REC_RESERVE or db['type']==recdblist.REC_FINAL_RESERVE :
169             ret=epgdb.searchtime2(db['title'], db['btime'],"5", db['chtxt'])
170             rec10d.rec10db.update_epg_timeline(db['type'], db['chtxt'], db['title'], db['btime'], ret[2],ret[3],ret[1])
171             if not (db['btime'] == ret[2] and  db['etime']==ret[3]):
172                 rec10d.rec10db.update_status_change_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
173 def update_timeline_dup(epgbtime,epgetime):
174     dbl=rec10d.rec10db.select_byepgtime_all_timeline(epgbtime, epgetime)
175     print dbl
176     for db in dbl:
177         if db['type']==recdblist.REC_KEYWORD or db['type']==recdblist.REC_KEYWORD_EVERY_SOME_DAYS:
178             dbn=epgdb.count_epgschedule(db['epgbtime'], db['epgetime'])
179             bctypet=chdb.chtxtsearch(db['chtxt'])['bctype']
180             if bctypet.find("cs") > -1 or bctypet.find("bs") > -1 :
181                 if dbn[1]>status.getRecordingMax()[1]:
182                     rec10d.rec10db.update_status_dup_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
183                 else:
184                     rec10d.rec10db.update_status_dup_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "0")
185             else:
186                 if dbn[0]>status.getRecordingMax()[0]:
187                     rec10d.rec10db.update_status_dup_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "1")
188                 else:
189                     rec10d.rec10db.update_status_dup_timeline(db['type'], db['chtxt'], db['title'], db['btime'], "0")