OSDN Git Service

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