-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 649832b
Showing
6 changed files
with
121 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.env | ||
poetry.lock | ||
library.json |
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 @@ | ||
SONOS_HOST="IP address of your Sonos Zone Player" |
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,37 @@ | ||
# My jukebox | ||
|
||
CLI to play an album through Sonos speakers. | ||
|
||
🎯 The aim is to be able to play music on speakers using NFC tags. | ||
|
||
🚧 For the moment: | ||
- artist, album and URI must be pre-populated in a JSON file | ||
- only works with Spotify URIs | ||
|
||
💡 Inspired by: | ||
- https://github.com/hankhank10/vinylemulator | ||
- https://github.com/zacharycohn/jukebox | ||
|
||
## Install | ||
|
||
Installing dependencies with [Poetry](https://python-poetry.org) | ||
```shell | ||
poetry install | ||
``` | ||
|
||
Add `SONOS_HOST` to env with IP address of your Sonos Zone Player. To do this you can use a `.env` file with [poetry-dotenv-plugin](https://github.com/mpeteuil/poetry-dotenv-plugin) | ||
|
||
Create a `library.json` file (`cp sample_library.json library.json`) and complete it with the desired artists and albums. | ||
|
||
## Usage | ||
|
||
Show help message | ||
```shell | ||
poetry run python app.py --help | ||
``` | ||
|
||
Play a specific album | ||
```shell | ||
poetry run python app.py play --artist "Your favorite artist" --album "Your favorite album by this artist" | ||
``` | ||
Artist and album must be entered in the library's JSON file. This file can be specified with the `--library` parameter. |
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,51 @@ | ||
#!python | ||
import argparse | ||
import json | ||
import os | ||
from pprint import pprint | ||
|
||
from soco import SoCo | ||
from soco.plugins.sharelink import ShareLinkPlugin | ||
|
||
|
||
def play(sharelink, uri): | ||
sharelink.soco.clear_queue() | ||
_ = sharelink.add_share_link_to_queue(uri, position=1, as_next=True) | ||
sharelink.soco.play_from_queue(index=0, start=True) | ||
|
||
|
||
def get_args(): | ||
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument("-l", "--library", default="library.json", help="path to the library JSON file") | ||
subparsers = parser.add_subparsers(required=True, dest="command", help="subcommands") | ||
play_parser = subparsers.add_parser("play", help="play specific songs") | ||
play_parser.add_argument("--artist", required=True, help="specify the artist name to play") | ||
play_parser.add_argument("--album", required=True, help="specify the album name to play") | ||
_ = subparsers.add_parser("list", help="list library contents") | ||
return parser.parse_args() | ||
|
||
|
||
def get_env(): | ||
sonos_host = os.environ.get("SONOS_HOST") | ||
if sonos_host is None: | ||
print("env var `SONOS_HOST` is required") | ||
exit(1) | ||
return sonos_host | ||
|
||
|
||
def main(): | ||
args = get_args() | ||
library = json.load(open(args.library, "r", encoding="utf-8")) | ||
match args.command: | ||
case "list": | ||
pprint(library) | ||
case "play": | ||
sonos_host = get_env() | ||
uri = library[args.artist][args.album] | ||
sonos = SoCo(sonos_host) | ||
sharelink = ShareLinkPlugin(sonos) | ||
play(sharelink, uri) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,24 @@ | ||
[tool.poetry] | ||
name = "jukebox" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Your Name <[email protected]>"] | ||
readme = "README.md" | ||
package-mode = false | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.12" | ||
soco = "^0.30.4" | ||
|
||
|
||
[tool.poetry.group.dev.dependencies] | ||
ruff = "^0.5.6" | ||
|
||
[tool.ruff] | ||
line-length = 120 | ||
indent-width = 4 | ||
target-version = "py312" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
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,5 @@ | ||
{ | ||
"Your favorite artist": { | ||
"Your favorite album by this artist": "URI of this album." | ||
} | ||
} |