diff --git a/sllo/Classes.py b/sllo/Classes.py index 1dcea49..6e77d7a 100644 --- a/sllo/Classes.py +++ b/sllo/Classes.py @@ -1,4 +1,8 @@ from enum import Enum +from sys import orig_argv as argv +from subprocess import run as execute +from subprocess import PIPE +from subprocess import Popen as subExecute class SpotifyLink: @@ -30,9 +34,61 @@ class SpotifyLink: return self.__getsmth(self.SMTH.CTYPE) def __getid(self) -> str: + """ + :return: Spotify content id + :rtype: str + """ return self.__getsmth(self.SMTH.CID) - def uri(self): + def uri(self) -> str: + """ + :return: Spotify URI + :rtype: str + """ ctype = self.__gettype() cid = self.__getid() return f"spotify:{ctype}:{cid}" + + +class Interface: + + @staticmethod + def __getlink() -> SpotifyLink: + """ + :rtype: SpotifyLink + """ + args = argv[2:] + if args != 1: + raise ValueError("Bad usage") + return SpotifyLink(args[0]) + + @staticmethod + def __check() -> bool: + """ + :rtype: bool + """ + stoud = execute("dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply " + "/org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep spotify", shell=True, text=True, + stdout=PIPE).stdout + if stoud == "": + return True + + def open(self) -> None: + """ + :rtype: None + """ + uri = self.__getlink().uri() + if self.__check(): + subExecute(["spotify", f"--uri={uri}"]) + else: + execute(f"dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 " + f"org.mpris.MediaPlayer2.Player.OpenUri string:'{uri}'", shell=True) + + @staticmethod + def help() -> None: + """ + :rtype: None + """ + msg = ("\nUsage:\nslopen 'https://open.spotify.com//'\n\nExample:\n" + "slopen 'https://open.spotify.com/track/4PTG3Z6ehGkBFwjybzWkR8?si=a4ec356b33fd49d3'") + print(msg)