-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #366 from micro-nova/spotifyd
Use Spotifyd instead of vollibrespot to get latest librespot changes
- Loading branch information
Showing
12 changed files
with
259 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
"""A module for interfacing with an MPRIS MediaPlayer2 over dbus.""" | ||
|
||
from dataclasses import dataclass | ||
from enum import Enum, auto | ||
import json | ||
from multiprocessing import Process | ||
import time | ||
from typing import List | ||
from dasbus.connection import SessionMessageBus | ||
|
||
from amplipi import utils | ||
|
||
|
||
METADATA_MAPPINGS = [ | ||
('artist', 'xesam:artist'), | ||
('title', 'xesam:title'), | ||
('art_url', 'mpris:artUrl'), | ||
('album', 'xesam:album') | ||
] | ||
|
||
METADATA_REFRESH_RATE = 0.5 | ||
METADATA_FILE_NAME = "metadata.txt" | ||
|
||
class CommandTypes(Enum): | ||
PLAY = auto() | ||
PAUSE = auto() | ||
NEXT = auto() | ||
PREVIOUS = auto() | ||
|
||
@dataclass | ||
class Metadata: | ||
"""A data class for storing metadata on a song.""" | ||
artist: str = '' | ||
title: str = '' | ||
art_url: str = '' | ||
album: str = '' | ||
state: str = '' | ||
|
||
|
||
class MPRIS: | ||
"""A class for interfacing with an MPRIS MediaPlayer2 over dbus.""" | ||
|
||
def __init__(self, service_suffix, src) -> None: | ||
self.mpris = SessionMessageBus().get_proxy( | ||
service_name = f"org.mpris.MediaPlayer2.{service_suffix}", | ||
object_path = "/org/mpris/MediaPlayer2", | ||
interface_name = "org.mpris.MediaPlayer2.Player" | ||
) | ||
|
||
self.capabilities: List[CommandTypes] = [] | ||
|
||
self.service_suffix = service_suffix | ||
self.src = src | ||
self.metadata_path = f'{utils.get_folder("config")}/srcs/{self.src}/{METADATA_FILE_NAME}' | ||
|
||
try: | ||
with open(self.metadata_path, "w", encoding='utf-8') as f: | ||
m = Metadata() | ||
m.state = "Stopped" | ||
json.dump(m.__dict__, f) | ||
except Exception as e: | ||
print (f'Exception clearing metadata file: {e}') | ||
|
||
self.metadata_process = Process(target=self._metadata_reader) | ||
self.metadata_process.start() | ||
|
||
def play(self) -> None: | ||
"""Plays.""" | ||
self.mpris.Play() | ||
|
||
def pause(self) -> None: | ||
"""Pauses.""" | ||
self.mpris.Pause() | ||
|
||
def next(self) -> None: | ||
"""Skips song.""" | ||
self.mpris.Next() | ||
|
||
def previous(self) -> None: | ||
"""Goes back a song.""" | ||
self.mpris.Previous() | ||
|
||
def play_pause(self) -> None: | ||
"""Plays or pauses depending on current state.""" | ||
self.mpris.PlayPause() | ||
|
||
def _load_metadata(self): | ||
try: | ||
with open(self.metadata_path, 'r', encoding='utf-8') as f: | ||
metadata_dict = json.load(f) | ||
metadata_obj = Metadata() | ||
|
||
for k in metadata_dict.keys(): | ||
metadata_obj.__dict__[k] = metadata_dict[k] | ||
|
||
return metadata_obj | ||
except Exception as e: | ||
print(f"mpris loading metadata at {self.metadata_path} failed: {e}") | ||
return None | ||
|
||
|
||
def metadata(self) -> Metadata: | ||
"""Returns metadata from MPRIS.""" | ||
return self._load_metadata() | ||
|
||
def is_playing(self) -> bool: | ||
"""Playing?""" | ||
return self._load_metadata().state == 'Playing' | ||
|
||
def is_stopped(self) -> bool: | ||
"""Stopped?""" | ||
return self._load_metadata().state == 'Stopped' | ||
|
||
def get_capabilities(self) -> List[CommandTypes]: | ||
"""Returns a list of supported commands.""" | ||
|
||
if len(self.capabilities) == 0: | ||
|
||
if self.mpris.CanPlay: | ||
self.capabilities.append(CommandTypes.PLAY) | ||
|
||
if self.mpris.CanPause: | ||
self.capabilities.append(CommandTypes.PAUSE) | ||
|
||
if self.mpris.CanGoNext: | ||
self.capabilities.append(CommandTypes.NEXT) | ||
|
||
if self.mpris.CanGoPrevious: | ||
self.capabilities.append(CommandTypes.PREVIOUS) | ||
|
||
return self.capabilities | ||
|
||
def __del__(self): | ||
try: | ||
self.metadata_process.kill() | ||
except: | ||
pass | ||
|
||
def _metadata_reader(self): | ||
"""Method run by the metadata process, also handles playing/paused.""" | ||
|
||
mpris = SessionMessageBus().get_proxy( | ||
service_name = f"org.mpris.MediaPlayer2.{self.service_suffix}", | ||
object_path = "/org/mpris/MediaPlayer2", | ||
interface_name = "org.mpris.MediaPlayer2.Player" | ||
) | ||
|
||
m = Metadata() | ||
m.state = 'Stopped' | ||
|
||
last_sent = m.__dict__ | ||
|
||
while True: | ||
try: | ||
raw_metadata = mpris.Metadata | ||
metadata = {} | ||
|
||
for mapping in METADATA_MAPPINGS: | ||
try: | ||
metadata[mapping[0]] = str(raw_metadata[mapping[1]]).strip("[]'") | ||
except KeyError: | ||
pass | ||
|
||
metadata['state'] = mpris.PlaybackStatus.strip("'") | ||
|
||
if metadata != last_sent: | ||
last_sent = metadata | ||
with open(self.metadata_path, 'w', encoding='utf-8') as metadata_file: | ||
json.dump(metadata, metadata_file) | ||
|
||
except: | ||
pass | ||
time.sleep(1.0/METADATA_REFRESH_RATE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.