rust-lastfm

Build Status Crates.io docs.rs

lastfm is an async Rust client to fetch your Last.fm listening history or the track you are currently playing

Installation

Add the following to your Cargo.toml:

toml [dependencies] lastfm = "*"

Replace the * with the actual version you want to use.

Alternatively you can run:

```bash cargo add lastfm ````

Usage

To use this library you will need a Last.fm account and an API key. You can get one by registering an application. If you have already registered an application, you can find your API key in the API settings.

Create a new client

If you have your API key exposed through the LASTFM_API_KEY environment variable, you can use the from_env method:

rust,no_run let client = Client::from_env("YOUR_USERNAME");

Note: this method will panic if LASTFM_API_KEY is not set.

Alternatively, you can use try_from_env which will return a Result.

rust,no_run let maybe_client = Client::try_from_env("YOUR_USERNAME"); match maybe_client { Ok(client) => { // do something with the client } Err(e) => { // handle error } }

Finally, for more advanced configurations you can use a ClientBuilder:

rust let client = ClientBuilder::new("YOUR_API_KEY", "YOUR_USERNAME").build();

Fetch the track you are currently playing

```rust,no_run

[tokio::main]

async fn main() -> Result<(), Box> { let client = ClientBuilder::new("YOURAPIKEY", "YOURUSERNAME").build(); let nowplaying = client.nowplaying().await?; if let Some(track) = nowplaying { println!("Now playing: {} - {}", track.artist.name, track.name); }

Ok(()) } ```

Fetch your listening history

Note: You will need the futures-util crate to use the Stream returned by all_tracks.

```rust,norun use futuresutil::pinmut; use futuresutil::stream::StreamExt;

[tokio::main]

async fn main() -> Result<(), Box> { let client = ClientBuilder::new("YOURAPIKEY", "YOURUSERNAME").build(); let tracks = client.alltracks().await?; println!("Total tracks: {}", tracks.total_tracks);

let recenttracks = tracks.intostream(); pinmut!(recenttracks); // needed for iteration while let Some(track) = recenttracks.next().await { match track { Ok(track) => { println!( "{}: {} - {}", track.date.torfc2822(), track.artist.name, track.name ); } Err(e) => { println!("Error fetching data: {:?}", e); } } } Ok(()) } ```

Examples

This package provides some usage examples in the examples folder.

You will need an API key to run the examples so you will need to:

Other implementations

This project is a port of something I have already done in JavaScript (Node.js). Check out lmammino/scrobbles if you are curious.

Contributing

Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.

License

Licensed under MIT License. © Luciano Mammino.