OSDN Git Service

fix miss
[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 import auto_move
19 recordedpath=unicode(configreader.getpath("recorded"),'utf-8')
20 recpath=unicode(configreader.getpath("recpath"),'utf-8')
21 def search_b25(path):
22     """
23     録画フォルダを検索し、各QUEの実行状況を表示する
24     """
25     b25list = glob.glob(path + "/*.b25")
26     for b25f in b25list:
27         ##b25f is title.ts.b25  avi is title.avi
28         dir = os.path.split(b25f)[0]
29         title = os.path.split(b25f)[1]
30         title = title.replace(".ts.b25", "")
31         avipath = os.path.join(dir, title + ".avi")
32         mp4path = os.path.join(dir, title + ".mp4")
33         tspath = os.path.join(dir, title + ".ts")
34         b25f = b25f.replace(".ts.b25", ".avi")
35         mode = "ts"
36         if os.path.isfile(tspath):##tsファイルが存在している
37             dtime = time.time()-os.path.getmtime(tspath)
38             dtime = int(dtime)
39             if dtime > 120:
40                 if os.path.getsize(tspath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
41                     mode = "avi"
42                 else:
43                     mode = "tsmiss"
44             else:
45                 mode = "ts"
46         if os.path.isfile(mp4path):##tsファイルが存在している
47             dtime = time.time()-os.path.getmtime(mkvpath)
48             dtime = int(dtime)
49             if dtime > 120:
50                 if os.path.getsize(avipath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
51                     mode = "fin"
52                 else:
53                     mode = "avimiss"
54             else:
55                 mode = "avi"
56         recdblist.printutf8(title + ":" + mode)
57 def search_avi(temppath,recpath):
58     """
59     録画一時フォルダ内のaviファイルを検索
60     """
61     avilist = glob.glob(temppath + "/*.avi")
62     ret=[]
63     for avif in avilist:
64         ##b25f is title.ts.b25  avi is title.avi
65         dir = os.path.split(avif)[0]
66         title = os.path.split(avif)[1]
67         title = title.replace(".avi", "")
68         avipath = os.path.join(dir, title + ".avi")
69         dtime = time.time()-os.path.getmtime(avipath)
70         dtime = int(dtime)
71         if dtime > 300:
72             if veryfySize(avipath):
73                 ret.append(title)
74     return ret
75 def search_mkv(temppath,recpath):
76     """
77     録画一時フォルダ内mkvファイルを検索
78     """
79     avilist = glob.glob(temppath + "/*.mkv")
80     ret=[]
81     for avif in avilist:
82         ##b25f is title.ts.b25  avi is title.avi
83         dir = os.path.split(avif)[0]
84         title = os.path.split(avif)[1]
85         title = title.replace(".mkv", "")
86         avipath = os.path.join(dir, title + ".mkv")
87         dtime = time.time()-os.path.getmtime(avipath)
88         dtime = int(dtime)
89         if dtime > 300:
90             if veryfySize(avipath):
91                 ret.append(title)
92     return ret
93 def search_mp4(temppath,recpath):
94     """
95     録画一時フォルダ内mp4ファイルを検索
96     """
97     avilist = glob.glob(temppath + "/*.mp4")
98     ret=[]
99     for avif in avilist:
100         ##b25f is title.ts.b25  avi is title.avi
101         dir = os.path.split(avif)[0]
102         title = os.path.split(avif)[1]
103         title = title.replace(".mp4", "")
104         avipath = os.path.join(dir, title + ".mp4")
105         dtime = time.time()-os.path.getmtime(avipath)
106         dtime = int(dtime)
107         if dtime > 300:
108             if veryfySize(avipath):
109                 ret.append(title)
110     return ret
111 def veryfySize(path):
112     #vsize=[297,497,596,1196]#SD 30m 1h 1.5h 2h
113     vsize=[245,275,295,591,830]
114     vsize=vsize+[325,449,560,590,602,690,805,860,1014,1138,1237]
115     vsize=vsize+[261,535,540,616,740]#HD 30m 1h
116     #vsize=vsize+[381,895,447]
117     ret = 0
118     for size in vsize:
119         if os.path.getsize(path)>(size-10)*1024*1024 and os.path.getsize(path)<(size+10)*1024*1024:
120             ret=1
121     if os.path.getsize(path)>270*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.encode('utf-8'), 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                     try:
139                         folderpath.append(os.path.join(fullpath, ft2))
140                     except Exception, inst:
141                         #print type(inst)
142                         #print inst
143                         ""
144         else:
145             lfpath.append(fullpath)
146     for dirp in lfpath:
147         cmpp=""
148         appp=""
149         ntitle=title
150         if os.path.isdir(dirp):
151             cmpp=os.path.dirname(dirp)
152             appp=dirp
153         else:
154             cmpp=os.path.basename(dirp)
155             appp=os.path.dirname(dirp)
156         ntitle=getTitle(title)
157         #recdblist.printutf8(cmpp)
158         cmpp=getTitle(os.path.splitext(cmpp)[0])
159         #recdblist.printutf8(cmpp+"\n")
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]>500:
169             return ngram[0][1]
170         else:
171             return ""
172     else:
173         return ""
174 def getTitle(title):
175     rT=re.compile("(.+)_(\d+)\Z")
176     tT=rT.match(title)
177     rT2=re.compile("(.+)_(.+)_(\d*)\Z")#_(aichi)_2010-02-06T01:59:00.mkv
178     tT2=rT2.match(title)
179     rT3=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+:\d+:\d+\Z")#_(aichi)_2010-02-06T01:59:00.mkv
180     tT3=rT3.match(title)
181     rT4=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+-\d+-\d+\Z")#_(aichi)_2010-02-06T01-59-00.mkv
182     tT4=rT4.match(title)
183     rT5=re.compile("(.+)_(.+)_(.+)\Z")#_(aichi)_2010-02-06T01-59-00.mkv
184     tT5=rT5.match(title)
185     ntitle=title
186     if tT :
187         ntitle=tT.group(1)
188     elif tT2:
189         ntitle=tT2.group(1)
190     elif tT3:
191         ntitle=tT3.group(1)
192     elif tT4:
193         ntitle=tT4.group(1)
194     elif tT5:
195         ntitle=tT5.group(1)
196     return ntitle
197 def execMove(title,temppath,recpath):
198     srcpath=os.path.join(temppath,title+".mkv")
199     #desttitle=destName(title, temppath, recpath)
200     
201     sf=searchFolder(title, recpath)
202     if sf!="":
203         destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
204         #os.path.join(sf,desttitle+".mkv")
205         #recdblist.printutf8(srcpath)
206         #recdblist.printutf8(destpath)
207         recdblist.printutf8("moving now..")
208         recdblist.printutf8(srcpath+" : "+destpath)
209         print srcpath
210         shutil.move(srcpath, destpath)
211         #shutil.copy(srcpath, destpath)
212         delpath=[os.path.join(temppath,title+".ts")]
213         delpath.append(os.path.join(temppath,title+".avi"))
214         delpath.append(os.path.join(temppath,title+".120.avi"))
215         delpath.append(os.path.join(temppath,title+".timecode.txt"))
216         delpath.append(os.path.join(temppath,title+".aac"))
217         delpath.append(os.path.join(temppath,title+".ts.b25"))
218         delpath.append(os.path.join(temppath,title+".ts.tsmix"))
219         delpath.append(os.path.join(temppath,title+".ts.log"))
220         delpath.append(os.path.join(temppath,title+".sa.avi"))
221         delpath.append(os.path.join(temppath,title+".sa.avi.log"))
222         delpath.append(os.path.join(temppath,title+".log"))
223         for dp in delpath:
224             try:
225                 os.remove(dp)
226                 ""
227             except:
228                 ""
229 def execMove_MKV(title,temppath,recpath):
230     srcpath=os.path.join(temppath,title+".mkv")
231     #desttitle=destName(title, temppath, recpath)
232
233     sf=searchFolder(title, recpath)
234     if sf!="":
235         destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+".mkv")
236         #os.path.join(sf,desttitle+".mkv")
237         #recdblist.printutf8(srcpath)
238         #recdblist.printutf8(destpath)
239         recdblist.printutf8("moving now..")
240         recdblist.printutf8(srcpath+" : "+destpath)
241         print srcpath
242         shutil.move(srcpath, destpath)
243         #shutil.copy(srcpath, destpath)
244         delpath=[os.path.join(temppath,title+".ts")]
245         delpath.append(os.path.join(temppath,title+".avi"))
246         delpath.append(os.path.join(temppath,title+".120.avi"))
247         delpath.append(os.path.join(temppath,title+".timecode.txt"))
248         delpath.append(os.path.join(temppath,title+".aac"))
249         delpath.append(os.path.join(temppath,title+".ts.b25"))
250         delpath.append(os.path.join(temppath,title+".ts.tsmix"))
251         delpath.append(os.path.join(temppath,title+".ts.log"))
252         delpath.append(os.path.join(temppath,title+".sa.avi"))
253         delpath.append(os.path.join(temppath,title+".sa.avi.log"))
254         delpath.append(os.path.join(temppath,title+".log"))
255         for dp in delpath:
256             try:
257                 os.remove(dp)
258                 ""
259             except:
260                 ""
261 def execMove_MP4(title,temppath,recpath):
262     srcpath=os.path.join(temppath,title+".mp4")
263     #desttitle=destName(title, temppath, recpath)
264
265     sf=searchFolder(title, recpath)
266     if sf!="":
267         destpath=os.path.join(sf,destNameMP4(title, temppath, sf)+".mp4")
268
269         #os.path.join(sf,desttitle+".mkv")
270         #recdblist.printutf8(srcpath)
271         #recdblist.printutf8(destpath)
272         recdblist.printutf8("moving now..")
273         recdblist.printutf8(srcpath+" : "+destpath)
274         print srcpath
275         shutil.move(srcpath, destpath)
276         #shutil.copy(srcpath, destpath)
277         delpath=[os.path.join(temppath,title+".ts")]
278         delpath.append(os.path.join(temppath,title+".avi"))
279         delpath.append(os.path.join(temppath,title+".120.avi"))
280         delpath.append(os.path.join(temppath,title+".timecode.txt"))
281         delpath.append(os.path.join(temppath,title+".aac"))
282         delpath.append(os.path.join(temppath,title+".ts.b25"))
283         delpath.append(os.path.join(temppath,title+".ts.tsmix"))
284         delpath.append(os.path.join(temppath,title+".ts.log"))
285         delpath.append(os.path.join(temppath,title+".sa.avi"))
286         delpath.append(os.path.join(temppath,title+".sa.avi.log"))
287         delpath.append(os.path.join(temppath,title+".log"))
288         for dp in delpath:
289             try:
290                 os.remove(dp)
291                 ""
292             except:
293                 ""
294 def destName(title,temppath,recpath):
295     """
296     titleから移動先に作るべきファイル名を作り出す。
297     """
298     dstpath=os.path.join(recpath,title+".avi")
299     srcpath=os.path.join(temppath,title+".avi")
300     if os.path.exists(dstpath):
301         gmtime=time.gmtime(os.path.getmtime(srcpath))
302         times=time.strftime("%y%m%d",gmtime)
303         title=title+"_"+times
304     return title
305 def destNameMKV(title,temppath,recpath):
306     """
307     titleから移動先に作るべきファイル名を作り出す。
308     """
309     dstpath=os.path.join(recpath,title+".mkv")
310     srcpath=os.path.join(temppath,title+".mkv")
311     if os.path.exists(dstpath):
312         gmtime=time.gmtime(os.path.getmtime(srcpath))
313         times=time.strftime("%y%m%d",gmtime)
314         title=title+"_"+times
315     return title
316 def destNameMP4(title,temppath,recpath):
317     """
318     titleから移動先に作るべきファイル名を作り出す。
319     """
320     dstpath=os.path.join(recpath,title+".mp4")
321     srcpath=os.path.join(temppath,title+".mp4")
322     if os.path.exists(dstpath):
323         gmtime=time.gmtime(os.path.getmtime(srcpath))
324         times=time.strftime("%y%m%d",gmtime)
325         title=title+"_"+times
326     return title
327 def detName(path,title):
328     """
329     type A ---title#<number>
330     type B ---title#<number>subtitle
331     type C ---title subtitle
332     type D ---title(without number)
333     """
334     #if re.match("#\d\s[0,10]\z|#[0-9]\s[0,10]\z", title)
335     #    recdblist.printutf8("typeA")
336     #elif re.match("#\d\s[0,10].|#[0-9]\s[0,10].", title)
337     #    recdblist.printutf8("typeB")
338
339 def detNameType(title):
340     """
341     type A ---title#<number>
342     type B ---title#<number>subtitle
343     type C ---title subtitle
344     type D ---title(without number)
345     """
346     recdblist.printutf8(title)
347     #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
348     rA=re.compile("(.+)#(\d*)\s*\Z")
349     tA=rA.match(title)
350     rB=re.compile("(.+)#(\d*)\s*(\D*)")
351     tB=rB.match(title)
352     if tA:
353         recdblist.printutf8("typeA")
354         recdblist.printutf8("title="+tA.group(1))
355         recdblist.printutf8("num="+tA.group(2))
356     elif tB:
357         recdblist.printutf8("typeB")
358         recdblist.printutf8("title="+tB.group(1))
359         recdblist.printutf8("num="+tB.group(2))
360         recdblist.printutf8("subtitle="+tB.group(3))
361 if __name__ == "__main__":
362     usage="usage: %prog read -h"
363     version="%prog 0.9.4"
364     parser=optparse.OptionParser(usage=usage,version=version)
365     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)")
366     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
367     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
368     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -A)")
369     parser.add_option("-t","--tssearch",action="store_true",dest="tssearch",default=False,help="Ts move auto search")
370     parser.add_option("-T","--Tsmove",action="store_true",dest="tsmove",default=False,help="Ts auto move.")
371     (opts,args)=parser.parse_args(sys.argv)
372     if opts.ltitle!="":
373         ltitle=unicode(opts.ltitle,'utf-8')
374         sf=searchFolder(ltitle, recordedpath)
375         print "###MKV###"
376         recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mkv")+".mkv"))
377         print "###MP4###"
378         recdblist.printutf8(os.path.join(sf, auto_move.get_move_dest_path(ltitle,recpath,sf,".mp4")+".mp4"))
379     elif opts.auto:
380         sa=auto_move.search_file(recpath, recordedpath, ".mkv")
381         for t in sa:
382             recdblist.printutf8(u"自動推測実行中-MKV")
383             sf=searchFolder(t,recordedpath)
384             if sf!="":
385                 recdblist.printutf8(u"移動先")
386                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
387                 recdblist.printutf8(u"実行中")
388                 auto_move.execMove(t, recpath, recordedpath,".mkv",1)
389             else:
390                 recdblist.printutf8(t+" can't find matching folder")
391         sa=auto_move.search_file(recpath, recordedpath, ".mp4")
392         for t in sa:
393             recdblist.printutf8(u"自動推測実行中-MP4")
394             sf=searchFolder(t,recordedpath)
395             if sf!="":
396                 recdblist.printutf8(u"移動先")
397                 recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
398                 recdblist.printutf8(u"実行中")
399                 auto_move.execMove(t, recpath, recordedpath,".mp4",1)
400             else:
401                 recdblist.printutf8(t+" can't find matching folder")
402     elif opts.list:
403         sa=auto_move.search_file(recpath, recordedpath,".mkv")
404         for t in sa:
405             sf=searchFolder(t,recordedpath)
406             if sf!="":
407                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mkv")+".mkv"))
408         sa=auto_move.search_file(recpath, recordedpath,".mp4")
409         for t in sa:
410             sf=searchFolder(t,recordedpath)
411             if sf!="":
412                 recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".mp4")+".mp4"))
413     elif opts.etitle!="":
414         if os.path.exists(os.path.join(recpath, etitle+".mkv")):
415             auto_move.execMove(etitle,recpath, recordedpath,".mkv",1)
416         elif os.path.exists(os.path.join(recpath, etitle+".mp4")):
417             auto_move.execMove(etitle,recpath, recordedpath,".mp4",1)
418         srcpath=os.path.join(temppath,title+".mkv")
419         etitle=unicode(opts.etitle,'utf-8')
420         try:
421             execMove_MKV(etitle,recpath, recordedpath)
422         except:
423             execMove_MP4(etitle,recpath, recordedpath)
424     elif opts.tssearch:
425         tsmovepath=""
426         try:
427             tsmovepath=unicode(configreader.getpath("ts_movepath"),'utf-8',700)
428         except:
429             ""
430         if tsmovepath!="":
431             sa=auto_move.search_file(recpath, recordedpath,".ts")
432             for t in sa:
433                 sf=searchFolder(t,recordedpath)
434                 if sf!="":
435                     recdblist.printutf8(t+u" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
436             sa=auto_move.search_file(recpath, recordedpath,".ts")
437         else:
438             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。")
439     elif opts.tsmove:
440         tsmovepath=""
441         try:
442             tsmovepath=unicode(configreader.getpath("ts_movepath"),'utf-8',700)
443         except:
444             ""
445         if tsmovepath!="":
446             sa=auto_move.search_file(recpath, recordedpath,".ts")
447             for t in sa:
448                 sf=searchFolder(t,recordedpath)
449                 if sf!="":
450                     recdblist.printutf8(u"移動先")
451                     recdblist.printutf8(t+" : "+os.path.join(sf, auto_move.get_move_dest_path(t, recpath,sf,".ts")+".ts"))
452                     recdblist.printutf8(u"実行中")
453                     auto_move.execMove(t, recpath, recordedpath,".ts",0)
454             sa=auto_move.search_file(recpath, recordedpath,".ts")
455         else:
456             recdblist.printutf8(u"ts_movepathが設定されていません。/etc/rec10.confを設定してください。")