2 # -*- coding: utf-8 -*-
4 #This file is part of <name prog> project
7 #Copyright (C) <year> <name|nick>
9 #This program is free software; you can redistribute it and/or
10 #modify it under the terms of the GNU General Public License
11 #as published by the Free Software Foundation; either version 2
12 #of the License, or (at your option) any later version.
14 #This program is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU General Public License for more details.
19 #You should have received a copy of the GNU General Public License
20 #along with this program; if not, write to the Free Software
21 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #You can contact author by email <my email>
31 from hashlib
import sha256
36 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
37 AUDIOCHECK_BIN
= os
.path
.join( SCRIPT_DIR
, "bin", "auCDtect")
38 DEVNULL
= open(os
.devnull
,'w')
45 def issupport(self
, filepath
):
46 '''docstring for issupport'''
47 in_name
, in_ext
= os
.path
.splitext(filepath
)
48 return not not self
.file_formats
.get( in_ext
[1:] )
49 def unpack_to_wav(self
, in_path
, out_path
=''):
50 in_name
, in_ext
= os
.path
.splitext(in_path
)
52 out_path
= tempfile
.mktemp(suffix
='.wav')
54 _args
= copy(self
.file_formats
.get( in_ext
[1:] ))
56 _args
.extend( [ os
.path
.abspath(in_path
), '-o', out_path
] )
57 opn
= subprocess
.Popen( _args
, stdout
=DEVNULL
, stderr
=DEVNULL
)
63 result_pattern
= re
.compile(
64 r
'This track looks like (?P<codec>[A-Z]{2,5}) with probability (?P<per>[\d]{1,3})%'
68 # file_item = { "name":None, "path": None, "result": {"codec":None,"per":None} }
70 self
.unpacker
= UnpackToWav()
71 def test(self
, file_path
):
72 args
= [ AUDIOCHECK_BIN
, file_path
, "-m5"]
73 # opn = os.popen( " ".join(args))
74 opn
= subprocess
.Popen( args
, stdout
= subprocess
.PIPE
, stderr
=DEVNULL
)
75 result
= opn
.communicate()[0]
77 match
= self
.result_pattern
.search( result
)
79 return match
.groupdict()
83 '''docstring for testing'''
85 for file_item
in self
.files
:
86 tmp
= self
.unpacker
.unpack_to_wav( file_item
["path"] )
87 result
= self
.test(tmp
)
88 file_item
["result"] = result
90 _summary
.append( int(result
["per"]) )
91 self
.summary
= sum(_summary
)/float(len(_summary
))
94 def load(self
, path
=os
.path
.curdir
):
95 if os
.path
.isdir( path
):
96 for f
in os
.listdir( path
):
97 if self
.unpacker
.issupport( f
):
98 self
.files
.append( {"name": f
, "path":os
.path
.join( path
, f
) } )
99 elif os
.path
.isfile( path
):
100 if self
.unpacker
.issupport( path
):
101 self
.files
.append( {"name": os
.path
.split(f
)[1], "path": path
} )
104 out_log
.append( "%s %s by Apkawa <apkawa@gmail.com>"%(NAME
, VERSION
) )
105 out_log
.append( "~"*23 )
106 out_log
.append( "~ Do not edit this file. ~" )
109 for file_item
in sorted(self
.files
):
110 result
= file_item
["result"]
112 out_log
.append( "%.2i -_- %s _-_ %s %s%%"%(i
,
113 file_item
["name"], result
["codec"], result
["per"] ) )
115 out_log
.append( "%.2i -_- %s _-_ ERROR"%(i
,
119 out_log
.append( "Summary: %.2f"%self
.summary
)
120 out_log
.append( "~"*23 )
121 str_out_log
= "\n".join( out_log
)
122 str_out_log
+= "\nsha256 log hash: %s%%"%sha
256(str_out_log
).hexdigest()
131 def main( *args
, **kwargs
):
141 if __name__
== "__main__":