OSDN Git Service

c353cbe142e2be803d8543571d8dfdd3dbd0a285
[rec10/rec10-git.git] / rec10 / trunk / src / auto_move.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5
6 import os
7 import os.path
8 import re
9 import time
10
11 import n_gram
12 import recdblist
13 def getTitle(title):
14     rT=re.compile("(.+)_(\d+)\Z")
15     tT=rT.match(title)
16     rT2=re.compile("(.+)_(.+)_(\d*)\Z")#_(aichi)_2010-02-06T01:59:00.mkv
17     tT2=rT2.match(title)
18     rT3=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+:\d+:\d+\Z")#_(aichi)_2010-02-06T01:59:00.mkv
19     tT3=rT3.match(title)
20     rT4=re.compile("(.+)_(.+)_\d+-\d+-\d+T\d+-\d+-\d+\Z")#_(aichi)_2010-02-06T01-59-00.mkv
21     tT4=rT4.match(title)
22     rT5=re.compile("(.+)_(.+)_(.+)\Z")#_(aichi)_2010-02-06T01-59-00.mkv
23     tT5=rT5.match(title)
24     ntitle=title
25     if tT :
26         ntitle=tT.group(1)
27     elif tT2:
28         ntitle=tT2.group(1)
29     elif tT3:
30         ntitle=tT3.group(1)
31     elif tT4:
32         ntitle=tT4.group(1)
33     elif tT5:
34         ntitle=tT5.group(1)
35     return ntitle
36 def detNameType(title):
37     """
38     type A ---title#<number>
39     type B ---title#<number>subtitle
40     type C ---title subtitle
41     type D ---title(without number)
42     """
43     recdblist.printutf8(title)
44     #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
45     rA=re.compile("(.+)#(\d*)\s*\Z")
46     tA=rA.match(title)
47     rB=re.compile("(.+)#(\d*)\s*(\D*)")
48     tB=rB.match(title)
49     if tA:
50         recdblist.printutf8("typeA")
51         recdblist.printutf8("title="+tA.group(1))
52         recdblist.printutf8("num="+tA.group(2))
53     elif tB:
54         recdblist.printutf8("typeB")
55         recdblist.printutf8("title="+tB.group(1))
56         recdblist.printutf8("num="+tB.group(2))
57         recdblist.printutf8("subtitle="+tB.group(3))
58 def search_file(temppath,recpath,ext):
59     """
60     録画一時フォルダ内mkvファイルを検索
61     """
62     avilist = glob.glob(temppath + "/*"+ext)
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(ext, "")
69         avipath = os.path.join(dir, title + ext)
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 get_move_dest_path(title,temppath,recpath,ext):
77     dstpath=os.path.join(recpath,title+ext)
78     srcpath=os.path.join(temppath,title+ext)
79     if os.path.exists(dstpath):
80         gmtime=time.gmtime(os.path.getmtime(srcpath))
81         times=time.strftime("%y%m%d",gmtime)
82         title=title+"_"+times
83     return title
84 def get_delpath(temppath,title):
85     delpath=[os.path.join(temppath,title+".ts")]
86     delpath.append(os.path.join(temppath,title+".avi"))
87     delpath.append(os.path.join(temppath,title+".120.avi"))
88     delpath.append(os.path.join(temppath,title+".timecode.txt"))
89     delpath.append(os.path.join(temppath,title+".aac"))
90     delpath.append(os.path.join(temppath,title+".ts.b25"))
91     delpath.append(os.path.join(temppath,title+".ts.tsmix"))
92     delpath.append(os.path.join(temppath,title+".ts.log"))
93     delpath.append(os.path.join(temppath,title+".sa.avi"))
94     delpath.append(os.path.join(temppath,title+".sa.avi.log"))
95     delpath.append(os.path.join(temppath,title+".log"))
96     return delpath
97 def searchFolder(title,path,threshold=500):
98     """
99     titleにマッチするフォルダを探し出す。
100     """
101     folderpath=os.listdir(path)
102     lfpath=[]
103     ngram=[]
104     for ft in folderpath:
105         fullpath=os.path.join(path.encode('utf-8'), ft)
106         if os.path.isdir(fullpath):
107             lfpath.append(fullpath)
108             ftt=os.listdir(fullpath)
109             if len(ftt)>0:
110                 for ft2 in ftt:
111                     try:
112                         folderpath.append(os.path.join(fullpath, ft2))
113                     except Exception, inst:
114                         #print type(inst)
115                         #print inst
116                         ""
117         else:
118             lfpath.append(fullpath)
119     for dirp in lfpath:
120         cmpp=""
121         appp=""
122         ntitle=title
123         if os.path.isdir(dirp):
124             cmpp=os.path.dirname(dirp)
125             appp=dirp
126         else:
127             cmpp=os.path.basename(dirp)
128             appp=os.path.dirname(dirp)
129         ntitle=getTitle(title)
130         #recdblist.printutf8(cmpp)
131         cmpp=getTitle(os.path.splitext(cmpp)[0])
132         #recdblist.printutf8(cmpp+"\n")
133         p=n_gram.trigram(ntitle,cmpp)
134         if p>0:
135             ngram.append((p,appp))
136     ngram=list(set(ngram))
137     ngram.sort()
138     ngram.reverse()
139     if len(ngram)>0:
140         #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
141         if ngram[0][0]>threshold:
142             return ngram[0][1]
143         else:
144             return ""
145     else:
146         return ""
147 def execMove(title,temppath,recpath,ext,autodel):
148     srcpath=os.path.join(temppath,title+ext)
149     #desttitle=destName(title, temppath, recpath)
150
151     sf=searchFolder(title, recpath)
152     if sf!="":
153         destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+ext)
154         recdblist.printutf8("moving now..")
155         recdblist.printutf8(srcpath+" : "+destpath)
156         print srcpath
157         shutil.move(srcpath, destpath)
158         #shutil.copy(srcpath, destpath)
159         if autodel==1:
160             delpath=get_delpath(temppath, title)
161             for dp in delpath:
162                 try:
163                     os.remove(dp)
164                     ""
165                 except:
166                     ""