Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cover art download #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions spotify_ripper/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import os
import sys
import base64
import urllib



def set_metadata_tags(args, audio_file, idx, track, ripper):
Expand Down Expand Up @@ -69,7 +71,19 @@ def set_metadata_tags(args, audio_file, idx, track, ripper):
genres_ascii = [to_ascii(genre) for genre in genres]

# cover art image
image = track.album.cover()
def get_cover_image(image_link):
image_link = 'https://i.scdn.co%s' % (
image_link[len('spotify'):].replace(':', '/'))
cover_file = urllib.urlretrieve(image_link)[0]

with open(cover_file, "rb") as f:
if f.mode == "rb":
return f.read()
else:
return None

image_link = str(track.album.cover(2).link)
image = get_cover_image(image_link)

def tag_to_ascii(_str, _str_ascii):
return _str if args.ascii_path_only else _str_ascii
Expand All @@ -82,13 +96,13 @@ def idx_of_total_str(_idx, _total):

def save_cover_image(embed_image_func):
if image is not None:
image.load()

def write_image(file_name):
cover_path = os.path.dirname(audio_file)
cover_file = os.path.join(cover_path, file_name)
if not path_exists(cover_file):
with open(cover_file, "wb") as f:
f.write(image.data)
f.write(image)

if args.cover_file is not None:
write_image(args.cover_file[0])
Expand All @@ -109,7 +123,7 @@ def embed_image():
mime='image/jpeg',
type=3,
desc='Front Cover',
data=image.data
data=image
)
)

Expand Down Expand Up @@ -168,7 +182,7 @@ def embed_image():
mime='image/jpeg',
type=3,
desc='Front Cover',
data=image.data
data=image
)
)

Expand Down Expand Up @@ -224,7 +238,7 @@ def embed_image():
pic.type = 3
pic.mime = "image/jpeg"
pic.desc = "Front Cover"
pic.data = image.data
pic.data = image
if args.output_type == "flac":
audio.add_picture(pic)
else:
Expand Down Expand Up @@ -262,7 +276,7 @@ def set_mp4_tags(audio):
audio.add_tags()

def embed_image():
audio.tags["covr"] = mp4.MP4Cover(image.data)
audio.tags["covr"] = mp4.MP4Cover(image)

save_cover_image(embed_image)

Expand All @@ -289,7 +303,7 @@ def set_m4a_tags(audio):
audio.add_tags()

def embed_image():
audio.tags[str("covr")] = m4a.M4ACover(image.data)
audio.tags[str("covr")] = m4a.M4ACover(image)

save_cover_image(embed_image)

Expand Down