OSDN Git Service

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