From 54c145024b7029de77e69a10eb8a1520b43a0a02 Mon Sep 17 00:00:00 2001 From: zeroday0619 Date: Sun, 17 Jan 2021 15:32:33 +0900 Subject: [PATCH] feat: type check --- setup.py | 2 +- spotify_uri/__init__.py | 20 ++++++++++---------- spotify_uri/album.py | 9 +++++++++ spotify_uri/artist.py | 9 +++++++++ spotify_uri/episode.py | 9 +++++++++ spotify_uri/local.py | 10 ++++++++++ spotify_uri/parse.py | 6 +++++- spotify_uri/playlist.py | 12 +++++++++++- spotify_uri/search.py | 9 +++++++++ spotify_uri/spotify.py | 13 +++++++++++-- spotify_uri/track.py | 9 +++++++++ spotify_uri/user.py | 9 +++++++++ 12 files changed, 102 insertions(+), 15 deletions(-) diff --git a/setup.py b/setup.py index 0eca067..492631b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='spotify_uri', - version='1.0.0', + version='1.0.1', description='This is a project that ported @TooTallNate/spotify-uri to Python.', author='zeroday0619', author_email='zeroday0619@kakao.com', diff --git a/spotify_uri/__init__.py b/spotify_uri/__init__.py index 8a43641..c88de61 100644 --- a/spotify_uri/__init__.py +++ b/spotify_uri/__init__.py @@ -1,7 +1,7 @@ -__version__ = "1.0.0" +__version__ = "1.0.1" -from spotify_uri.parse import parse +from spotify_uri.parse import parse as _parse from spotify_uri.search import Search as _Search from spotify_uri.local import Local as _Local from spotify_uri.playlist import Playlist as _Playlist @@ -12,25 +12,25 @@ from spotify_uri.spotify import SpotifyUri -def parseSpotifyUri(uri: str): - return parse(uri) +def parse(uri: str): + return _parse(uri) def formatURI(_input: str) -> str: - _uri = parse(_input) + _uri: SpotifyUri = parse(_input) if bool(type(_input) == str) else _input return _uri.toURI() def formatEmbedURL(_input: str) -> str: - _uri = parse(_input) + _uri: SpotifyUri = parse(_input) if bool(type(_input) == str) else _input return _uri.toEmbedURL() -def formatOpenURL(_input: str): - _uri = parse(_input) +def formatOpenURL(_input: str) -> str: + _uri: SpotifyUri = parse(_input) if bool(type(_input) == str) else _input return _uri.toOpenURL() -def formatPlayURL(_input: str): - _uri = parse(_input) +def formatPlayURL(_input: str) -> str: + _uri: SpotifyUri = parse(_input) if bool(type(_input) == str) else _input return _uri.toPlayURL() diff --git a/spotify_uri/album.py b/spotify_uri/album.py index df2a474..8b880de 100644 --- a/spotify_uri/album.py +++ b/spotify_uri/album.py @@ -1,3 +1,4 @@ +from typing import Any from spotify_uri.spotify import SpotifyUri from spotify_uri.util import encode @@ -8,6 +9,14 @@ def __init__(self, uri: str, _id: str) -> None: self.id = _id super(Album, self).__init__(uri) + @staticmethod + def is_(v: Any) -> bool: + x = v is Album + if x: + return v.type == "album" + else: + return False + def toURI(self) -> str: return f"spotify:{self.type}:{encode(self.id)}" diff --git a/spotify_uri/artist.py b/spotify_uri/artist.py index 14246a7..0aeb4aa 100644 --- a/spotify_uri/artist.py +++ b/spotify_uri/artist.py @@ -1,3 +1,4 @@ +from typing import Any from spotify_uri.spotify import SpotifyUri from spotify_uri.util import encode @@ -8,6 +9,14 @@ def __init__(self, uri: str, _id: str) -> None: self.id = _id super(Artist, self).__init__(uri) + @staticmethod + def is_(v: Any) -> bool: + x = v is Artist + if x: + return v.type == "artist" + else: + return False + def toURI(self) -> str: return f"spotify:{self.type}:${encode(self.id)}" diff --git a/spotify_uri/episode.py b/spotify_uri/episode.py index 9881a86..c42f3fc 100644 --- a/spotify_uri/episode.py +++ b/spotify_uri/episode.py @@ -1,3 +1,4 @@ +from typing import Any from spotify_uri.spotify import SpotifyUri from spotify_uri.util import encode @@ -8,6 +9,14 @@ def __init__(self, uri: str, _id: str) -> None: self.id = _id super(Episode, self).__init__(uri) + @staticmethod + def is_(v: Any) -> bool: + x = v is Episode + if x: + return v.type == "episode" + else: + return False + def toURI(self) -> str: return f"spotify:{self.type}:{encode(self.id)}" diff --git a/spotify_uri/local.py b/spotify_uri/local.py index 70e6bdf..3ce20d9 100644 --- a/spotify_uri/local.py +++ b/spotify_uri/local.py @@ -1,15 +1,25 @@ +from typing import Any from spotify_uri.spotify import SpotifyUri from spotify_uri.util import encode class Local(SpotifyUri): def __init__(self, uri: str, artist: str, album: str, track: str, seconds: int) -> None: + self.type = "local" self.artist = artist self.album = album self.track = track self.seconds = seconds super(Local, self).__init__(uri) + @staticmethod + def is_(v: Any) -> bool: + x = v is Local + if x: + return v.type == "local" + else: + return False + def toURI(self) -> str: return f"spotify:local:{encode(self.artist)}:{encode(self.album)}:{encode(self.track)}:{self.seconds}" diff --git a/spotify_uri/parse.py b/spotify_uri/parse.py index b50e3ae..adca783 100644 --- a/spotify_uri/parse.py +++ b/spotify_uri/parse.py @@ -9,6 +9,7 @@ from spotify_uri.episode import Episode from spotify_uri.user import User from spotify_uri.util import decode +from spotify_uri.spotify import SpotifyUri def parse(_input: str): @@ -19,11 +20,14 @@ def parse(_input: str): :return: :rtype: """ - uri = _input + uri = _input.uri if SpotifyUri.is_(_input) else _input parsed = urlparse(uri) if parsed.hostname == "embed.spotify.com": parsedQs = dict(parse_qs(parsed.query)) + + if bool(type(parsedQs.get("uri")[0]) != str): + raise TypeError return parse(parsedQs.get("uri")[0]) if parsed.scheme == "spotify": diff --git a/spotify_uri/playlist.py b/spotify_uri/playlist.py index b738644..63307d0 100644 --- a/spotify_uri/playlist.py +++ b/spotify_uri/playlist.py @@ -1,13 +1,23 @@ +from typing import Any from spotify_uri.spotify import SpotifyUri from spotify_uri.util import encode class Playlist(SpotifyUri): - def __init__(self, uri: str, _id: str = None, user: str = None): + def __init__(self, uri: str, _id: str = None, user: str = None) -> None: + self.type = "playlist" self.id = _id self.user = user super(Playlist, self).__init__(uri) + @staticmethod + def is_(v: Any) -> bool: + x = v is Playlist + if x: + return v.type == "playlist" + else: + return False + def toURI(self) -> str: if self.user: if self.id == "starred": diff --git a/spotify_uri/search.py b/spotify_uri/search.py index b3eb4f7..77d842d 100644 --- a/spotify_uri/search.py +++ b/spotify_uri/search.py @@ -1,3 +1,4 @@ +from typing import Any from spotify_uri.spotify import SpotifyUri from spotify_uri.util import encode @@ -8,6 +9,14 @@ def __init__(self, uri: str, query: str) -> None: self.query = query super(Search, self).__init__(uri) + @staticmethod + def is_(v: Any) -> bool: + x = v is Search + if x: + return v.type == "search" + else: + return False + def toURI(self) -> str: return f"spotify:search:{encode(self.query)}" diff --git a/spotify_uri/spotify.py b/spotify_uri/spotify.py index dd9bef0..a5bb4a4 100644 --- a/spotify_uri/spotify.py +++ b/spotify_uri/spotify.py @@ -1,9 +1,10 @@ +from typing import Any from abc import ABC, abstractmethod class SpotifyUri(ABC): - def __init__(self, uri: str): - self.uri = uri + def __init__(self, uri: str) -> None: + self.uri: str = uri @abstractmethod def toURL(self) -> str: @@ -13,6 +14,14 @@ def toURL(self) -> str: def toURI(self) -> str: pass + @staticmethod + def is_(v: Any) -> bool: + x = v is SpotifyUri + if x: + return type(v.uri) == str + else: + return False + def toEmbedURL(self) -> str: embed_url = f"https://embed.spotify.com/?uri={self.toURI()}" return embed_url diff --git a/spotify_uri/track.py b/spotify_uri/track.py index 7ef3f15..3fffcc3 100644 --- a/spotify_uri/track.py +++ b/spotify_uri/track.py @@ -1,3 +1,4 @@ +from typing import Any from spotify_uri.spotify import SpotifyUri from spotify_uri.util import encode @@ -8,6 +9,14 @@ def __init__(self, uri: str, _id: str) -> None: self.id = _id super(Track, self).__init__(uri) + @staticmethod + def is_(v: Any) -> bool: + x = v is Track + if x: + return v.type == "track" + else: + return False + def toURI(self) -> str: return f"spotify:{self.type}:{encode(self.id)}" diff --git a/spotify_uri/user.py b/spotify_uri/user.py index 7bbd550..dac095d 100644 --- a/spotify_uri/user.py +++ b/spotify_uri/user.py @@ -1,3 +1,4 @@ +from typing import Any from spotify_uri.spotify import SpotifyUri from spotify_uri.util import encode @@ -8,6 +9,14 @@ def __init__(self, uri: str, user: str) -> None: self.user = user super(User, self).__init__(uri) + @staticmethod + def is_(v: Any) -> bool: + x = v is User + if x: + return v.type == "user" + else: + return False + def toURI(self) -> str: return f"spotify:{self.type}:{encode(self.user)}"