Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Update client ID generation to support MP3s with junk/padding bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
thebigmunch committed Oct 18, 2019
1 parent 5328db8 commit ec4b60e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 26 additions & 1 deletion src/google_music_proto/musicmanager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down

0 comments on commit ec4b60e

Please sign in to comment.