diff --git a/CHANGELOG.md b/CHANGELOG.md index f94efaf..e8311c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ This project adheres to [Semantic Versioning](https://semver.org). * Use try/except for track/disc number conversions. * Add check for files above maximum allowed upload size. +* Properly calculate client ID for MP3s that don't + have an MPEG frame at the start of the file or + immediately following an ID3v2 tag. ## [2.5.0](https://github.com/thebigmunch/google-music-proto/releases/tag/2.5.0) (2019-07-22) diff --git a/pyproject.toml b/pyproject.toml index 20e8d0c..ecd05bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ homepage = "https://github.com/thebigmunch/google-music-proto" python = "^3.6" attrs = ">=18.2,<19.4" -audio-metadata = "^0.5" +audio-metadata = "^0.6" marshmallow = "^2.0" pendulum = "^2.0" protobuf = "^3.5" diff --git a/src/google_music_proto/musicmanager/utils.py b/src/google_music_proto/musicmanager/utils.py index 5a1be25..7c11648 100644 --- a/src/google_music_proto/musicmanager/utils.py +++ b/src/google_music_proto/musicmanager/utils.py @@ -23,6 +23,31 @@ def generate_client_id(song): md5sum = None if isinstance(song, audio_metadata.FLAC): md5sum = unhexlify(song.streaminfo.md5) + elif isinstance(song, audio_metadata.MP3): + m = md5() + + if '_id3' in song and isinstance(song._id3, audio_metadata.ID3v2): + audio_start = song._id3._size + else: + audio_start = 0 + + audio_size = song.streaminfo._end - audio_start + + with open(song.filepath, 'rb') as f: + f.seek(audio_start, os.SEEK_SET) + + # Speed up by reading in chunks + read = 0 + while True: + read_size = min(audio_size - read, 65536) + if read_size <= 0: + break + + read += read_size + data = f.read(read_size) + m.update(data) + + md5sum = m.digest() else: m = md5() @@ -34,7 +59,7 @@ def generate_client_id(song): read = 0 while True: read_size = min(audio_size - read, 65536) - if not read_size: + if read_size <= 0: break read += read_size