OSDN Git Service

fix to read xml using dom.
[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 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+[458,916]#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     return ret
121 def searchFolder(title,path):
122     """
123     titleにマッチするフォルダを探し出す。
124     """
125     folderpath=os.listdir(path)
126     lfpath=[]
127     ngram=[]
128     for ft in folderpath:
129         fullpath=os.path.join(path.encode('utf-8'), ft)
130         if os.path.isdir(fullpath):
131             lfpath.append(fullpath)
132             ftt=os.listdir(fullpath)
133             if len(ftt)>0:
134                 for ft2 in ftt:
135                     try:
136                         folderpath.append(os.path.join(fullpath, ft2))
137                     except Exception, inst:
138                         #print type(inst)
139                         #print inst
140                         ""
141         else:
142             lfpath.append(fullpath)
143     for dirp in lfpath:
144         cmpp=""
145         appp=""
146         ntitle=title
147         if os.path.isdir(dirp):
148             cmpp=os.path.dirname(dirp)
149             appp=dirp
150         else:
151             cmpp=os.path.basename(dirp)
152             appp=os.path.dirname(dirp)
153         rT=re.compile("(.+)_(\d*)\Z")
154         tT=rT.match(title)
155         tT2=rT.match(cmpp)
156         if tT:
157             ntitle=tT.group(0)
158         if tT2:
159             cmpp=cmpp.group(0)
160         p=n_gram.trigram(ntitle,cmpp)
161         if p>0:
162             ngram.append((p,appp))
163     ngram=list(set(ngram))
164     ngram.sort()
165     ngram.reverse()
166     if len(ngram)>0:
167         #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
168         if ngram[0][0]>400:
169             return ngram[0][1]
170         else:
171             return ""
172     else:
173         return ""
174 def execMove(title,temppath,recpath):
175     srcpath=os.path.join(temppath,title+".mp4")
176     #desttitle=destName(title, temppath, recpath)
177     
178     sf=searchFolder(title, recpath)
179     if sf!="":
180         destpath=os.path.join(sf,destNameMP4(title, temppath, sf)+".mp4")
181         #os.path.join(sf,desttitle+".mkv")
182         #recdblist.printutf8(srcpath)
183         #recdblist.printutf8(destpath)
184         recdblist.printutf8("moving now..")
185         recdblist.printutf8(srcpath+" : "+destpath)
186         shutil.move(srcpath, destpath)
187         #shutil.copy(srcpath, destpath)
188         delpath=[os.path.join(temppath,title+".ts")]
189         delpath.append(os.path.join(temppath,title+".avi"))
190         delpath.append(os.path.join(temppath,title+".ts.b25"))
191         delpath.append(os.path.join(temppath,title+".ts.tsmix"))
192         delpath.append(os.path.join(temppath,title+".ts.log"))
193         delpath.append(os.path.join(temppath,title+".sa.avi"))
194         delpath.append(os.path.join(temppath,title+".sa.avi.log"))
195         delpath.append(os.path.join(temppath,title+".log"))
196         for dp in delpath:
197             try:
198                 os.remove(dp)
199                 ""
200             except:
201                 ""
202 def destName(title,temppath,recpath):
203     """
204     titleから移動先に作るべきファイル名を作り出す。
205     """
206     dstpath=os.path.join(recpath,title+".avi")
207     srcpath=os.path.join(temppath,title+".avi")
208     if os.path.exists(dstpath):
209         gmtime=time.gmtime(os.path.getmtime(srcpath))
210         times=time.strftime("%y%m%d",gmtime)
211         title=title+"_"+times
212     return title
213 def destNameMKV(title,temppath,recpath):
214     """
215     titleから移動先に作るべきファイル名を作り出す。
216     """
217     dstpath=os.path.join(recpath,title+".mkv")
218     srcpath=os.path.join(temppath,title+".mkv")
219     if os.path.exists(dstpath):
220         gmtime=time.gmtime(os.path.getmtime(srcpath))
221         times=time.strftime("%y%m%d",gmtime)
222         title=title+"_"+times
223     return title
224 def destNameMP4(title,temppath,recpath):
225     """
226     titleから移動先に作るべきファイル名を作り出す。
227     """
228     dstpath=os.path.join(recpath,title+".mp4")
229     srcpath=os.path.join(temppath,title+".mp4")
230     if os.path.exists(dstpath):
231         gmtime=time.gmtime(os.path.getmtime(srcpath))
232         times=time.strftime("%y%m%d",gmtime)
233         title=title+"_"+times
234     return title
235 def detName(path,title):
236     """
237     type A ---title#<number>
238     type B ---title#<number>subtitle
239     type C ---title subtitle
240     type D ---title(without number)
241     """
242     #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
243     #    recdblist.printutf8("typeA")
244     #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
245     #    recdblist.printutf8("typeB")
246
247 def detNameType(title):
248     """
249     type A ---title#<number>
250     type B ---title#<number>subtitle
251     type C ---title subtitle
252     type D ---title(without number)
253     """
254     recdblist.printutf8(title)
255     #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
256     rA=re.compile("(.+)#(\d*)\s*\Z")
257     tA=rA.match(title)
258     rB=re.compile("(.+)#(\d*)\s*(\D*)")
259     tB=rB.match(title)
260     if tA:
261         recdblist.printutf8("typeA")
262         recdblist.printutf8("title="+tA.group(1))
263         recdblist.printutf8("num="+tA.group(2))
264     elif tB:
265         recdblist.printutf8("typeB")
266         recdblist.printutf8("title="+tB.group(1))
267         recdblist.printutf8("num="+tB.group(2))
268         recdblist.printutf8("subtitle="+tB.group(3))
269 if __name__ == "__main__":
270     usage="usage: %prog read -h"
271     version="%prog 0.7.0beta1"
272     parser=optparse.OptionParser(usage=usage,version=version)
273     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)")
274     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
275     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
276     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
277     (opts,args)=parser.parse_args(sys.argv)
278     if opts.ltitle!="":
279         #print opts.ltitle
280         ltitle=unicode(opts.ltitle,'utf-8')
281         sf=searchFolder(ltitle, recordedpath)
282         recdblist.printutf8(os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4"))
283         #print os.path.join(sf, destNameMP4(ltitle,recpath,sf)+".mp4")
284     elif opts.auto:
285         sa=search_mp4(recpath, recordedpath)
286         for t in sa:
287             recdblist.printutf8(u"自動推測実行中")
288             sf=searchFolder(t,recordedpath)
289             if sf!="":
290                 recdblist.printutf8(u"移動先")
291                 recdblist.printutf8(t+" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4"))
292                 recdblist.printutf8(u"実行中")
293                 execMove(t, recpath, recordedpath)
294             else:
295                 recdblist.printutf8(t+" can't find matching folder.")
296     elif opts.list:
297         sa=search_mp4(recpath, recordedpath)
298         for t in sa:
299             sf=searchFolder(t,recordedpath)
300             if sf!="":
301                 recdblist.printutf8(t+u" : "+os.path.join(sf, destNameMP4(t, recpath,sf)+".mp4"))
302     elif opts.etitle!="":
303         etitle=unicode(opts.etitle,'utf-8')
304         execMove(etitle,recpath, recordedpath)
305