"""Util functions.""" import re # Match Spotify artist URI # e.g. spotify:artist:165ZgPlLkK7bf5bDoFc6Sb # or Spotify artist URL # e.g. https://open.spotify.com/artist/165ZgPlLkK7bf5bDoFc6Sb SPOTIFY_REGEX = re.compile( r'(?:(?:spotify:artist:)|(?:https:\/\/open.spotify.com\/artist\/))(\w+)\/?$') # noqa def get_spotify_id(spotify_url: str) -> str: """Parse spotify_id from spotify_url. Args: spotify_url (str): spotify artist page url. Returns: str: spotify id """ if not spotify_url: return None spotify_url = spotify_url.strip() # remove url params e.g. ?si=fy_lY-vBQhGAGeoNuh0iZg&dl_branch=1 url_params = spotify_url.find('?') if url_params > 0: spotify_url = spotify_url[:url_params] match = SPOTIFY_REGEX.match(spotify_url) if match: return match.group(1)