Skip to content

Commit

Permalink
🎉 initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gudsfile committed Aug 9, 2024
0 parents commit 649832b
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
poetry.lock
library.json
1 change: 1 addition & 0 deletions .sample_env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SONOS_HOST="IP address of your Sonos Zone Player"
37 changes: 37 additions & 0 deletions README.md
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.
51 changes: 51 additions & 0 deletions app.py
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()
24 changes: 24 additions & 0 deletions pyproject.toml
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"
5 changes: 5 additions & 0 deletions sample_library.json
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."
}
}

0 comments on commit 649832b

Please sign in to comment.