OSDN Git Service

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