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