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