Refactor Cocoa stuff into a 'receiver'
The idea here is that there are other places that might want to know what's playing, besides MPNowPlayingInfoCenter. For example, to expose the now playing info to Übersicht efficiently, it needs to be available from a web browser, ideally using WebSockets. So there could be a receiver that runs a small WebSockets server and sends out now playing info to anyone who connects. Additionally, I hope to write receivers for MPRIS and for the System Media Transport Controls on Windows, making mpd-now-playable equally useful across all platforms. None of this is implemented yet, of course, but I hope to get the WebSockets receiver done pretty soon! I'm going to keep the default behaviour unchanged. Unless you explicitly configure different receivers in config.toml, mpd-now-playable will just behave as an MPNowPlayingInfoCenter integration as it's always done.
This commit is contained in:
parent
27d8c37139
commit
00ba34bd0b
12 changed files with 214 additions and 38 deletions
50
src/mpd_now_playable/receivers/cocoa/persistent_id.py
Normal file
50
src/mpd_now_playable/receivers/cocoa/persistent_id.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
from hashlib import blake2b
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
from uuid import UUID
|
||||
|
||||
from ...song import Song
|
||||
|
||||
# The maximum size for a BLAKE2b "person" value is sixteen bytes, so we need to be concise.
|
||||
HASH_PERSON_PREFIX: Final = b"mnp.mac."
|
||||
TRACKID_HASH_PERSON: Final = HASH_PERSON_PREFIX + b"mb_tid"
|
||||
RELEASETRACKID_HASH_PERSON: Final = HASH_PERSON_PREFIX + b"mb_rtid"
|
||||
FILE_HASH_PERSON: Final = HASH_PERSON_PREFIX + b"f"
|
||||
|
||||
PERSISTENT_ID_BITS: Final = 64
|
||||
PERSISTENT_ID_BYTES: Final = PERSISTENT_ID_BITS // 8
|
||||
|
||||
|
||||
def digest_trackid(trackid: UUID) -> bytes:
|
||||
return blake2b(
|
||||
trackid.bytes, digest_size=PERSISTENT_ID_BYTES, person=TRACKID_HASH_PERSON
|
||||
).digest()
|
||||
|
||||
|
||||
def digest_releasetrackid(trackid: UUID) -> bytes:
|
||||
return blake2b(
|
||||
trackid.bytes,
|
||||
digest_size=PERSISTENT_ID_BYTES,
|
||||
person=RELEASETRACKID_HASH_PERSON,
|
||||
).digest()
|
||||
|
||||
|
||||
def digest_file_uri(file: Path) -> bytes:
|
||||
return blake2b(
|
||||
bytes(file), digest_size=PERSISTENT_ID_BYTES, person=FILE_HASH_PERSON
|
||||
).digest()
|
||||
|
||||
|
||||
# The MPMediaItemPropertyPersistentID is only 64 bits, while a UUID is 128
|
||||
# bits and not all tracks will even have their MusicBrainz track ID included.
|
||||
# To work around this, we compute a BLAKE2 hash from the UUID, or failing
|
||||
# that from the file URI. BLAKE2 can be customised to different digest sizes,
|
||||
# making it perfect for this problem.
|
||||
def song_to_persistent_id(song: Song) -> int:
|
||||
if song.musicbrainz_trackid:
|
||||
hashed_id = digest_trackid(song.musicbrainz_trackid)
|
||||
elif song.musicbrainz_releasetrackid:
|
||||
hashed_id = digest_releasetrackid(song.musicbrainz_releasetrackid)
|
||||
else:
|
||||
hashed_id = digest_file_uri(song.file)
|
||||
return int.from_bytes(hashed_id)
|
||||
Loading…
Add table
Add a link
Reference in a new issue