OSDN Git Service

fix classify and some bug.
[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-2010 Yukikaze
5
6 import glob
7 import n_gram
8 import os
9 import os.path
10 import re
11 import time
12 import optparse
13 import sys
14 import shutil
15
16 import configreader
17 import recdblist
18 recordedpath=unicode(configreader.getpath("recorded"),'utf-8')
19 recpath=unicode(configreader.getpath("recpath"),'utf-8')
20 def search_b25(path):
21     """
22     録画フォルダを検索し、各QUEの実行状況を表示する
23     """
24     b25list = glob.glob(path + "/*.b25")
25     for b25f in b25list:
26         ##b25f is title.ts.b25  avi is title.avi
27         dir = os.path.split(b25f)[0]
28         title = os.path.split(b25f)[1]
29         title = title.replace(".ts.b25", "")
30         avipath = os.path.join(dir, title + ".avi")
31         mp4path = os.path.join(dir, title + ".mp4")
32         tspath = os.path.join(dir, title + ".ts")
33         b25f = b25f.replace(".ts.b25", ".avi")
34         mode = "ts"
35         if os.path.isfile(tspath):##tsファイルが存在している
36             dtime = time.time()-os.path.getmtime(tspath)
37             dtime = int(dtime)
38             if dtime > 120:
39                 if os.path.getsize(tspath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
40                     mode = "avi"
41                 else:
42                     mode = "tsmiss"
43             else:
44                 mode = "ts"
45         if os.path.isfile(mp4path):##tsファイルが存在している
46             dtime = time.time()-os.path.getmtime(mkvpath)
47             dtime = int(dtime)
48             if dtime > 120:
49                 if os.path.getsize(avipath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
50                     mode = "fin"
51                 else:
52                     mode = "avimiss"
53             else:
54                 mode = "avi"
55         recdblist.printutf8(title + ":" + mode)
56 def search_avi(temppath,recpath):
57     """
58     録画一時フォルダ内のaviファイルを検索
59     """
60     avilist = glob.glob(temppath + "/*.avi")
61     ret=[]
62     for avif in avilist:
63         ##b25f is title.ts.b25  avi is title.avi
64         dir = os.path.split(avif)[0]
65         title = os.path.split(avif)[1]
66         title = title.replace(".avi", "")
67         avipath = os.path.join(dir, title + ".avi")
68         dtime = time.time()-os.path.getmtime(avipath)
69         dtime = int(dtime)
70         if dtime > 300:
71             if veryfySize(avipath):
72                 ret.append(title)
73     return ret
74 def search_mkv(temppath,recpath):
75     """
76     録画一時フォルダ内mkvファイルを検索
77     """
78     avilist = glob.glob(temppath + "/*.mkv")
79     ret=[]
80     for avif in avilist:
81         ##b25f is title.ts.b25  avi is title.avi
82         dir = os.path.split(avif)[0]
83         title = os.path.split(avif)[1]
84         title = title.replace(".mkv", "")
85         avipath = os.path.join(dir, title + ".mkv")
86         dtime = time.time()-os.path.getmtime(avipath)
87         dtime = int(dtime)
88         if dtime > 300:
89             if veryfySize(avipath):
90                 ret.append(title)
91     return ret
92 def search_mp4(temppath,recpath):
93     """
94     録画一時フォルダ内mp4ファイルを検索
95     """
96     avilist = glob.glob(temppath + "/*.mp4")
97     ret=[]
98     for avif in avilist:
99         ##b25f is title.ts.b25  avi is title.avi
100         dir = os.path.split(avif)[0]
101         title = os.path.split(avif)[1]
102         title = title.replace(".mp4", "")
103         avipath = os.path.join(dir, title + ".mp4")
104         dtime = time.time()-os.path.getmtime(avipath)
105         dtime = int(dtime)
106         if dtime > 300:
107             if veryfySize(avipath):
108                 ret.append(title)
109     return ret
110 def veryfySize(path):
111     #vsize=[297,497,596,1196]#SD 30m 1h 1.5h 2h
112     vsize=[245,275,295,591,830]
113     vsize=vsize+[325,449,560,590,602,690,805,860,1014,1138,1237]
114     vsize=vsize+[261,535,540,616,740]#HD 30m 1h
115     #vsize=vsize+[381,895,447]
116     ret = 0
117     for size in vsize:
118         if os.path.getsize(path)>(size-10)*1024*1024 and os.path.getsize(path)<(size+10)*1024*1024:
119             ret=1
120     if os.path.getsize(path)>270*1024*1024:
121         ret=1
122     return ret
123 def searchFolder(title,path):
124     """
125     titleにマッチするフォルダを探し出す。
126     """
127     folderpath=os.listdir(path)
128     lfpath=[]
129     ngram=[]
130     for ft in folderpath:
131         fullpath=os.path.join(path.encode('utf-8'), ft)
132         if os.path.isdir(fullpath):
133             lfpath.append(fullpath)
134             ftt=os.listdir(fullpath)
135             if len(ftt)>0:
136                 for ft2 in ftt:
137                     try:
138                         folderpath.append(os.path.join(fullpath, ft2))
139                     except Exception, inst:
140                         #print type(inst)
141                         #print inst
142                         ""
143         else:
144             lfpath.append(fullpath)
145     for dirp in lfpath:
146         cmpp=""
147         appp=""
148         ntitle=title
149         if os.path.isdir(dirp):
150             cmpp=os.path.dirname(dirp)
151             appp=dirp
152         else:
153             cmpp=os.path.basename(dirp)
154             appp=os.path.dirname(dirp)
155         ntitle=getTitle(title)
156         #recdblist.printutf8(cmpp)
157         cmpp=getTitle(os.path.splitext(cmpp)[0])
158         #recdblist.printutf8(cmpp+"\n")
159         p=n_gram.trigram(ntitle,cmpp)
160         if p>0:
161             ngram.append((p,appp))
162     ngram=list(set(ngram))
163     ngram.sort()
164     ngram.reverse()
165     if len(ngram)>0:
166         #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
167         if ngram[0][0]>500:
168             return ngram[0][1]
169         else:
170             return ""
171     else:
172         return ""
173 def getTitle(title):
174     rT=re.compile("(.+)_(\d+)\Z")
175     tT=rT.match(title)
176     rT2=re.compile("(.+)_(.+)_(\d*)\Z")#_(aichi)_2010-02-06T01:59:00.mkv
177     tT2=rT2.match(title)
178     rT3=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+:\d+:\d+\Z")#_(aichi)_2010-02-06T01:59:00.mkv
179     tT3=rT3.match(title)
180     rT4=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+-\d+-\d+\Z")#_(aichi)_2010-02-06T01-59-00.mkv
181     tT4=rT4.match(title)
182     rT5=re.compile("(.+)_(.+)_(.+)\Z")#_(aichi)_2010-02-06T01-59-00.mkv
183     tT5=rT5.match(title)
184     ntitle=title
185     if tT :
186         ntitle=tT.group(1)
187     elif tT2:
188         ntitle=tT2.group(1)
189     elif tT3:
190         ntitle=tT3.group(1)
191     elif tT4:
192         ntitle=tT4.group(1)
193     elif tT5:
194         ntitle=tT5.group(1)
195     return ntitle
196 def execMove(title,temppath,recpath):
197     srcpath=os.path.join(temppath,title+".mkv")
198     #desttitle=destName(title, temppath, recpath)
199     
200     sf=searchFolder(title, recpath)
201     if sf!="":
202         destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
203         #os.path.join(sf,desttitle+".mkv")
204         #recdblist.printutf8(srcpath)
205         #recdblist.printutf8(destpath)
206         recdblist.printutf8("moving now..")
207         recdblist.printutf8(srcpath+" : "+destpath)
208         print srcpath
209         shutil.move(srcpath, destpath)
210         #shutil.copy(srcpath, destpath)
211         delpath=[os.path.join(temppath,title+".ts")]
212         delpath.append(os.path.join(temppath,title+".avi"))
213         delpath.append(os.path.join(temppath,title+".120.avi"))
214         delpath.append(os.path.join(temppath,title+".timecode.txt"))
215         delpath.append(os.path.join(temppath,title+".aac"))
216         delpath.append(os.path.join(temppath,title+".ts.b25"))
217         delpath.append(os.path.join(temppath,title+".ts.tsmix"))
218         delpath.append(os.path.join(temppath,title+".ts.log"))
219         delpath.append(os.path.join(temppath,title+".sa.avi"))
220         delpath.append(os.path.join(temppath,title+".sa.avi.log"))
221         delpath.append(os.path.join(temppath,title+".log"))
222         for dp in delpath:
223             try:
224                 os.remove(dp)
225                 ""
226             except:
227                 ""
228 def destName(title,temppath,recpath):
229     """
230     titleから移動先に作るべきファイル名を作り出す。
231     """
232     dstpath=os.path.join(recpath,title+".avi")
233     srcpath=os.path.join(temppath,title+".avi")
234     if os.path.exists(dstpath):
235         gmtime=time.gmtime(os.path.getmtime(srcpath))
236         times=time.strftime("%y%m%d",gmtime)
237         title=title+"_"+times
238     return title
239 def destNameMKV(title,temppath,recpath):
240     """
241     titleから移動先に作るべきファイル名を作り出す。
242     """
243     dstpath=os.path.join(recpath,title+".mkv")
244     srcpath=os.path.join(temppath,title+".mkv")
245     if os.path.exists(dstpath):
246         gmtime=time.gmtime(os.path.getmtime(srcpath))
247         times=time.strftime("%y%m%d",gmtime)
248         title=title+"_"+times
249     return title
250 def destNameMP4(title,temppath,recpath):
251     """
252     titleから移動先に作るべきファイル名を作り出す。
253     """
254     dstpath=os.path.join(recpath,title+".mp4")
255     srcpath=os.path.join(temppath,title+".mp4")
256     if os.path.exists(dstpath):
257         gmtime=time.gmtime(os.path.getmtime(srcpath))
258         times=time.strftime("%y%m%d",gmtime)
259         title=title+"_"+times
260     return title
261 def detName(path,title):
262     """
263     type A ---title#<number>
264     type B ---title#<number>subtitle
265     type C ---title subtitle
266     type D ---title(without number)
267     """
268     #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
269     #    recdblist.printutf8("typeA")
270     #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
271     #    recdblist.printutf8("typeB")
272
273 def detNameType(title):
274     """
275     type A ---title#<number>
276     type B ---title#<number>subtitle
277     type C ---title subtitle
278     type D ---title(without number)
279     """
280     recdblist.printutf8(title)
281     #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
282     rA=re.compile("(.+)#(\d*)\s*\Z")
283     tA=rA.match(title)
284     rB=re.compile("(.+)#(\d*)\s*(\D*)")
285     tB=rB.match(title)
286     if tA:
287         recdblist.printutf8("typeA")
288         recdblist.printutf8("title="+tA.group(1))
289         recdblist.printutf8("num="+tA.group(2))
290     elif tB:
291         recdblist.printutf8("typeB")
292         recdblist.printutf8("title="+tB.group(1))
293         recdblist.printutf8("num="+tB.group(2))
294         recdblist.printutf8("subtitle="+tB.group(3))
295 if __name__ == "__main__":
296     usage="usage: %prog read -h"
297     version="%prog 0.7.0beta1"
298     parser=optparse.OptionParser(usage=usage,version=version)
299     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)")
300     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
301     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
302     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
303     (opts,args)=parser.parse_args(sys.argv)
304     if opts.ltitle!="":
305         #print opts.ltitle
306         ltitle=unicode(opts.ltitle,'utf-8')
307         sf=searchFolder(ltitle, recordedpath)
308         recdblist.printutf8(os.path.join(sf, destNameMKV(ltitle,recpath,sf)+".mkv"))
309         #print os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4")
310     elif opts.auto:
311         sa=search_mkv(recpath, recordedpath)
312         for t in sa:
313             recdblist.printutf8(u"自動推測実行中")
314             sf=searchFolder(t,recordedpath)
315             if sf!="":
316                 recdblist.printutf8(u"移動先")
317                 recdblist.printutf8(t+" : "+os.path.join(sf, destNameMKV(t, recpath,sf)+".mkv"))
318                 recdblist.printutf8(u"実行中")
319                 execMove(t, recpath, recordedpath)
320             else:
321                 recdblist.printutf8(t+" can't find matching folder.")
322     elif opts.list:
323         sa=search_mkv(recpath, recordedpath)
324         for t in sa:
325             sf=searchFolder(t,recordedpath)
326             if sf!="":
327                 recdblist.printutf8(t+u" : "+os.path.join(sf, destNameMKV(t, recpath,sf)+".mkv"))
328     elif opts.etitle!="":
329         etitle=unicode(opts.etitle,'utf-8')
330         execMove(etitle,recpath, recordedpath)
331