Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Implement device class option with speaker default
Browse files Browse the repository at this point in the history
  • Loading branch information
dspearson committed Aug 26, 2024
1 parent 7e70839 commit f16fa8a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ A simple program for populating a `credentials.json` via Spotify's zeroconf auth

- `--name`: Name of the virtual speaker (default: "Speaker").
- `--path`: Target path for credentials (default: `credentials.json` relative to execution).
- `--class`: Class of device, defaults on omission to speaker,
one of: computer, tablet, smartphone,
speaker, tv, avr, stb, audiodongle,
gameconsole, castaudio, castvideo,
automobile, smartwatch, chromebook,
carthing, homething.


## How It Works

Expand All @@ -23,9 +30,11 @@ Install the rust toolchain (see https://rustup.rs/), and run `cargo build --rele

## Example Usage (spotifyd)

A kind user pointed out that if you select a device type of class 'computer' then premium is not required, so this functionality is implemented (but optional, defaulting to speaker, which is what I use):

```bash
$ ./target/release/librespot-auth --name "Example Speaker"
Open Spotify and select output device: Example Speaker
$ ./target/release/librespot-auth --name "Second Laptop" --class=computer
Open Spotify and select output device: Second Laptop
```
Open the Spotify client from a machine on the same network as you ran this, ensuring no proxy is in use that may interfere with zeroconf. Select the speaker you just defined, i.e. "Example Speaker", as an output device. The credentials are then saved to `credentials.json`. Ensure spotifyd is stopped, i.e. `sudo systemctl stop spotifyd`, copy this file to your spotifyd `cache_directory`, and then start spotifyd again (`sudo systemctl start spotifyd`).
Open the Spotify client from a machine on the same network as you ran this, ensuring no proxy is in use that may interfere with zeroconf. Select the speaker you just defined, i.e. "Second Laptop", as an output device. The credentials are then saved to `credentials.json` in the provided path. Ensure spotifyd is stopped, i.e. `sudo systemctl stop spotifyd`, copy this file to your spotifyd `cache_directory`, and then start spotifyd again (`sudo systemctl start spotifyd`).
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use clap::Parser;
use futures::StreamExt;
use librespot_core::authentication::Credentials;
use librespot_core::SessionConfig;
use librespot_discovery::{DeviceType, Discovery};
use librespot_core::config::DeviceType;
use librespot_discovery::Discovery;
use sha1::{Digest, Sha1};
use serde_json;
use std::fs::File;
use std::io::Write;
use std::process::exit;
use std::str::FromStr;
use log::warn;

#[derive(Parser, Debug)]
Expand All @@ -17,6 +19,8 @@ struct Args {
name: String,
#[arg(short, long, default_value = "credentials.json")]
path: String,
#[arg(short, long, default_value = "speaker")]
class: String,
}

pub fn save_credentials_and_exit(location: &str, cred: &Credentials) {
Expand All @@ -40,10 +44,17 @@ async fn main() {
let name = args.name;
let credentials_location = args.path;
let device_id = hex::encode(Sha1::digest(name.clone().as_bytes()));
let device_type = match DeviceType::from_str(&args.class) {
Ok(device_type) => device_type,
Err(_) => {
eprintln!("Invalid device type: {}", args.class);
exit(1);
}
};

let mut server = Discovery::builder(device_id.clone(), SessionConfig::default().client_id)
.name(name.clone())
.device_type(DeviceType::Computer)
.device_type(device_type)
.launch()
.unwrap();

Expand Down

0 comments on commit f16fa8a

Please sign in to comment.