Merge pull request #502 from ritiek/spotify-creds

Spotify Credentials from file
This commit is contained in:
Ritiek Malhotra
2019-02-26 20:50:12 -08:00
committed by GitHub
8 changed files with 259 additions and 247 deletions

View File

@@ -34,6 +34,8 @@ default_conf = {
"skip": None, "skip": None,
"write-successful": None, "write-successful": None,
"log-level": "INFO", "log-level": "INFO",
"spotify_client_id": "4fe3fecfe5334023a1472516cc99d805",
"spotify_client_secret": "0f02b7c483c04257984695007a4a8d5c"
} }
} }
@@ -147,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(
@@ -260,6 +262,18 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True):
default=config["write-successful"], default=config["write-successful"],
help="path to file to write successful tracks to", help="path to file to write successful tracks to",
) )
parser.add_argument(
"-sci",
"--spotify-client-id",
default=config["spotify_client_id"],
help=argparse.SUPPRESS
)
parser.add_argument(
"-scs",
"--spotify-client-secret",
default=config["spotify_client_secret"],
help=argparse.SUPPRESS
)
parser.add_argument( parser.add_argument(
"-c", "--config", default=None, help="path to custom config.yml file" "-c", "--config", default=None, help="path to custom config.yml file"
) )

View File

@@ -148,6 +148,7 @@ 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"]
if meta_tags["album"]["artists"][0]["name"]:
audiofile[preset["albumartist"]] = 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"]

View File

@@ -51,6 +51,7 @@ def main():
internals.filter_path(const.args.folder) internals.filter_path(const.args.folder)
youtube_tools.set_api_key() youtube_tools.set_api_key()
spotify = spotify_tools.SpotifyAuthorize()
logzero.setup_default_logger(formatter=const._formatter, level=const.args.log_level) logzero.setup_default_logger(formatter=const._formatter, level=const.args.log_level)

View File

@@ -12,45 +12,47 @@ import os
from spotdl import const from spotdl import const
from spotdl import internals from spotdl import internals
# token = generate_token()
# spotify = spotipy.Spotify(auth=token)
def generate_token():
""" Generate the token. Please respect these credentials :) """ class SpotifyAuthorize:
""" Class to handle all interactions with spotipy instance. """
def __init__(self):
self.client_id = const.args.spotify_client_id
self.client_secret = const.args.spotify_client_secret
token = self.generate_token()
self.spotify = spotipy.Spotify(auth=token)
def generate_token(self):
""" Generate the token. """
credentials = oauth2.SpotifyClientCredentials( credentials = oauth2.SpotifyClientCredentials(
client_id="4fe3fecfe5334023a1472516cc99d805", client_id=self.client_id,
client_secret="0f02b7c483c04257984695007a4a8d5c", client_secret=self.client_secret,
) )
token = credentials.get_access_token() token = credentials.get_access_token()
return token return token
def refresh_token(self):
""" Refresh expired token. """
new_token = self.generate_token()
self.spotify = spotipy.Spotify(auth=new_token)
def refresh_token(): def generate_metadata(self, raw_song):
""" Refresh expired token"""
global spotify
new_token = generate_token()
spotify = spotipy.Spotify(auth=new_token)
# token is mandatory when using Spotify's API
# https://developer.spotify.com/news-stories/2017/01/27/removing-unauthenticated-calls-to-the-web-api/
_token = generate_token()
spotify = spotipy.Spotify(auth=_token)
def generate_metadata(raw_song):
""" Fetch a song's metadata from Spotify. """ """ Fetch a song's metadata from Spotify. """
if internals.is_spotify(raw_song): if internals.is_spotify(raw_song):
# fetch track information directly if it is spotify link # fetch track information directly if it is spotify link
log.debug("Fetching metadata for given track URL") log.debug("Fetching metadata for given track URL")
meta_tags = spotify.track(raw_song) meta_tags = self.spotify.track(raw_song)
else: else:
# otherwise search on spotify and fetch information from first result # otherwise search on spotify and fetch information from first result
log.debug('Searching for "{}" on Spotify'.format(raw_song)) log.debug('Searching for "{}" on Spotify'.format(raw_song))
try: try:
meta_tags = spotify.search(raw_song, limit=1)["tracks"]["items"][0] meta_tags = self.spotify.search(raw_song, limit=1)["tracks"]["items"][0]
except IndexError: except IndexError:
return None return None
artist = spotify.artist(meta_tags["artists"][0]["id"]) artist = self.spotify.artist(meta_tags["artists"][0]["id"])
album = spotify.album(meta_tags["album"]["id"]) album = self.spotify.album(meta_tags["album"]["id"])
try: try:
meta_tags[u"genre"] = titlecase(artist["genres"][0]) meta_tags[u"genre"] = titlecase(artist["genres"][0])
@@ -90,10 +92,15 @@ def generate_metadata(raw_song):
log.debug(pprint.pformat(meta_tags)) log.debug(pprint.pformat(meta_tags))
return meta_tags return meta_tags
def write_user_playlist(self, username, text_file=None):
""" Write user playlists to text_file """
links = self.get_playlists(username=username)
playlist = internals.input_link(links)
return self.write_playlist(playlist, text_file)
def get_playlists(username): def get_playlists(self, username):
""" Fetch user playlists when using the -u option. """ """ Fetch user playlists when using the -u option. """
playlists = spotify.user_playlists(username) playlists = self.spotify.user_playlists(username)
links = [] links = []
check = 1 check = 1
@@ -112,20 +119,13 @@ def get_playlists(username):
links.append(playlist_url) links.append(playlist_url)
check += 1 check += 1
if playlists["next"]: if playlists["next"]:
playlists = spotify.next(playlists) playlists = self.spotify.next(playlists)
else: else:
break break
return links return links
def fetch_playlist(self, playlist):
def write_user_playlist(username, text_file=None):
links = get_playlists(username=username)
playlist = internals.input_link(links)
return write_playlist(playlist, text_file)
def fetch_playlist(playlist):
try: try:
playlist_id = internals.extract_spotify_id(playlist) playlist_id = internals.extract_spotify_id(playlist)
except IndexError: except IndexError:
@@ -133,7 +133,7 @@ def fetch_playlist(playlist):
log.error("The provided playlist URL is not in a recognized format!") log.error("The provided playlist URL is not in a recognized format!")
sys.exit(10) sys.exit(10)
try: try:
results = spotify.user_playlist( results = self.spotify.user_playlist(
user=None, playlist_id=playlist_id, fields="tracks,next,name" user=None, playlist_id=playlist_id, fields="tracks,next,name"
) )
except spotipy.client.SpotifyException: except spotipy.client.SpotifyException:
@@ -143,23 +143,19 @@ def fetch_playlist(playlist):
return results return results
def write_playlist(self, playlist_url, text_file=None):
def write_playlist(playlist_url, text_file=None): playlist = self.fetch_playlist(playlist_url)
playlist = fetch_playlist(playlist_url)
tracks = playlist["tracks"] tracks = playlist["tracks"]
if not text_file: if not text_file:
text_file = u"{0}.txt".format(slugify(playlist["name"], ok="-_()[]{}")) text_file = u"{0}.txt".format(slugify(playlist["name"], ok="-_()[]{}"))
filepath = os.path.join(const.args.folder if const.args.folder else "", text_file) return self.write_tracks(tracks, text_file)
return write_tracks(tracks, filepath)
def fetch_album(self, album):
def fetch_album(album):
album_id = internals.extract_spotify_id(album) album_id = internals.extract_spotify_id(album)
album = spotify.album(album_id) album = self.spotify.album(album_id)
return album return album
def fetch_albums_from_artist(self, artist_url, album_type=None):
def fetch_albums_from_artist(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
@@ -172,19 +168,19 @@ def fetch_albums_from_artist(artist_url, album_type=None):
# fetching artist's albums limitting the results to the US to avoid duplicate # fetching artist's albums limitting the results to the US to avoid duplicate
# albums from multiple markets # albums from multiple markets
artist_id = internals.extract_spotify_id(artist_url) artist_id = internals.extract_spotify_id(artist_url)
results = spotify.artist_albums(artist_id, album_type=album_type, country="US") results = self.spotify.artist_albums(artist_id, album_type=album_type, country="US")
albums = results["items"] albums = results["items"]
# indexing all pages of results # indexing all pages of results
while results["next"]: while results["next"]:
results = spotify.next(results) results = self.spotify.next(results)
albums.extend(results["items"]) albums.extend(results["items"])
return albums return albums
def write_all_albums_from_artist(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
current working directory called [ARTIST].txt, where [ARTIST] is the artist current working directory called [ARTIST].txt, where [ARTIST] is the artist
@@ -196,7 +192,7 @@ def write_all_albums_from_artist(artist_url, text_file=None):
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 = fetch_albums_from_artist(artist_url, album_type=None) 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
@@ -206,19 +202,16 @@ def write_all_albums_from_artist(artist_url, text_file=None):
for album in albums: for album in albums:
# logging album name # logging album name
log.info("Fetching album: " + album["name"]) log.info("Fetching album: " + album["name"])
write_album(album_base_url + album["id"], text_file=text_file) self.write_album(album_base_url + album["id"], text_file=text_file)
def write_album(self, album_url, text_file=None):
def write_album(album_url, text_file=None): album = self.fetch_album(album_url)
album = fetch_album(album_url) tracks = self.spotify.album_tracks(album["id"])
tracks = spotify.album_tracks(album["id"])
if not text_file: if not text_file:
text_file = u"{0}.txt".format(slugify(album["name"], ok="-_()[]{}")) text_file = u"{0}.txt".format(slugify(album["name"], ok="-_()[]{}"))
filepath = os.path.join(const.args.folder if const.args.folder else "", text_file) return self.write_tracks(tracks, text_file)
return write_tracks(tracks, filepath)
def write_tracks(self, tracks, text_file):
def write_tracks(tracks, text_file):
log.info(u"Writing {0} tracks to {1}".format(tracks["total"], text_file)) log.info(u"Writing {0} tracks to {1}".format(tracks["total"], text_file))
track_urls = [] track_urls = []
with open(text_file, "a") as file_out: with open(text_file, "a") as file_out:
@@ -242,7 +235,7 @@ def write_tracks(tracks, text_file):
# 1 page = 50 results # 1 page = 50 results
# check if there are more pages # check if there are more pages
if tracks["next"]: if tracks["next"]:
tracks = spotify.next(tracks) tracks = self.spotify.next(tracks)
else: else:
break break
return track_urls return track_urls

View File

@@ -48,6 +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
spotify = spotify_tools.SpotifyAuthorize()
def fallback_metadata(meta_tags): def fallback_metadata(meta_tags):
@@ -67,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
@@ -83,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)
@@ -97,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",

View File

@@ -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

View File

@@ -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:

View File

@@ -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