def new_block(self, mp3path, proof: int, previous_hash: Optional[str]) -> Dict[str, Any]: """ 生成新區塊 :param proof: The proof given by the Proof of Work algorithm :param previous_hash: Hash of previous Block :return: New Block """ block = { 'main_index': len(self.main_chain) + 1, 'timestamp': time(), 'proof': proof, 'music_hash': mp3hash(mp3path), 'previous_hash': previous_hash or self.hash(self.main_chain[-1]), } # 創立music-chain(交易區塊)的創世區塊 self.music_chain = [] self.new_music_block(len(self.main_chain) + 1, previous_hash='1', music_proof=100) self.main_chain.append(block) return block
def test_default_algorithm_is_sha1(self): hasher = hashlib.new('sha1') hash = mp3hash.mp3hash(SONG1_PATH, hasher=hasher) retcode, output = call(SCRIPT, SONG1_PATH) assert_that(output, starts_with(hash))
def test_existent_file_outputs_hash_space_filename(self): hash = mp3hash.mp3hash(SONG1_PATH) filename = os.path.basename(SONG1_PATH) retcode, output = call(SCRIPT, SONG1_PATH) assert_that(output, is_(hash + u' ' + filename + '\n'))
def _scan_file(source): """Extract metadata from this audio file.""" info = { "source": os.path.abspath(source), "hash": mp3hash(source), } # Add ID3 metadata, if available. try: eyed3.log.setLevel("ERROR") id3file = eyed3.load(source) if id3file and id3file.tag: tag = id3file.tag if tag.title: info["title"] = tag.title if tag.artist: info["artist"] = tag.artist if tag.album_artist: info["album_artist"] = tag.album_artist if tag.genre: info["genre"] = tag.genre.name if tag.bpm: info["bpm"] = tag.bpm release_date = tag.best_release_date if release_date: info["year"] = release_date.year if tag.publisher: info["publisher"] = tag.publisher except UnicodeDecodeError: pass # Insert the track record into our library. track = library.Track.create(info)
def test_maxbytes_option_changes_maxbytes_used(self): maxbytes = 1000 hash = mp3hash.mp3hash(SONG1_PATH, maxbytes=maxbytes) retcode, output = call(SCRIPT, SONG1_PATH, '--maxbytes', str(maxbytes)) assert_that(output, starts_with(hash))
def test_algorithm_option_changes_algorithm_used(self): algorithm = 'md5' hasher = hashlib.new(algorithm) hash = mp3hash.mp3hash(SONG1_PATH, hasher=hasher) retcode, output = call(SCRIPT, SONG1_PATH, '--algorithm', algorithm) assert_that(output, starts_with(hash))
def test_output_redirects_output_to_file_in_given_path(self): hash = mp3hash.mp3hash(SONG1_PATH) filename = os.path.basename(SONG1_PATH) retcode, output = call(SCRIPT, SONG1_PATH, '--output', NON_EXISTENT_FILE) with open(NON_EXISTENT_FILE) as output_file: assert_that(output_file.read(), is_(hash + u' ' + filename + '\n'))
def test_hasher_protocol(self): class Adler32Hasher(object): def __init__(self): self.value = None def update(self, data): self.value = zlib.adler32( data, *([self.value] if self.value is not None else []) ) & 0xffffffff def hexdigest(self): return hex(self.value) hasher = Adler32Hasher hash1 = mp3hash(SONG1_PATH, hasher=hasher()) hash2 = mp3hash(SONG2_PATH, hasher=hasher()) assert_that(hash1, is_(equal_to(hash2)))
def test_hasher_protocol(self): class Adler32Hasher(object): def __init__(self): self.value = None def update(self, data): self.value = zlib.adler32( data, *([self.value] if self.value is not None else [])) & 0xffffffff def hexdigest(self): return hex(self.value) hasher = Adler32Hasher hash1 = mp3hash(SONG1_PATH, hasher=hasher()) hash2 = mp3hash(SONG2_PATH, hasher=hasher()) assert_that(hash1, is_(equal_to(hash2)))
def add(self, path): file_hash = mp3hash(path) if(file_hash): try: id3 = EasyID3(path) except (ID3NoHeaderError, ValueError): return self._hashes[file_hash].add(path, id3)
def _scan_file(source): """Extract representative audio summary segments and generate metadata. source: an MP3, WAV, or other music file readable by ffmpeg """ hash = mp3hash(source) base_path = os.path.join(library.dir, hash) # Generate the summary clip, if it doesn't already exist. summary_path = base_path + '.wav' if not os.path.isfile(summary_path): _gen_summary(source, summary_path) info = { "source": os.path.abspath(source), "hash": hash, "summary": os.path.abspath(summary_path), } # Add ID3 metadata, if available. try: eyed3.log.setLevel("ERROR") id3file = eyed3.load(source) if id3file and id3file.tag: tag = id3file.tag if tag.title: info["title"] = tag.title if tag.artist: info["artist"] = tag.artist if tag.genre: info["genre"] = tag.genre.name if tag.bpm: info["bpm"] = tag.bpm release_date = tag.best_release_date if release_date: info["year"] = release_date.year except UnicodeDecodeError: pass library.Track.create(**info)
def test_maxbytes_negative(self): mp3hash(SONG1_PATH, maxbytes=-15)
def check_num(self, maxbytes): hash1 = mp3hash(SONG1_PATH, maxbytes=maxbytes) hash2 = mp3hash(SONG2_PATH, maxbytes=maxbytes) assert_that(hash1, is_(equal_to(hash2)))
def check_algs(self, alg): hash1 = mp3hash(SONG1_PATH, hasher=hashlib.new(alg)) hash2 = mp3hash(SONG2_PATH, hasher=hashlib.new(alg)) assert_that(hash1, is_(equal_to(hash2)))
def test_mp3hash(self): hash1 = mp3hash(SONG1_PATH) hash2 = mp3hash(SONG2_PATH) assert_that(hash1, is_(equal_to(hash2)))
def hash(self, filename, maxbytes=512 * 1024): return mp3hash.mp3hash(filename, maxbytes=maxbytes)
def hashAudio(songFileName): return mp3hash(songFileName)
def test_existent_file_outputs_hash_only_if_hash_opt_is_present(self): hash = mp3hash.mp3hash(SONG1_PATH) retcode, output = call(SCRIPT, SONG1_PATH, '--hash') assert_that(output, is_(hash + '\n'))
def audio_hash(upload): file = upload return mp3hash(file, None, hashlib.sha3_256())
def test_maxbytes_0(self): mp3hash(SONG1_PATH, maxbytes=-15)
# Example showing use of mp3hash library # pip install mp3hash # to strip all the header data and shove it through md5 # obtaining an md5 of just the audio data # FAIL - gives different output for the same mp3 file # with different header formats!! import sys import hashlib import mp3hash print(sys.argv[1]) # init an md5 obj otherwise mp3hash defaults to sha1 md5er = hashlib.md5() out = mp3hash.mp3hash(sys.argv[1], None, hashlib.md5()) print(out)
def hash_audio(path_to_audio): return mp3hash(path_to_audio)