OSDN Git Service

fix mkv option of classify.py.
[rec10/rec10-git.git] / rec10 / trunk / src / classify.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5
6 __author__="yukikaze"
7 __date__ ="$2009/09/14 17:38:25$"
8
9 import glob
10 import n_gram
11 import os
12 import os.path
13 import time
14 import optparse
15 import sys
16 import shutil
17
18 import configreader
19 recordedpath=configreader.getpath("recorded")
20 recpath=configreader.getpath("recpath")
21 def search_b25(path):
22     """
23     録画フォルダを検索し、各QUEの実行状況を表示する
24     """
25     b25list = glob.glob(path + "/*.b25")
26     for b25f in b25list:
27         ##b25f is title.ts.b25  avi is title.avi
28         dir = os.path.split(b25f)[0]
29         title = os.path.split(b25f)[1]
30         title = title.replace(".ts.b25", "")
31         avipath = os.path.join(dir, title + ".avi")
32         mkvpath = os.path.join(dir, title + ".mkv")
33         tspath = os.path.join(dir, title + ".ts")
34         b25f = b25f.replace(".ts.b25", ".avi")
35         mode = "ts"
36         if os.path.isfile(tspath):##tsファイルが存在している
37             dtime = time.time()-os.path.getmtime(tspath)
38             dtime = int(dtime)
39             if dtime > 120:
40                 if os.path.getsize(tspath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
41                     mode = "avi"
42                 else:
43                     mode = "tsmiss"
44             else:
45                 mode = "ts"
46         if os.path.isfile(mkvpath):##tsファイルが存在している
47             dtime = time.time()-os.path.getmtime(mkvpath)
48             dtime = int(dtime)
49             if dtime > 120:
50                 if os.path.getsize(avipath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
51                     mode = "fin"
52                 else:
53                     mode = "avimiss"
54             else:
55                 mode = "avi"
56         print title + ":" + mode
57 def search_avi(temppath,recpath):
58     """
59     録画一時フォルダ内のaviファイルを検索
60     """
61     avilist = glob.glob(temppath + "/*.avi")
62     ret=[]
63     for avif in avilist:
64         ##b25f is title.ts.b25  avi is title.avi
65         dir = os.path.split(avif)[0]
66         title = os.path.split(avif)[1]
67         title = title.replace(".avi", "")
68         avipath = os.path.join(dir, title + ".avi")
69         dtime = time.time()-os.path.getmtime(avipath)
70         dtime = int(dtime)
71         if dtime > 300:
72             if veryfySize(avipath):
73                 ret.append(title)
74     return ret
75 def search_mkv(temppath,recpath):
76     """
77     録画一時フォルダ内mkvファイルを検索
78     """
79     avilist = glob.glob(temppath + "/*.mkv")
80     ret=[]
81     for avif in avilist:
82         ##b25f is title.ts.b25  avi is title.avi
83         dir = os.path.split(avif)[0]
84         title = os.path.split(avif)[1]
85         title = title.replace(".mkv", "")
86         avipath = os.path.join(dir, title + ".mkv")
87         dtime = time.time()-os.path.getmtime(avipath)
88         dtime = int(dtime)
89         if dtime > 300:
90             if veryfySize(avipath):
91                 ret.append(title)
92     return ret
93
94 def veryfySize(path):
95     #vsize=[297,497,596,1196]#SD 30m 1h 1.5h 2h
96     vsize=[295,591]
97     #vsize=vsize+[458,916]#HD 30m 1h
98     #vsize=vsize+[381,895,447]
99     ret = 0
100     for size in vsize:
101         if os.path.getsize(path)>(size-10)*1024*1024 and os.path.getsize(path)<(size+10)*1024*1024:
102             ret=1
103     return ret
104 def searchFolder(title,path):
105     """
106     titleにマッチするフォルダを探し出す。
107     """
108     folderpath=os.listdir(path)
109     lfpath=[]
110     ngram=[]
111     for ft in folderpath:
112         fullpath=os.path.join(path, ft)
113         if os.path.isdir(fullpath):
114             lfpath.append(fullpath)
115             ftt=os.listdir(fullpath)
116             if len(ftt)>0:
117                 for ft2 in ftt:
118                     folderpath.append(os.path.join(fullpath, ft2))
119         else:
120             lfpath.append(fullpath)
121     for dirp in lfpath:
122         cmpp=""
123         appp=""
124         if os.path.isdir(dirp):
125             cmpp=os.path.dirname(dirp)
126             appp=dirp
127         else:
128             cmpp=os.path.basename(dirp)
129             appp=os.path.dirname(dirp)
130         p=n_gram.trigram(title.decode("utf-8"),cmpp.decode("utf-8"))
131         if p>0:
132             ngram.append((p,appp))
133     ngram=list(set(ngram))
134     ngram.sort()
135     ngram.reverse()
136     if len(ngram)>0:
137         if ngram[0][0]>400:
138             return ngram[0][1]
139         else:
140             return ""
141     else:
142         return ""
143 def execMove(title,temppath,recpath):
144     srcpath=os.path.join(temppath,title+".mkv")
145     desttitle=destName(title, temppath, recpath)
146     sf=searchFolder(title, recpath)
147     if sf!="":
148         destpath=os.path.join(sf,desttitle+".mkv")
149         #print srcpath
150         #print destpath
151         shutil.move(srcpath, destpath)
152         #shutil.copy(srcpath, destpath)
153         delpath=[os.path.join(temppath,title+".ts")]
154         delpath.append(os.path.join(temppath,title+".ts.b25"))
155         delpath.append(os.path.join(temppath,title+".ts.tsmix"))
156         delpath.append(os.path.join(temppath,title+".ts.log"))
157         delpath.append(os.path.join(temppath,title+".sa.avi"))
158         delpath.append(os.path.join(temppath,title+".log"))
159         for dp in delpath:
160             try:
161                 os.remove(dp)
162                 ""
163             except:
164                 ""
165 def destName(title,temppath,recpath):
166     """
167     titleから移動先に作るべきファイル名を作り出す。
168     """
169     dstpath=os.path.join(recpath,title+".avi")
170     srcpath=os.path.join(temppath,title+".avi")
171     if os.path.exists(dstpath):
172         gmtime=time.gmtime(os.path.getmtime(srcpath))
173         times=time.strftime("%y%m%d",gmtime)
174         title=title+"_"+times
175     return title
176 def destNameMKV(title,temppath,recpath):
177     """
178     titleから移動先に作るべきファイル名を作り出す。
179     """
180     dstpath=os.path.join(recpath,title+".mkv")
181     srcpath=os.path.join(temppath,title+".mkv")
182     if os.path.exists(dstpath):
183         gmtime=time.gmtime(os.path.getmtime(srcpath))
184         times=time.strftime("%y%m%d",gmtime)
185         title=title+"_"+times
186     return title
187
188 def detName(path,title):
189     ""
190     
191 if __name__ == "__main__":
192     usage="usage: %prog read -h"
193     version="%prog 0.7.0beta1"
194     parser=optparse.OptionParser(usage=usage,version=version)
195     parser.add_option("-s","--search",action="store",type="string",dest="ltitle",default="",metavar="TITLE",help="test to search where the title should be contained(test for -e)")
196     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
197     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
198     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
199     (opts,args)=parser.parse_args(sys.argv)
200     if opts.ltitle!="":
201         sf=searchFolder(opts.ltitle, recordedpath)
202         print os.path.join(sf, destNameMKV(opts.ltitle,recpath,sf)+".mkv")
203     elif opts.auto:
204         sa=search_mkv(recpath, recordedpath)
205         for t in sa:
206             print "自動推測実行中"
207             sf=searchFolder(t,recordedpath)
208             if sf!="":
209                 print "移動先"
210                 print t+" : "+os.path.join(sf, destNameMKV(t, recpath,sf)+".mkv")
211                 print "実行中"
212                 execMove(t, recpath, recordedpath)
213             else:
214                 print t+" can't find matching folder."
215     elif opts.list:
216         sa=search_mkv(recpath, recordedpath)
217         for t in sa:
218             sf=searchFolder(t,recordedpath)
219             if sf!="":
220                 print t+" : "+os.path.join(sf, destNameMKV(t, recpath,sf)+".mkv")
221     elif opts.etitle!="":
222         execMove(opts.etitle,recpath, recordedpath)
223