OSDN Git Service

many bug fixes.
[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 time
14 import optparse
15 import sys
16 import shutil
17
18 import configreader
19 recordedpath=configreader.getpath("recorded")
20 recpath=configreader.getpath("recpath")
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         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(avipath):##tsファイルが存在している
46             dtime = time.time()-os.path.getmtime(avipath)
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         print 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 veryfySize(path):
75     vsize=[297,497,596,1196]#SD 30m 1h 1.5h 2h
76     vsize=vsize+[458,916]#HD 30m 1h
77     vsize=vsize+[381,895,447]
78     ret = 0
79     for size in vsize:
80         if os.path.getsize(path)>(size-10)*1024*1024 and os.path.getsize(path)<(size+10)*1024*1024:
81             ret=1
82     return ret
83 def searchFolder(title,path):
84     """
85     titleにマッチするフォルダを探し出す。
86     """
87     folderpath=os.listdir(path)
88     lfpath=[]
89     ngram=[]
90     for ft in folderpath:
91         fullpath=os.path.join(path, ft)
92         if os.path.isdir(fullpath):
93             lfpath.append(fullpath)
94             ftt=os.listdir(fullpath)
95             if len(ftt)>0:
96                 for ft2 in ftt:
97                     folderpath.append(os.path.join(fullpath, ft2))
98         else:
99             lfpath.append(fullpath)
100     for dirp in lfpath:
101         cmpp=""
102         appp=""
103         if os.path.isdir(dirp):
104             cmpp=os.path.dirname(dirp)
105             appp=dirp
106         else:
107             cmpp=os.path.basename(dirp)
108             appp=os.path.dirname(dirp)
109         p=n_gram.trigram(title.decode("utf-8"),cmpp.decode("utf-8"))
110         if p>0:
111             ngram.append((p,appp))
112     ngram=list(set(ngram))
113     ngram.sort()
114     ngram.reverse()
115     if len(ngram)>0:
116         if ngram[0][0]>400:
117             return ngram[0][1]
118         else:
119             return ""
120     else:
121         return ""
122 def execMove(title,temppath,recpath):
123     srcpath=os.path.join(temppath,title+".avi")
124     desttitle=destName(title, temppath, recpath)
125     sf=searchFolder(title, recpath)
126     if sf!="":
127         destpath=os.path.join(sf,desttitle+".avi")
128         shutil.move(srcpath, destpath)
129         delpath=[os.path.join(temppath,title+".ts")]
130         delpath.append(os.path.join(temppath,title+".ts.b25"))
131         delpath.append(os.path.join(temppath,title+".ts.b25.log"))
132         delpath.append(os.path.join(temppath,title+".ts.tsmix"))
133         delpath.append(os.path.join(temppath,title+".ts.log"))
134         delpath.append(os.path.join(temppath,title+".sa.avi"))
135         for dp in delpath:
136             try:
137                 os.remove(dp)
138             except:
139                 ""
140 def destName(title,temppath,recpath):
141     """
142     titleから移動先に作るべきファイル名を作り出す。
143     """
144     dstpath=os.path.join(recpath,title+".avi")
145     srcpath=os.path.join(temppath,title+".avi")
146     if os.path.exists(dstpath):
147         gmtime=time.gmtime(os.path.getmtime(srcpath))
148         times=time.strftime("%y%m%d",gmtime)
149         title=title+"_"+times
150     return title
151 if __name__ == "__main__":
152     usage="usage: %prog read -h"
153     version="%prog 0.7.0alpha"
154     parser=optparse.OptionParser(usage=usage,version=version)
155     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)")
156     parser.add_option("-A","--Auto",action="store_true",dest="auto",default=False,help="auto classifying mode(not a test)")
157     parser.add_option("-e","--exec",action="store",type="string",dest="etitle",default="",metavar="TITLE",help="exec move(not a test)")
158     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode(test for -a)")
159     (opts,args)=parser.parse_args(sys.argv)
160     if opts.ltitle!="":
161         sf=searchFolder(opts.ltitle, recordedpath)
162         print os.path.join(sf, destName(opts.ltitle,recpath,sf)+".avi")
163     elif opts.auto:
164         sa=search_avi(recpath, recordedpath)
165         for t in sa:
166             print "自動推測実行中"
167             sf=searchFolder(t,recordedpath)
168             if sf!="":
169                 print "移動先"
170                 print t+" : "+os.path.join(sf, destName(t, recpath,sf)+".avi")
171                 print "実行中"
172                 execMove(t, recpath, recordedpath)
173             else:
174                 print t+" can't find matching folder."
175     elif opts.list:
176         sa=search_avi(recpath, recordedpath)
177         for t in sa:
178             sf=searchFolder(t,recordedpath)
179             if sf!="":
180                 print t+" : "+os.path.join(sf, destName(t, recpath,sf)+".avi")
181     elif opts.etitle!="":
182         execMove(opts.etitle,recpath, recordedpath)