1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-03 01:39:28 +02:00
librespot/examples
Jay Malhotra df5f957bdd
refactor: Introduce SpotifyUri struct (#1538)
* refactor: Introduce SpotifyUri struct

Contributes to #1266

Introduces a new `SpotifyUri` struct which is layered on top of the
existing `SpotifyId`, but has the capability to support URIs that do
not confirm to the canonical base62 encoded format. This allows it to
describe URIs like `spotify:local`, `spotify:genre` and others that
`SpotifyId` cannot represent.

Changed the internal player state to use these URIs as much as possible,
such that the player could in the future accept a URI of the type
`spotify:local`, as a means of laying the groundwork for local file
support.

* fix: Don't pass unknown URIs from deprecated player methods

* refactor: remove SpotifyUri::to_base16

This should be deprecated for the same reason to_base62 is, and could unpredictably throw errors -- consumers should match on the inner ID if they need a base62 representation and handle failure appropriately

* refactor: Store original data in SpotifyUri::Unknown

Instead of assuming Unknown has a u128 SpotifyId, store the original data and type that we failed to parse.

* refactor: Remove SpotifyItemType

* refactor: Address review feedback

* test: Add more SpotifyUri tests

* chore: Correctly mark changes as breaking in CHANGELOG.md

* refactor: Respond to review feedback

* chore: Changelog updates
2025-09-11 19:59:53 +00:00
..
get_token.rs Credentials with access token (oauth) (#1309) 2024-09-13 07:35:55 +02:00
play.rs refactor: Introduce SpotifyUri struct (#1538) 2025-09-11 19:59:53 +00:00
play_connect.rs refactor: update to Rust 1.85 and edition 2024, use inline log args 2025-08-13 16:19:39 +02:00
playlist_tracks.rs refactor: Introduce SpotifyUri struct (#1538) 2025-09-11 19:59:53 +00:00
README.md docs: Document examples (#1567) 2025-09-09 19:05:04 +00:00

Examples

This folder contains examples of how to use the librespot library for various purposes.

How to run the examples

In general, to invoke an example, clone down the repo and use cargo as follows:

cargo run --example [filename]

in which filename is the file name of the example, for instance get_token or play.

Acquiring an access token

Most examples require an access token as the first positional argument. Note that an access token gained by the client credentials flow will not work. librespot-oauth provides a utility to acquire an access token using an OAuth flow, which will be able to run the examples. To invoke this, run:

cargo run --package librespot-oauth --example oauth_sync

A browser window will open and prompt you to authorize with Spotify. Once done, take the access_token property from the dumped object response and proceed to use it in examples. You may find it convenient to save it in a shell variable like $ACCESS_TOKEN.

Once you have obtained the token you can proceed to run the example. Check each individual file to see what arguments are expected. As a demonstration, here is how to invoke the play example to play a song -- the second argument is the URI of the track to play.

cargo run --example play "$ACCESS_TOKEN" 2WUy2Uywcj5cP0IXQagO3z