Skip to content

Commit

Permalink
Fixed bug where cover can't be found (SathyaBhat#154)
Browse files Browse the repository at this point in the history
* added album, release year and track number metadata

* fixed the tests

* added album cover

* Update test_spotify_fetch_tracks.py

* added genre metadata

* genre added

* fixed style

* Update spotify.py

* Update youtube.py

* Update youtube.py

* remove the env as its not useful

* fixed bug where cover can't be found

Co-authored-by: Sathyajith Bhat <[email protected]>
  • Loading branch information
gonzk and SathyaBhat committed Feb 27, 2021
1 parent 34ad664 commit da805a4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
16 changes: 13 additions & 3 deletions spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def fetch_tracks(sp, item_type, url):
track_year = item['track']['album']['release_date'][:4]
album_total = item['track']['album']['total_tracks']
track_num = item['track']['track_number']
cover = item['track']['album']['images'][0]['url']
if len(item['track']['album']['images']) > 0:
cover = item['track']['album']['images'][0]['url']
else:
cover = None

if len(sp.artist(artist_id=item['track']['artists'][0]['uri'])['genres']) > 0:
genre = sp.artist(artist_id=item['track']['artists'][0]['uri'])['genres'][0]
else:
Expand All @@ -55,7 +59,10 @@ def fetch_tracks(sp, item_type, url):
track_album = album_info['name']
track_year = album_info['release_date'][:4]
album_total = album_info['total_tracks']
cover = album_info['images'][0]['url']
if len(album_info['images']) > 0:
cover = album_info['images'][0]['url']
else:
cover = None
if len(sp.artist(artist_id=album_info['artists'][0]['uri'])['genres']) > 0:
genre = sp.artist(artist_id=album_info['artists'][0]['uri'])['genres'][0]
else:
Expand All @@ -82,7 +89,10 @@ def fetch_tracks(sp, item_type, url):
track_year = items['album']['release_date'][:4]
album_total = items['album']['total_tracks']
track_num = items['track_number']
cover = items['album']['images'][0]['url']
if len(items['album']['images']) > 0:
cover = items['album']['images'][0]['url']
else:
cover = None
if len(sp.artist(artist_id=items['artists'][0]['uri'])['genres']) > 0:
genre = sp.artist(artist_id=items['artists'][0]['uri'])['genres'][0]
else:
Expand Down
16 changes: 9 additions & 7 deletions spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ def download_songs(songs, download_directory, format_string, skip_mp3, keep_play
song_file['tracknumber'] = str(song.get('num')) + '/' + str(song.get('num_tracks'))
song_file['genre'] = song.get('genre')
song_file.save()
song_file = MP3(path.join(f"{file_path}.mp3"), ID3=ID3)
song_file.tags['APIC'] = APIC(
encoding=3,
mime='image/jpeg',
type=3, desc=u'Cover',
data=urllib.request.urlopen(song.get('cover')).read()
)
song_file = MP3(path.join(download_directory, f"{song.get('artist')} - {song.get('name')}.mp3"),
ID3=ID3)
if song.get('cover') is not None:
song_file.tags['APIC'] = APIC(
encoding=3,
mime='image/jpeg',
type=3, desc=u'Cover',
data=urllib.request.urlopen(song.get('cover')).read()
)
song_file.save()
56 changes: 47 additions & 9 deletions tests/test_youtube.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import os
import urllib
import urllib.request

from mutagen.easyid3 import EasyID3
from mutagen.id3 import APIC, ID3
from mutagen.mp3 import MP3

from spotify_dl import youtube as yt


def test_download_one_false_skip():

songs = [{'album': 'Hell Freezes Over (Remaster 2018)',
'artist': 'Eagles',
'cover': 'https://i.scdn.co/image/ab67616d0000b27396d28597a5ae44ab66552183',
'genre': 'album rock',
'name': 'Hotel California - Live On MTV, 1994',
'num': 6,
'num_tracks': 15,
'year': '1994'}]
yt.download_songs(songs, download_directory='~/Downloads', format_string='best',
'artist': 'Eagles',
'cover': 'https://i.scdn.co/image/ab67616d0000b27396d28597a5ae44ab66552183',
'genre': 'album rock',
'name': 'Hotel California - Live On MTV, 1994',
'num': 6,
'num_tracks': 15,
'year': '1994'}]
yt.download_songs(songs, download_directory=os.path.dirname(os.path.realpath(__file__)),
format_string='best',
skip_mp3=False)
music = MP3("tests/Eagles - Hotel California - Live On MTV, 1994.mp3", ID3=EasyID3)
tags = ID3("tests/Eagles - Hotel California - Live On MTV, 1994.mp3")
assert (music['artist'][0] == 'Eagles')
assert (music['album'][0] == 'Hell Freezes Over (Remaster 2018)')
assert (music['genre'][0] == 'album rock')
assert (tags.getall("APIC")[0].data == APIC(encoding=3,
mime='image/jpeg',
type=3, desc=u'Cover',
data=urllib.request.urlopen(songs[0].get('cover')).read()
))

def test_download_one_true_skip():
songs = [
Expand All @@ -26,3 +44,23 @@ def test_download_one_true_skip():
'year': '1994'}]
yt.download_songs(songs, download_directory='~/Downloads', format_string='best',
skip_mp3=False)

def test_download_cover_none():
songs = [
{'album': 'Queen II (Deluxe Remastered Version)',
'artist': 'Queen',
'cover': None,
'genre': 'classic rock',
'name': "The Fairy Feller's Master-Stroke - Remastered 2011",
'num': 7,
'num_tracks': 16,
'year': '1974'}]
yt.download_songs(songs, download_directory=os.path.dirname(os.path.realpath(__file__)),
format_string='best',
skip_mp3=False)
music = MP3("tests/Queen - The Fairy Feller's Master-Stroke - Remastered 2011.mp3", ID3=EasyID3)
tags = ID3("tests/Queen - The Fairy Feller's Master-Stroke - Remastered 2011.mp3")
assert (music['artist'][0] == 'Queen')
assert (music['album'][0] == 'Queen II (Deluxe Remastered Version)')
assert (music['genre'][0] == 'classic rock')
assert (len(tags.getall("APIC")) == 0)

0 comments on commit da805a4

Please sign in to comment.