more scripts file
[archive.git] / Apkawa / pyaudiocheck / pyaudiocheck.py
blob28ba487059bab891a276591cbb9ba45eb74ac6f3
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ###
4 #This file is part of <name prog> project
6 #<описание программы>
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>
24 ###
25 import os
26 import sys
27 import tempfile
28 import subprocess
29 import re
30 from copy import copy
31 from hashlib import sha256
33 NAME = "pyaudiocheck"
34 VERSION = "0.0.1a"
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')
39 class UnpackToWav:
40 file_formats = {
41 'flac':['flac','-d']
43 def __init__(self):
44 pass
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)
51 if not out_path:
52 out_path = tempfile.mktemp(suffix='.wav')
53 if in_ext:
54 _args = copy(self.file_formats.get( in_ext[1:] ))
55 if _args:
56 _args.extend( [ os.path.abspath(in_path), '-o', out_path ] )
57 opn = subprocess.Popen( _args, stdout=DEVNULL, stderr=DEVNULL )
58 return out_path
62 class AudioCheck:
63 result_pattern = re.compile(
64 r'This track looks like (?P<codec>[A-Z]{2,5}) with probability (?P<per>[\d]{1,3})%'
66 files = []
67 summary = 0
68 # file_item = { "name":None, "path": None, "result": {"codec":None,"per":None} }
69 def __init__(self):
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]
76 os.remove(file_path)
77 match = self.result_pattern.search( result)
78 if match:
79 return match.groupdict()
80 else:
81 return False
82 def testing(self):
83 '''docstring for testing'''
84 _summary = []
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
89 if 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 } )
102 def make_log(self):
103 out_log = []
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. ~" )
107 out_log.append( "" )
108 i = 1
109 for file_item in sorted(self.files):
110 result = file_item["result"]
111 if result:
112 out_log.append( "%.2i -_- %s _-_ %s %s%%"%(i,
113 file_item["name"], result["codec"], result["per"] ) )
114 else:
115 out_log.append( "%.2i -_- %s _-_ ERROR"%(i,
116 file_item["name"] ))
118 out_log.append( "" )
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%%"%sha256(str_out_log).hexdigest()
123 return str_out_log
131 def main( *args, **kwargs):
133 main()
135 auc = AudioCheck()
136 auc.load()
137 auc.testing()
138 print auc.make_log()
141 if __name__ == "__main__":
142 main()