Giter Site home page Giter Site logo

duckfromdiscord / lastfm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lmammino/lastfm

0.0 0.0 0.0 106 KB

An async client to fetch your Last.fm listening history or the track you are currently playing

Home Page: https://loige.co

License: MIT License

Rust 100.00%

lastfm's Introduction

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:

[dependencies]
lastfm = "*"

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

Alternatively you can run:

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:

let client = Client::<String, &str>::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.

let maybe_client = Client::<String, &str>::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 Client::builder():

let client = Client::builder().api_key("YOUR_API_KEY").username("YOUR_USERNAME").build();

Fetch the track you are currently playing

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = Client::builder().api_key("YOUR_API_KEY").username("YOUR_USERNAME").build();
  let now_playing = client.now_playing().await?;
  if let Some(track) = now_playing {
    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.

use futures_util::pin_mut;
use futures_util::stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = Client::builder().api_key("YOUR_API_KEY").username("YOUR_USERNAME").build();
  let tracks = client.all_tracks().await?;
  println!("Total tracks: {}", tracks.total_tracks);

   let recent_tracks = tracks.into_stream();
   pin_mut!(recent_tracks); // needed for iteration
   while let Some(track) = recent_tracks.next().await {
       match track {
           Ok(track) => {
               println!(
                   "{}: {} - {}",
                   track.date.to_rfc2822(),
                   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:

  • copy .env~sample into .env
  • add your last.fm API Key in there
  • run a give example. E.g.: cargo run --example fetch_all

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.

lastfm's People

Contributors

lmammino avatar duckfromdiscord avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.