OSDN Git Service

fix miss
[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         iff=""
82         try:
83             iff=u"("+configreader.getenv("iff")+u")_"
84             if iff==u"()_":
85                 iff = u""
86         except:
87             iff=""
88         title=title+u"_"+iff+gmtime.strftime("%Y-%m-%dT%H-%M-%S")
89     return title
90 def get_delpath(temppath,title):
91     delpath=[os.path.join(temppath,title+".ts")]
92     delpath.append(os.path.join(temppath,title+".avi"))
93     delpath.append(os.path.join(temppath,title+".120.avi"))
94     delpath.append(os.path.join(temppath,title+".timecode.txt"))
95     delpath.append(os.path.join(temppath,title+".aac"))
96     delpath.append(os.path.join(temppath,title+".ts.b25"))
97     delpath.append(os.path.join(temppath,title+".ts.tsmix"))
98     delpath.append(os.path.join(temppath,title+".ts.log"))
99     delpath.append(os.path.join(temppath,title+".sa.avi"))
100     delpath.append(os.path.join(temppath,title+".sa.avi.log"))
101     delpath.append(os.path.join(temppath,title+".log"))
102     return delpath
103 def searchFolder(title,path,threshold=500):
104     """
105     titleにマッチするフォルダを探し出す。
106     """
107     folderpath=os.listdir(path)
108     lfpath=[]
109     ngram=[]
110     for ft in folderpath:
111         fullpath=os.path.join(path.encode('utf-8'), ft)
112         if os.path.isdir(fullpath):
113             lfpath.append(fullpath)
114             ftt=os.listdir(fullpath)
115             if len(ftt)>0:
116                 for ft2 in ftt:
117                     try:
118                         folderpath.append(os.path.join(fullpath, ft2))
119                     except Exception, inst:
120                         #print type(inst)
121                         #print inst
122                         ""
123         else:
124             lfpath.append(fullpath)
125     for dirp in lfpath:
126         cmpp=""
127         appp=""
128         ntitle=title
129         if os.path.isdir(dirp):
130             cmpp=os.path.dirname(dirp)
131             appp=dirp
132         else:
133             cmpp=os.path.basename(dirp)
134             appp=os.path.dirname(dirp)
135         ntitle=getTitle(title)
136         #recdblist.printutf8(cmpp)
137         cmpp=getTitle(os.path.splitext(cmpp)[0])
138         #recdblist.printutf8(cmpp+"\n")
139         p=n_gram.trigram(ntitle,cmpp)
140         if p>0:
141             ngram.append((p,appp))
142     ngram=list(set(ngram))
143     ngram.sort()
144     ngram.reverse()
145     if len(ngram)>0:
146         #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
147         if ngram[0][0]>threshold:
148             return ngram[0][1]
149         else:
150             return ""
151     else:
152         return ""
153 def execMove(title,temppath,recpath,ext,autodel):
154     srcpath=os.path.join(temppath,title+ext)
155     #desttitle=destName(title, temppath, recpath)
156
157     sf=searchFolder(title, recpath)
158     if sf!="":
159         destpath=os.path.join(sf,destNameMKV(title, temppath, sf)+ext)
160         recdblist.printutf8("moving now..")
161         recdblist.printutf8(srcpath+" : "+destpath)
162         print srcpath
163         shutil.move(srcpath, destpath)
164         #shutil.copy(srcpath, destpath)
165         if autodel==1:
166             delpath=get_delpath(temppath, title)
167             for dp in delpath:
168                 try:
169                     os.remove(dp)
170                     ""
171                 except:
172                     ""