Skip to content

Commit

Permalink
feat: type check
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroday0619 committed Jan 17, 2021
1 parent 39a2eaa commit 54c1450
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 15 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
Expand Down
20 changes: 10 additions & 10 deletions spotify_uri/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
9 changes: 9 additions & 0 deletions spotify_uri/album.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from spotify_uri.spotify import SpotifyUri
from spotify_uri.util import encode

Expand All @@ -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)}"

Expand Down
9 changes: 9 additions & 0 deletions spotify_uri/artist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from spotify_uri.spotify import SpotifyUri
from spotify_uri.util import encode

Expand All @@ -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)}"

Expand Down
9 changes: 9 additions & 0 deletions spotify_uri/episode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from spotify_uri.spotify import SpotifyUri
from spotify_uri.util import encode

Expand All @@ -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)}"

Expand Down
10 changes: 10 additions & 0 deletions spotify_uri/local.py
Original file line number Diff line number Diff line change
@@ -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}"

Expand Down
6 changes: 5 additions & 1 deletion spotify_uri/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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":
Expand Down
12 changes: 11 additions & 1 deletion spotify_uri/playlist.py
Original file line number Diff line number Diff line change
@@ -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":
Expand Down
9 changes: 9 additions & 0 deletions spotify_uri/search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from spotify_uri.spotify import SpotifyUri
from spotify_uri.util import encode

Expand All @@ -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)}"

Expand Down
13 changes: 11 additions & 2 deletions spotify_uri/spotify.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spotify_uri/track.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from spotify_uri.spotify import SpotifyUri
from spotify_uri.util import encode

Expand All @@ -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)}"

Expand Down
9 changes: 9 additions & 0 deletions spotify_uri/user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from spotify_uri.spotify import SpotifyUri
from spotify_uri.util import encode

Expand All @@ -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)}"

Expand Down

0 comments on commit 54c1450

Please sign in to comment.