Filter unwanted text from Spotify URLs when extracting information (#394)

* Split unwanted URL part

* Convert get_splits() -> extract_spotify_id()

* Add tests for extract_spotify_id()

* Extract Spotify artist ID when fetching artist albums
This commit is contained in:
Ritiek Malhotra
2018-10-26 18:29:29 +05:30
committed by GitHub
parent 94dc27a77b
commit be4bb25c96
3 changed files with 102 additions and 38 deletions

View File

@@ -89,9 +89,7 @@ def format_string(string_format, tags, slugification=False, force_spaces=False):
format_tags[11] = tags["external_ids"]["isrc"]
format_tags_sanitized = {
k: sanitize_title(str(v), ok="'-_()[]{}")
if slugification
else str(v)
k: sanitize_title(str(v), ok="'-_()[]{}") if slugification else str(v)
for k, v in format_tags.items()
}
@@ -157,14 +155,30 @@ def get_sec(time_str):
return sec
def get_splits(url):
if "/" in url:
if url.endswith("/"):
url = url[:-1]
splits = url.split("/")
def extract_spotify_id(raw_string):
"""
Returns a Spotify ID of a playlist, album, etc. after extracting
it from a given HTTP URL or Spotify URI.
"""
if "/" in raw_string:
# Input string is an HTTP URL
if raw_string.endswith("/"):
raw_string = raw_string[:-1]
# We need to manually trim additional text from HTTP URLs
# We could skip this if https://github.com/plamere/spotipy/pull/324
# gets merged,
to_trim = raw_string.find("?")
if not to_trim == -1:
raw_string = raw_string[:to_trim]
splits = raw_string.split("/")
else:
splits = url.split(":")
return splits
# Input string is a Spotify URI
splits = raw_string.split(":")
spotify_id = splits[-1]
return spotify_id
def get_unique_tracks(text_file):
@@ -183,6 +197,7 @@ def get_unique_tracks(text_file):
return lines
# a hacky way to get user's localized music directory
# (thanks @linusg, issue #203)
def get_music_dir():
@@ -203,9 +218,14 @@ def get_music_dir():
# Windows / Cygwin
# Queries registry for 'My Music' folder path (as this can be changed)
if 'win' in sys.platform:
if "win" in sys.platform:
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", 0, winreg.KEY_ALL_ACCESS)
key = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders",
0,
winreg.KEY_ALL_ACCESS,
)
return winreg.QueryValueEx(key, "My Music")[0]
except (FileNotFoundError, NameError):
pass