#api #deprecated #local #abstraction #client #longer #cli

bin+lib spotify

易于使用的Spotify本地API抽象

15 个不稳定版本

0.8.2 2023年10月10日
0.8.0 2022年10月5日
0.7.1 2017年9月6日
0.6.2 2017年5月3日
0.6.0 2016年11月27日

#1 in #longer

Download history 33/week @ 2024-03-22 14/week @ 2024-03-29 1/week @ 2024-04-05

每月71次下载

MIT 许可证

41KB
605

Spotify-rs

Crates.io Crates.io

文档

Spotify-rs 提供了 Spotify 本地 API 的易于使用的抽象。

⚠️ 弃用警告

此库已弃用且不再维护。Spotify 已从其桌面客户端中删除了本地 API 和 SpotifyWebHelper,因此此库不再有用。我保留它是为了历史原因,但再使用它的意义不大。

我能用它做什么

功能

  • 使用曲目 ID 或 URI 播放曲目
  • 暂停/恢复当前播放的曲目
  • 获取当前播放的曲目(包括专辑、艺术家等)
  • 获取当前音量
  • 获取 Spotify 客户端版本和在线状态
  • 在单独的线程中通过长轮询对更改做出反应

还有一些其他功能

  • 自动修复损坏但可恢复的曲目 ID 和 URI
  • 检查 SpotifyWebHelper 是否正在运行(仅限 Windows)

项目是否还在活跃中?

我已经很长时间没有真正在这个库上工作过了,但这主要是因为它已经基本完成了。据我所知,没有显著的错误,cli 应该可以正常工作。我已经努力将所有内容从 2015 年版本迁移到 2021 年版本,并且代码可以编译(使用 Rust 1.64 进行测试)。

示例

以下是一个最小示例,以展示您可以使用 spotify-rs 做什么。
请注意,这个示例为了简洁而省略了适当的错误处理。

extern crate spotify;
use spotify::Spotify;

fn main() {
    // Grab an instance of the Spotify API
    let spotify = Spotify::connect().unwrap();

    // Fetch the current status from Spotify
    let status = spotify.status().unwrap();

    // Display the Spotify Client version
    println!("Spotify Client (Version {})", status.version());
             
    // Display the currently playing track
    println!("Playing: {:#}", status.track());
}

示例输出

Spotify Client (Version 1.0.42.151.g19de0aa6)
Playing: Rick Astley - Never Gonna Give You Up

以下是一个完整的示例,包含长轮询和更好的错误处理

extern crate spotify;
use spotify::{Spotify, SpotifyError};

fn main() {
    // Grab an instance of the Spotify API.
    let spotify = match Spotify::connect() {
        Ok(result) => result,
        Err(error) => {
            // Display a nice end-user-friendly error message
            match error {
                SpotifyError::ClientNotRunning => {
                    println!("The Spotify Client is not running!");
                    std::process::exit(1);
                }
                SpotifyError::WebHelperNotRunning => {
                    println!("The SpotifyWebHelper process is not running!");
                    std::process::exit(2);
                }
                SpotifyError::InternalError(err) => {
                    println!("Internal Error: {:?}", err);
                    std::process::exit(3);
                }
            }
        }
    };

    // Start polling.
    // Updates the state every 250ms.
    // 
    // The 'status' variable holds the `SpotifyStatus`,
    // the 'change' variable contains booleans to indicate which fields
    // had changed since the last update.
    let reactor = spotify.poll(|_, status, change| {
        // Print the Spotify Client version on change.
        if change.client_version {
            println!("Spotify Client (Version {})", status.version());
        }
        // Print the currently playing track on change.
        if change.track {
            println!("Now playing: {:#}", status.track());
        }
        // Print the current volume on change.
        if change.volume {
            println!("Volume: {}%", status.volume_percentage());
        }

        // Returning true will continue polling, whereas returning
        // false will stop polling and return from the thread.
        true
    });

    // Join the reactor thread so the application
    // doesn't close before receiving any data.
    if reactor.join().ok().is_none() {
        println!("Unable to join into the live-update.");
        std::process::exit(4);
    }
}

示例输出

Spotify Client (Version 1.0.42.151.g19de0aa6)
Now playing: Tim Minchin - White Wine In The Sun
Volume: 100%
Now playing: Tim Minchin - Encore
Volume: 50%
Volume: 76%
Now playing: Tim Minchin - Ready For This ?
Volume: 100%

常见问题解答

无法连接,怎么回事?
请确保 Spotify 正在运行且 SpotifyWebHelper 进程处于活动状态。

如果您找不到 SpotifyWebHelper.exe 在进程列表中,您可能不小心将其禁用了。以下是如何启用它的方法

  • 打开 Spotify
  • Ctrl + P 打开首选项
  • 向下滚动并点击 '显示高级设置'
  • 启动和窗口行为 部分,
    启用 允许 Spotify 从 Web 打开

完成这些操作后,您可能需要重新启动 Spotify。

更新:我不确定这个选项现在是否仍然公开。macOS上的Spotify 1.1.95(2022)似乎已经没有了这个选项,我不确定Spotify是否仍然公开本地API。如果它没有,这个库就几乎没有任何用处。如果您知道它是否仍然有效,请打开一个问题并通知我!

依赖项

~21MB
~444K SLoC