| Crates.io | lastfm-rust |
| lib.rs | lastfm-rust |
| version | 0.1.0 |
| created_at | 2025-01-21 12:35:53.251128+00 |
| updated_at | 2025-01-21 12:35:53.251128+00 |
| description | Rust library for accessing the Last.fm API. |
| homepage | |
| repository | https://github.com/uppercasee/lastfm-rust |
| max_upload_size | |
| id | 1524820 |
| size | 204,319 |
lastfm-rust is a Rust library that provides user for interacting with the Last.fm API.
You can view Last.fm's official API documentation here.
Run this command in your terminal to add the latest version of lastfm-rust.
cargo add lastfm-rust
Before you begin, make sure you have the following environment variables set up for authentication:
API_KEYAPI_SECRETSK (session key) only if you already have oneThese keys are required for making API requests.
You can create a .env file in your project directory with the following content:
API_KEY=your_api_key
API_SECRET=your_api_secret
SK=your_session_key
Here is a simple example of how to use the lastfm-rust library to interact with the API:
use dotenv::dotenv;
use lastfm_rust::Lastfm;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
dotenv().ok();
let api_key = std::env::var("API_KEY").expect("API_KEY env variable is required");
let api_secret = std::env::var("API_SECRET").expect("API_SECRET env variable is required");
let sk = std::env::var("SK").expect("SK env variable is required");
// Initialize Lastfm client
let lastfm = Lastfm::builder()
.api_key(api_key)
.api_secret(api_secret)
.session_key(sk)
.build()?;
// Perform an action (e.g., add tags to an album)
let response = lastfm
.album()
.add_tags()
.artist("artist_name")
.album("album_name")
.tags("tag_name")
.send()
.await?;
match response {
APIResponse::Success(value) => {
println!("{}", value);
}
APIResponse::Error(err) => {
println!("Error: {} - {}", err.error, err.message);
}
}
Ok(())
}
Here’s how you can get session key using the auth() method:
let lastfm = Lastfm::builder()
.api_key(api_key)
.api_secret(api_secret)
.build()?;
// Request token
let response = lastfm.auth().get_token().send().await?;
let token = match response {
APIResponse::Success(value) => value.token,
APIResponse::Error(err) => {
return Err(Box::new(err) as Box<dyn Error>);
}
};
// Authorize the token and get session key
lastfm.auth().pls_authorize(token.to_string());
let response = lastfm.auth().get_session().token(&token).send().await?;
println!("Session: {}", response);
Note: If you're looking for a good scrobbling functionality, please use rustfm-scrobble or any other library for now.
let album_info = lastfm
.album()
.get_info()
.artist("artist_name")
.album("album_name")
.send()
.await?;
let tags = lastfm
.album()
.get_tags()
.artist("artist_name")
.album("album_name")
.send()
.await?;
let remove_response = lastfm
.album()
.remove_tags()
.artist("artist_name")
.album("album_name")
.tags("tag_name")
.send()
.await?;
More examples can be found in the examples folder.
serde_json::ValueContributions are welcome! For any bug reports/feature requests, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.