mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-12-08 20:39:08 +00:00
Rebase fixes
This commit is contained in:
@@ -149,7 +149,7 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True):
|
|||||||
"-nf",
|
"-nf",
|
||||||
"--no-fallback-metadata",
|
"--no-fallback-metadata",
|
||||||
default=config["no-fallback-metadata"],
|
default=config["no-fallback-metadata"],
|
||||||
help="use YouTube metadata as fallback if track not found on Spotify",
|
help="do not use YouTube as fallback for metadata if track not found on Spotify",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|||||||
@@ -148,7 +148,8 @@ class EmbedMetadata:
|
|||||||
def _embed_basic_metadata(self, audiofile, preset=TAG_PRESET):
|
def _embed_basic_metadata(self, audiofile, preset=TAG_PRESET):
|
||||||
meta_tags = self.meta_tags
|
meta_tags = self.meta_tags
|
||||||
audiofile[preset["artist"]] = meta_tags["artists"][0]["name"]
|
audiofile[preset["artist"]] = meta_tags["artists"][0]["name"]
|
||||||
audiofile[preset["albumartist"]] = meta_tags["album"]["artists"][0]["name"]
|
if meta_tags["album"]["artists"][0]["name"]:
|
||||||
|
audiofile[preset["albumartist"]] = meta_tags["album"]["artists"][0]["name"]
|
||||||
if meta_tags["album"]["name"]:
|
if meta_tags["album"]["name"]:
|
||||||
audiofile[preset["album"]] = meta_tags["album"]["name"]
|
audiofile[preset["album"]] = meta_tags["album"]["name"]
|
||||||
audiofile[preset["title"]] = meta_tags["name"]
|
audiofile[preset["title"]] = meta_tags["name"]
|
||||||
|
|||||||
@@ -12,9 +12,6 @@ import os
|
|||||||
from spotdl import const
|
from spotdl import const
|
||||||
from spotdl import internals
|
from spotdl import internals
|
||||||
|
|
||||||
spotify = None
|
|
||||||
|
|
||||||
|
|
||||||
# token = generate_token()
|
# token = generate_token()
|
||||||
# spotify = spotipy.Spotify(auth=token)
|
# spotify = spotipy.Spotify(auth=token)
|
||||||
|
|
||||||
@@ -86,6 +83,7 @@ class SpotifyAuthorize:
|
|||||||
# Some sugar
|
# Some sugar
|
||||||
meta_tags["year"], *_ = meta_tags["release_date"].split("-")
|
meta_tags["year"], *_ = meta_tags["release_date"].split("-")
|
||||||
meta_tags["duration"] = meta_tags["duration_ms"] / 1000.0
|
meta_tags["duration"] = meta_tags["duration_ms"] / 1000.0
|
||||||
|
meta_tags["spotify_metadata"] = True
|
||||||
# Remove unwanted parameters
|
# Remove unwanted parameters
|
||||||
del meta_tags["duration_ms"]
|
del meta_tags["duration_ms"]
|
||||||
del meta_tags["available_markets"]
|
del meta_tags["available_markets"]
|
||||||
@@ -157,13 +155,13 @@ class SpotifyAuthorize:
|
|||||||
album = self.spotify.album(album_id)
|
album = self.spotify.album(album_id)
|
||||||
return album
|
return album
|
||||||
|
|
||||||
def fetch_album_from_artist(self, artist_url, album_type="album"):
|
def fetch_albums_from_artist(self, artist_url, album_type=None):
|
||||||
"""
|
"""
|
||||||
This funcction returns all the albums from a give artist_url using the US
|
This funcction returns all the albums from a give artist_url using the US
|
||||||
market
|
market
|
||||||
:param artist_url - spotify artist url
|
:param artist_url - spotify artist url
|
||||||
:param album_type - the type of album to fetch (ex: single) the default is
|
:param album_type - the type of album to fetch (ex: single) the default is
|
||||||
a standard album
|
all albums
|
||||||
:param return - the album from the artist
|
:param return - the album from the artist
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -181,6 +179,7 @@ class SpotifyAuthorize:
|
|||||||
|
|
||||||
return albums
|
return albums
|
||||||
|
|
||||||
|
|
||||||
def write_all_albums_from_artist(self, artist_url, text_file=None):
|
def write_all_albums_from_artist(self, artist_url, text_file=None):
|
||||||
"""
|
"""
|
||||||
This function gets all albums from an artist and writes it to a file in the
|
This function gets all albums from an artist and writes it to a file in the
|
||||||
@@ -193,7 +192,7 @@ class SpotifyAuthorize:
|
|||||||
album_base_url = "https://open.spotify.com/album/"
|
album_base_url = "https://open.spotify.com/album/"
|
||||||
|
|
||||||
# fetching all default albums
|
# fetching all default albums
|
||||||
albums = self.fetch_album_from_artist(artist_url)
|
albums = self.fetch_albums_from_artist(artist_url, album_type=None)
|
||||||
|
|
||||||
# if no file if given, the default save file is in the current working
|
# if no file if given, the default save file is in the current working
|
||||||
# directory with the name of the artist
|
# directory with the name of the artist
|
||||||
@@ -205,13 +204,6 @@ class SpotifyAuthorize:
|
|||||||
log.info("Fetching album: " + album["name"])
|
log.info("Fetching album: " + album["name"])
|
||||||
self.write_album(album_base_url + album["id"], text_file=text_file)
|
self.write_album(album_base_url + album["id"], text_file=text_file)
|
||||||
|
|
||||||
# fetching all single albums
|
|
||||||
singles = self.fetch_album_from_artist(artist_url, album_type="single")
|
|
||||||
|
|
||||||
for single in singles:
|
|
||||||
log.info("Fetching single: " + single["name"])
|
|
||||||
self.write_album(album_base_url + single["id"], text_file=text_file)
|
|
||||||
|
|
||||||
def write_album(self, album_url, text_file=None):
|
def write_album(self, album_url, text_file=None):
|
||||||
album = self.fetch_album(album_url)
|
album = self.fetch_album(album_url)
|
||||||
tracks = self.spotify.album_tracks(album["id"])
|
tracks = self.spotify.album_tracks(album["id"])
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ def go_pafy(raw_song, meta_tags=None):
|
|||||||
def match_video_and_metadata(track):
|
def match_video_and_metadata(track):
|
||||||
""" Get and match track data from YouTube and Spotify. """
|
""" Get and match track data from YouTube and Spotify. """
|
||||||
meta_tags = None
|
meta_tags = None
|
||||||
spotipy = spotify_tools.SpotifyAuthorize()
|
spotify = spotify_tools.SpotifyAuthorize()
|
||||||
|
|
||||||
|
|
||||||
def fallback_metadata(meta_tags):
|
def fallback_metadata(meta_tags):
|
||||||
@@ -68,13 +68,13 @@ def match_video_and_metadata(track):
|
|||||||
content = go_pafy(track, meta_tags=None)
|
content = go_pafy(track, meta_tags=None)
|
||||||
track = slugify(content.title).replace("-", " ")
|
track = slugify(content.title).replace("-", " ")
|
||||||
if not const.args.no_metadata:
|
if not const.args.no_metadata:
|
||||||
meta_tags = spotify_tools.generate_metadata(track)
|
meta_tags = spotify.generate_metadata(track)
|
||||||
meta_tags = fallback_metadata(meta_tags)
|
meta_tags = fallback_metadata(meta_tags)
|
||||||
|
|
||||||
elif internals.is_spotify(track):
|
elif internals.is_spotify(track):
|
||||||
log.debug("Input song is a Spotify URL")
|
log.debug("Input song is a Spotify URL")
|
||||||
# Let it generate metadata, YouTube doesn't know Spotify slang
|
# Let it generate metadata, YouTube doesn't know Spotify slang
|
||||||
meta_tags = spotify_tools.generate_metadata(track)
|
meta_tags = spotify.generate_metadata(track)
|
||||||
content = go_pafy(track, meta_tags)
|
content = go_pafy(track, meta_tags)
|
||||||
if const.args.no_metadata:
|
if const.args.no_metadata:
|
||||||
meta_tags = None
|
meta_tags = None
|
||||||
@@ -84,7 +84,7 @@ def match_video_and_metadata(track):
|
|||||||
if const.args.no_metadata:
|
if const.args.no_metadata:
|
||||||
content = go_pafy(track, meta_tags=None)
|
content = go_pafy(track, meta_tags=None)
|
||||||
else:
|
else:
|
||||||
meta_tags = spotify_tools.generate_metadata(track)
|
meta_tags = spotify.generate_metadata(track)
|
||||||
content = go_pafy(track, meta_tags=meta_tags)
|
content = go_pafy(track, meta_tags=meta_tags)
|
||||||
meta_tags = fallback_metadata(meta_tags)
|
meta_tags = fallback_metadata(meta_tags)
|
||||||
|
|
||||||
@@ -98,7 +98,8 @@ def generate_metadata(content):
|
|||||||
"artists": [{"name": content.author}],
|
"artists": [{"name": content.author}],
|
||||||
"duration": content.length,
|
"duration": content.length,
|
||||||
"external_urls": {"youtube": content.watchv_url},
|
"external_urls": {"youtube": content.watchv_url},
|
||||||
"album": {"images" : [{"url": content.getbestthumb()}], "name": None},
|
"album": {"images" : [{"url": content.getbestthumb()}],
|
||||||
|
"artists": [{"name": None}],"name": None},
|
||||||
"year": content.published.split("-")[0],
|
"year": content.published.split("-")[0],
|
||||||
"release_date": content.published.split(" ")[0],
|
"release_date": content.published.split(" ")[0],
|
||||||
"type": "track",
|
"type": "track",
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ def pytest_namespace():
|
|||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def metadata_fixture():
|
def metadata_fixture():
|
||||||
meta_tags = spotify_tools.generate_metadata(SPOTIFY_TRACK_URL)
|
meta_tags = spotify_tools.SpotifyAuthorize().generate_metadata(SPOTIFY_TRACK_URL)
|
||||||
return meta_tags
|
return meta_tags
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,23 +6,26 @@ import loader
|
|||||||
|
|
||||||
loader.load_defaults()
|
loader.load_defaults()
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def spotify():
|
||||||
|
return spotify_tools.SpotifyAuthorize()
|
||||||
|
|
||||||
def test_generate_token():
|
def test_generate_token(spotify):
|
||||||
token = spotify_tools.generate_token()
|
token = spotify.generate_token()
|
||||||
assert len(token) == 83
|
assert len(token) == 83
|
||||||
|
|
||||||
|
|
||||||
def test_refresh_token():
|
def test_refresh_token(spotify):
|
||||||
old_instance = spotify_tools.spotify
|
old_instance = spotify.spotify
|
||||||
spotify_tools.refresh_token()
|
spotify.refresh_token()
|
||||||
new_instance = spotify_tools.spotify
|
new_instance = spotify.spotify
|
||||||
assert not old_instance == new_instance
|
assert not old_instance == new_instance
|
||||||
|
|
||||||
|
|
||||||
class TestGenerateMetadata:
|
class TestGenerateMetadata:
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def metadata_fixture(self):
|
def metadata_fixture(self, spotify):
|
||||||
metadata = spotify_tools.generate_metadata("ncs - spectre")
|
metadata = spotify.generate_metadata("ncs - spectre")
|
||||||
return metadata
|
return metadata
|
||||||
|
|
||||||
def test_len(self, metadata_fixture):
|
def test_len(self, metadata_fixture):
|
||||||
@@ -38,7 +41,7 @@ class TestGenerateMetadata:
|
|||||||
assert metadata_fixture["duration"] == 230.634
|
assert metadata_fixture["duration"] == 230.634
|
||||||
|
|
||||||
|
|
||||||
def test_get_playlists():
|
def test_get_playlists(spotify):
|
||||||
expect_playlist_ids = [
|
expect_playlist_ids = [
|
||||||
"34gWCK8gVeYDPKcctB6BQJ",
|
"34gWCK8gVeYDPKcctB6BQJ",
|
||||||
"04wTU2c2WNQG9XE5oSLYfj",
|
"04wTU2c2WNQG9XE5oSLYfj",
|
||||||
@@ -50,15 +53,15 @@ def test_get_playlists():
|
|||||||
for playlist_id in expect_playlist_ids
|
for playlist_id in expect_playlist_ids
|
||||||
]
|
]
|
||||||
|
|
||||||
playlists = spotify_tools.get_playlists("uqlakumu7wslkoen46s5bulq0")
|
playlists = spotify.get_playlists("uqlakumu7wslkoen46s5bulq0")
|
||||||
assert playlists == expect_playlists
|
assert playlists == expect_playlists
|
||||||
|
|
||||||
|
|
||||||
def test_write_user_playlist(tmpdir, monkeypatch):
|
def test_write_user_playlist(tmpdir, spotify, monkeypatch):
|
||||||
expect_tracks = 17
|
expect_tracks = 17
|
||||||
text_file = os.path.join(str(tmpdir), "test_us.txt")
|
text_file = os.path.join(str(tmpdir), "test_us.txt")
|
||||||
monkeypatch.setattr("builtins.input", lambda x: 1)
|
monkeypatch.setattr("builtins.input", lambda x: 1)
|
||||||
spotify_tools.write_user_playlist("uqlakumu7wslkoen46s5bulq0", text_file)
|
spotify.write_user_playlist("uqlakumu7wslkoen46s5bulq0", text_file)
|
||||||
with open(text_file, "r") as f:
|
with open(text_file, "r") as f:
|
||||||
tracks = len(f.readlines())
|
tracks = len(f.readlines())
|
||||||
assert tracks == expect_tracks
|
assert tracks == expect_tracks
|
||||||
@@ -66,8 +69,8 @@ def test_write_user_playlist(tmpdir, monkeypatch):
|
|||||||
|
|
||||||
class TestFetchPlaylist:
|
class TestFetchPlaylist:
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def playlist_fixture(self):
|
def playlist_fixture(self, spotify):
|
||||||
playlist = spotify_tools.fetch_playlist(
|
playlist = spotify.fetch_playlist(
|
||||||
"https://open.spotify.com/playlist/0fWBMhGh38y0wsYWwmM9Kt"
|
"https://open.spotify.com/playlist/0fWBMhGh38y0wsYWwmM9Kt"
|
||||||
)
|
)
|
||||||
return playlist
|
return playlist
|
||||||
@@ -79,10 +82,10 @@ class TestFetchPlaylist:
|
|||||||
assert playlist_fixture["tracks"]["total"] == 14
|
assert playlist_fixture["tracks"]["total"] == 14
|
||||||
|
|
||||||
|
|
||||||
def test_write_playlist(tmpdir):
|
def test_write_playlist(tmpdir, spotify):
|
||||||
expect_tracks = 14
|
expect_tracks = 14
|
||||||
text_file = os.path.join(str(tmpdir), "test_pl.txt")
|
text_file = os.path.join(str(tmpdir), "test_pl.txt")
|
||||||
spotify_tools.write_playlist(
|
spotify.write_playlist(
|
||||||
"https://open.spotify.com/playlist/0fWBMhGh38y0wsYWwmM9Kt", text_file
|
"https://open.spotify.com/playlist/0fWBMhGh38y0wsYWwmM9Kt", text_file
|
||||||
)
|
)
|
||||||
with open(text_file, "r") as f:
|
with open(text_file, "r") as f:
|
||||||
@@ -93,8 +96,8 @@ def test_write_playlist(tmpdir):
|
|||||||
# XXX: Mock this test off if it fails in future
|
# XXX: Mock this test off if it fails in future
|
||||||
class TestFetchAlbum:
|
class TestFetchAlbum:
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def album_fixture(self):
|
def album_fixture(self, spotify):
|
||||||
album = spotify_tools.fetch_album(
|
album = spotify.fetch_album(
|
||||||
"https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg"
|
"https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg"
|
||||||
)
|
)
|
||||||
return album
|
return album
|
||||||
@@ -109,14 +112,13 @@ class TestFetchAlbum:
|
|||||||
# XXX: Mock this test off if it fails in future
|
# XXX: Mock this test off if it fails in future
|
||||||
class TestFetchAlbumsFromArtist:
|
class TestFetchAlbumsFromArtist:
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def albums_from_artist_fixture(self):
|
def albums_from_artist_fixture(self, spotify):
|
||||||
albums = spotify_tools.fetch_albums_from_artist(
|
albums = spotify.fetch_albums_from_artist(
|
||||||
"https://open.spotify.com/artist/7oPftvlwr6VrsViSDV7fJY"
|
"https://open.spotify.com/artist/7oPftvlwr6VrsViSDV7fJY"
|
||||||
)
|
)
|
||||||
return albums
|
return albums
|
||||||
|
|
||||||
def test_len(self, albums_from_artist_fixture):
|
def test_len(self, albums_from_artist_fixture):
|
||||||
# TODO: Mock this test (failed in #493)
|
|
||||||
assert len(albums_from_artist_fixture) == 52
|
assert len(albums_from_artist_fixture) == 52
|
||||||
|
|
||||||
def test_zeroth_album_name(self, albums_from_artist_fixture):
|
def test_zeroth_album_name(self, albums_from_artist_fixture):
|
||||||
@@ -132,11 +134,10 @@ class TestFetchAlbumsFromArtist:
|
|||||||
assert albums_from_artist_fixture[0]["total_tracks"] == 12
|
assert albums_from_artist_fixture[0]["total_tracks"] == 12
|
||||||
|
|
||||||
|
|
||||||
# TODO: Mock this test (failed in #493)
|
def test_write_all_albums_from_artist(tmpdir, spotify):
|
||||||
def test_write_all_albums_from_artist(tmpdir):
|
|
||||||
expect_tracks = 282
|
expect_tracks = 282
|
||||||
text_file = os.path.join(str(tmpdir), "test_ab.txt")
|
text_file = os.path.join(str(tmpdir), "test_ab.txt")
|
||||||
spotify_tools.write_all_albums_from_artist(
|
spotify.write_all_albums_from_artist(
|
||||||
"https://open.spotify.com/artist/4dpARuHxo51G3z768sgnrY", text_file
|
"https://open.spotify.com/artist/4dpARuHxo51G3z768sgnrY", text_file
|
||||||
)
|
)
|
||||||
with open(text_file, "r") as f:
|
with open(text_file, "r") as f:
|
||||||
@@ -144,10 +145,10 @@ def test_write_all_albums_from_artist(tmpdir):
|
|||||||
assert tracks == expect_tracks
|
assert tracks == expect_tracks
|
||||||
|
|
||||||
|
|
||||||
def test_write_album(tmpdir):
|
def test_write_album(tmpdir, spotify):
|
||||||
expect_tracks = 15
|
expect_tracks = 15
|
||||||
text_file = os.path.join(str(tmpdir), "test_al.txt")
|
text_file = os.path.join(str(tmpdir), "test_al.txt")
|
||||||
spotify_tools.write_album(
|
spotify.write_album(
|
||||||
"https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg", text_file
|
"https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg", text_file
|
||||||
)
|
)
|
||||||
with open(text_file, "r") as f:
|
with open(text_file, "r") as f:
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class TestYouTubeAPIKeys:
|
|||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def metadata_fixture():
|
def metadata_fixture():
|
||||||
metadata = spotify_tools.generate_metadata(TRACK_SEARCH)
|
metadata = spotify_tools.SpotifyAuthorize().generate_metadata(TRACK_SEARCH)
|
||||||
return metadata
|
return metadata
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user