OSDN Git Service

new classify function added.
[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
17 import configreader
18 recordedpath=configreader.getpath("recorded")
19 recpath=configreader.getpath("recpath")
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         tspath = os.path.join(dir, title + ".ts")
32         b25f = b25f.replace(".ts.b25", ".avi")
33         mode = "ts"
34         if os.path.isfile(tspath):##tsファイルが存在している
35             dtime = time.time()-os.path.getmtime(tspath)
36             dtime = int(dtime)
37             if dtime > 120:
38                 if os.path.getsize(tspath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
39                     mode = "avi"
40                 else:
41                     mode = "tsmiss"
42             else:
43                 mode = "ts"
44         if os.path.isfile(avipath):##tsファイルが存在している
45             dtime = time.time()-os.path.getmtime(avipath)
46             dtime = int(dtime)
47             if dtime > 120:
48                 if os.path.getsize(avipath) > 1 * 1000 * 1000:##最終更新から22分以上経過かつ1MB以上
49                     mode = "fin"
50                 else:
51                     mode = "avimiss"
52             else:
53                 mode = "avi"
54         print title + ":" + mode
55 def search_avi(temppath,recpath):
56     avilist = glob.glob(temppath + "/*.avi")
57     ret=[]
58     for avif in avilist:
59         ##b25f is title.ts.b25  avi is title.avi
60         dir = os.path.split(avif)[0]
61         title = os.path.split(avif)[1]
62         title = title.replace(".avi", "")
63         avipath = os.path.join(dir, title + ".avi")
64         tspath = os.path.join(dir, title + ".ts")
65         b25path = os.path.join(dir, title + ".ts.b25")
66         b25logpath = os.path.join(dir, title + ".ts.b25.log")
67         sapath = os.path.join(dir, title + ".sa.avi")
68         dtime = time.time()-os.path.getmtime(avipath)
69         dtime = int(dtime)
70         #print title+str(dtime)
71         if dtime > 300:
72             if veryfySize(avipath):
73                 sf=searchFolder(title,recpath)
74                 if sf!="":
75                     ret.append([title,sf])
76     return ret
77 def veryfySize(path):
78     vsize=[297,596,458,916]
79     ret = 0
80     for size in vsize:
81         if os.path.getsize(path)>(size-5)*1024*1024 and os.path.getsize(path)<(size+5)*1024*1024:
82             ret=1
83     return ret
84 def searchFolder(title,path):
85     folderpath=os.listdir(path)
86     lfpath=[]
87     ngram=[]
88     for ft in folderpath:
89         fullpath=os.path.join(path, ft)
90         if os.path.isdir(fullpath):
91             lfpath.append(fullpath)
92             ftt=os.listdir(fullpath)
93             if len(ftt)>0:
94                 for ft2 in ftt:
95                     folderpath.append(os.path.join(fullpath, ft2))
96         else:
97             lfpath.append(fullpath)
98     for dirp in lfpath:
99         cmpp=""
100         appp=""
101         if os.path.isdir(dirp):
102             cmpp=os.path.dirname(dirp)
103             appp=dirp
104         else:
105             cmpp=os.path.basename(dirp)
106             appp=os.path.dirname(dirp)
107         p=n_gram.trigram(title.decode("utf-8"),cmpp.decode("utf-8"))
108         if p>0:
109             ngram.append((p,appp))
110     ngram=list(set(ngram))
111     ngram.sort()
112     ngram.reverse()
113     if len(ngram)>0:
114         if ngram[0][0]>400:
115             return ngram[0][1]
116         else:
117             return ""
118     else:
119         return ""
120 def execMove(title,temppath,recpath):
121     ""
122 def detName(title,temppath,recpath):
123     dstpath=os.path.join(recpath,title+".avi")
124     srcpath=os.path.join(temppath,title+".avi")
125     print srcpath
126     if os.path.exists(dstpath):
127         gmtime=time.gmtime(os.path.getmtime(srcpath))
128         times=time.strftime("%y%m%d",gmtime)
129         title=title+"_"+times
130     return title
131 if __name__ == "__main__":
132     usage="usage: %prog [-l Title] [-a]"
133     version="%prog 0.7.0alpha"
134     parser=optparse.OptionParser(usage=usage,version=version)
135     parser.add_option("-s","--search",action="store",type="string",dest="ltitle",default="",metavar="TITLE",help="test to search where the title should be contained.")
136     parser.add_option("-a",action="store_true",dest="auto",default=False,help="auto disposing mode")
137     parser.add_option("-l","--list",action="store_true",dest="list",default=False,help="File listing mode")
138     (opts,args)=parser.parse_args(sys.argv)
139     if opts.ltitle!="":
140         searchFolder(opts.ltitle, "/home/ftpusr/ftp4/録画終了/")
141     if opts.auto:
142         ""
143     if opts.list:
144         sa=search_avi(recpath, recordedpath)
145         for s in sa:
146             print s[0]+" : "+os.path.join(s[1], detName(s[0], recpath,s[1])+".avi")
147