Implement device class option with speaker default

This commit is contained in:
Dominic Pearson
2024-08-26 16:58:52 +02:00
parent 7e70839798
commit f16fa8ad71
2 changed files with 25 additions and 5 deletions

View File

@@ -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
@@ -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`).

View File

@@ -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)]
@@ -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) {
@@ -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();