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

Commit f16fa8a

Browse files
committed
Implement device class option with speaker default
1 parent 7e70839 commit f16fa8a

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ A simple program for populating a `credentials.json` via Spotify's zeroconf auth
66

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

1017
## How It Works
1118

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

2431
## Example Usage (spotifyd)
2532

33+
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):
34+
2635
```bash
27-
$ ./target/release/librespot-auth --name "Example Speaker"
28-
Open Spotify and select output device: Example Speaker
36+
$ ./target/release/librespot-auth --name "Second Laptop" --class=computer
37+
Open Spotify and select output device: Second Laptop
2938
```
3039
31-
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`).
40+
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`).

src/main.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use clap::Parser;
22
use futures::StreamExt;
33
use librespot_core::authentication::Credentials;
44
use librespot_core::SessionConfig;
5-
use librespot_discovery::{DeviceType, Discovery};
5+
use librespot_core::config::DeviceType;
6+
use librespot_discovery::Discovery;
67
use sha1::{Digest, Sha1};
78
use serde_json;
89
use std::fs::File;
910
use std::io::Write;
1011
use std::process::exit;
12+
use std::str::FromStr;
1113
use log::warn;
1214

1315
#[derive(Parser, Debug)]
@@ -17,6 +19,8 @@ struct Args {
1719
name: String,
1820
#[arg(short, long, default_value = "credentials.json")]
1921
path: String,
22+
#[arg(short, long, default_value = "speaker")]
23+
class: String,
2024
}
2125

2226
pub fn save_credentials_and_exit(location: &str, cred: &Credentials) {
@@ -40,10 +44,17 @@ async fn main() {
4044
let name = args.name;
4145
let credentials_location = args.path;
4246
let device_id = hex::encode(Sha1::digest(name.clone().as_bytes()));
47+
let device_type = match DeviceType::from_str(&args.class) {
48+
Ok(device_type) => device_type,
49+
Err(_) => {
50+
eprintln!("Invalid device type: {}", args.class);
51+
exit(1);
52+
}
53+
};
4354

4455
let mut server = Discovery::builder(device_id.clone(), SessionConfig::default().client_id)
4556
.name(name.clone())
46-
.device_type(DeviceType::Computer)
57+
.device_type(device_type)
4758
.launch()
4859
.unwrap();
4960

0 commit comments

Comments
 (0)