OSDN Git Service

fix some.
[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         cmpp=getTitle(cmpp)
157         p=n_gram.trigram(ntitle,cmpp)
158         if p>0:
159             ngram.append((p,appp))
160     ngram=list(set(ngram))
161     ngram.sort()
162     ngram.reverse()
163     if len(ngram)>0:
164         #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
165         if ngram[0][0]>400:
166             return ngram[0][1]
167         else:
168             return ""
169     else:
170         return ""
171 def getTitle(title):
172     rT=re.compile("(.+)_(\d*)\Z")
173     tT=rT.match(title)
174     rT2=re.compile("(.+)_(.+)_(\d*)\Z")#_(aichi)_2010-02-06T01:59:00.mkv
175     tT2=rT2.match(title)
176     rT3=re.compile("(.+)_(.+)_\d-\d-\dT\d:\d:\d\Z")#_(aichi)_2010-02-06T01:59:00.mkv
177     tT3=rT3.match(title)
178     ntitle=title
179     if tT :
180         ntitle=tT.group(0)
181     elif tT2:
182         ntitle=tT3.group(0)
183     elif tT3:
184         ntitle=tT4.group(0)
185     return ntitle
186 def execMove(title,temppath,recpath):
187     srcpath=os.path.join(temppath,title+".mkv")
188     #desttitle=destName(title, temppath, recpath)
189     
190     sf=searchFolder(title, recpath)
191     if sf!="":
192         destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
193         #os.path.join(sf,desttitle+".mkv")
194         #recdblist.printutf8(srcpath)
195         #recdblist.printutf8(destpath)
196         recdblist.printutf8("moving now..")
197         recdblist.printutf8(srcpath+" : "+destpath)
198         print srcpath
199         shutil.move(srcpath, destpath)
200         #shutil.copy(srcpath, destpath)
201         delpath=[os.path.join(temppath,title+".ts")]
202         delpath.append(os.path.join(temppath,title+".avi"))
203         delpath.append(os.path.join(temppath,title+".120.avi"))
204         delpath.append(os.path.join(temppath,title+".timecode.txt"))
205         delpath.append(os.path.join(temppath,title+".aac"))
206         delpath.append(os.path.join(temppath,title+".ts.b25"))
207         delpath.append(os.path.join(temppath,title+".ts.tsmix"))
208         delpath.append(os.path.join(temppath,title+".ts.log"))
209         delpath.append(os.path.join(temppath,title+".sa.avi"))
210         delpath.append(os.path.join(temppath,title+".sa.avi.log"))
211         delpath.append(os.path.join(temppath,title+".log"))
212         for dp in delpath:
213             try:
214                 os.remove(dp)
215                 ""
216             except:
217                 ""
218 def destName(title,temppath,recpath):
219     """
220     titleから移動先に作るべきファイル名を作り出す。
221     """
222     dstpath=os.path.join(recpath,title+".avi")
223     srcpath=os.path.join(temppath,title+".avi")
224     if os.path.exists(dstpath):
225         gmtime=time.gmtime(os.path.getmtime(srcpath))
226         times=time.strftime("%y%m%d",gmtime)
227         title=title+"_"+times
228     return title
229 def destNameMKV(title,temppath,recpath):
230     """
231     titleから移動先に作るべきファイル名を作り出す。
232     """
233     dstpath=os.path.join(recpath,title+".mkv")
234     srcpath=os.path.join(temppath,title+".mkv")
235     if os.path.exists(dstpath):
236         gmtime=time.gmtime(os.path.getmtime(srcpath))
237         times=time.strftime("%y%m%d",gmtime)
238         title=title+"_"+times
239     return title
240 def destNameMP4(title,temppath,recpath):
241     """
242     titleから移動先に作るべきファイル名を作り出す。
243     """
244     dstpath=os.path.join(recpath,title+".mp4")
245     srcpath=os.path.join(temppath,title+".mp4")
246     if os.path.exists(dstpath):
247         gmtime=time.gmtime(os.path.getmtime(srcpath))
248         times=time.strftime("%y%m%d",gmtime)
249         title=title+"_"+times
250     return title
251 def detName(path,title):
252     """
253     type A ---title#<number>
254     type B ---title#<number>subtitle
255     type C ---title subtitle
256     type D ---title(without number)
257     """
258     #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
259     #    recdblist.printutf8("typeA")
260     #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
261     #    recdblist.printutf8("typeB")
262
263 def detNameType(title):
264     """
265     type A ---title#<number>
266     type B ---title#<number>subtitle
267     type C ---title subtitle
268     type D ---title(without number)
269     """
270     recdblist.printutf8(title)
271     #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
272     rA=re.compile("(.+)#(\d*)\s*\Z")
273     tA=rA.match(title)
274     rB=re.compile("(.+)#(\d*)\s*(\D*)")
275     tB=rB.match(title)
276     if tA:
277         recdblist.printutf8("typeA")
278         recdblist.printutf8("title="+tA.group(1))
279         recdblist.printutf8("num="+tA.group(2))
280     elif tB:
281         recdblist.printutf8("typeB")
282         recdblist.printutf8("title="+tB.group(1))
283         recdblist.printutf8("num="+tB.group(2))
284         recdblist.printutf8("subtitle="+tB.group(3))
285 if __name__ == "__main__":
286     usage="usage: %prog read -h"
287     version="%prog 0.7.0beta1"
288     parser=optparse.OptionParser(usage=usage,version=version)
289     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)")
290     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
291     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
292     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
293     (opts,args)=parser.parse_args(sys.argv)
294     if opts.ltitle!="":
295         #print opts.ltitle
296         ltitle=unicode(opts.ltitle,'utf-8')
297         sf=searchFolder(ltitle, recordedpath)
298         recdblist.printutf8(os.path.join(sf, destNameMKV(ltitle,recpath,sf)+".mkv"))
299         #print os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4")
300     elif opts.auto:
301         sa=search_mkv(recpath, recordedpath)
302         for t in sa:
303             recdblist.printutf8(u"自動推測実行中")
304             sf=searchFolder(t,recordedpath)
305             if sf!="":
306                 recdblist.printutf8(u"移動先")
307                 recdblist.printutf8(t+" : "+os.path.join(sf, destNameMKV(t, recpath,sf)+".mkv"))
308                 recdblist.printutf8(u"実行中")
309                 execMove(t, recpath, recordedpath)
310             else:
311                 recdblist.printutf8(t+" can't find matching folder.")
312     elif opts.list:
313         sa=search_mkv(recpath, recordedpath)
314         for t in sa:
315             sf=searchFolder(t,recordedpath)
316             if sf!="":
317                 recdblist.printutf8(t+u" : "+os.path.join(sf, destNameMKV(t, recpath,sf)+".mkv"))
318     elif opts.etitle!="":
319         etitle=unicode(opts.etitle,'utf-8')
320         execMove(etitle,recpath, recordedpath)
321