LastFMHandler

Struct LastFMHandler 

Source
pub struct LastFMHandler { /* private fields */ }

Implementations§

Source§

impl LastFMHandler

Source

pub fn new(username: &str) -> Result<Self>

Creates a new LastFMHandler instance.

§Arguments
  • username - The Last.fm username.
§Errors

Returns LastFmError::MissingEnvVar if the environment variable LAST_FM_API_KEY is not set.

§Returns
  • Result<Self> - The created LastFMHandler instance.
Source

pub async fn get_user_loved_tracks_with_options( &self, limit: impl Into<TrackLimit>, ) -> Result<Vec<LovedTrack>>

👎Deprecated since 2.0.0: Use LovedTracksClient from the api module instead

Get loved tracks for a user with all available options.

This is the most flexible method for fetching loved tracks, exposing all parameters supported by the Last.fm API’s user.getlovedtracks method.

§Arguments
  • limit - The number of tracks to fetch. Use None or TrackLimit::Unlimited to fetch all tracks.
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<LovedTrack>> - The fetched loved tracks.
§Examples
// Get all loved tracks
let tracks = handler.get_user_loved_tracks_with_options(None).await?;

// Get first 100 loved tracks
let tracks = handler.get_user_loved_tracks_with_options(Some(100)).await?;
Source

pub async fn get_user_loved_tracks( &self, limit: impl Into<TrackLimit>, ) -> Result<Vec<LovedTrack>>

👎Deprecated since 2.0.0: Use LovedTracksClient from the api module instead

Get loved tracks for a user.

§Arguments
  • limit - The number of tracks to fetch. If None, fetch all tracks.
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<LovedTrack>, Error> - The fetched tracks.
Source

pub async fn get_user_recent_tracks( &self, limit: impl Into<TrackLimit>, ) -> Result<Vec<RecentTrack>>

👎Deprecated since 2.0.0: Use RecentTracksClient from the api module instead

Get recent tracks for a user.

§Arguments
  • limit - The number of tracks to fetch. If None, fetch all tracks.
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<RecentTrack>, Error> - The fetched tracks.
Source

pub async fn get_user_top_tracks_with_options( &self, limit: impl Into<TrackLimit>, period: Option<Period>, ) -> Result<Vec<TopTrack>>

👎Deprecated since 2.0.0: Use TopTracksClient from the api module instead

Get top tracks for a user with all available options.

This is the most flexible method for fetching top tracks, exposing all parameters supported by the Last.fm API’s user.gettoptracks method.

§Arguments
  • limit - The number of tracks to fetch. Use None or TrackLimit::Unlimited to fetch all available top tracks.
  • period - Optional period filter for the time range:
    • Period::Overall - All time (default if None)
    • Period::Week - Last 7 days
    • Period::Month - Last month
    • Period::ThreeMonth - Last 3 months
    • Period::SixMonth - Last 6 months
    • Period::TwelveMonth - Last 12 months
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<TopTrack>> - The fetched top tracks.
§Examples
// Get all-time top 50 tracks
let tracks = handler.get_user_top_tracks_with_options(Some(50), None).await?;

// Get top tracks from the last week
let tracks = handler.get_user_top_tracks_with_options(None, Some(Period::Week)).await?;

// Get top 100 tracks from the last 3 months
let tracks = handler.get_user_top_tracks_with_options(Some(100), Some(Period::ThreeMonth)).await?;
Source

pub async fn get_user_top_tracks( &self, limit: impl Into<TrackLimit>, period: Option<Period>, ) -> Result<Vec<TopTrack>>

👎Deprecated since 2.0.0: Use TopTracksClient from the api module instead

Get top tracks for a user.

§Arguments
  • limit - The number of tracks to fetch. If None, fetch all available top tracks.
  • period - Optional period filter (Period::Overall, Period::SevenDay, Period::OneMonth, Period::ThreeMonth, Period::SixMonth, Period::TwelveMonth)
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<TopTrack>> - The fetched tracks.
Source

pub async fn get_and_save_recent_tracks( &self, limit: impl Into<TrackLimit>, format: FileFormat, filename_prefix: &str, ) -> Result<String>

Get and save recent tracks to a file.

§Arguments
  • limit - The number of tracks to fetch. If None, fetch all tracks.
  • format - The file format to save the tracks in.
§Errors
  • LastFmError::Api - If the API returns an error.
  • LastFmError::Io - If there is an error saving the file.
§Returns
  • Result<String, Box<dyn std::error::Error>> - The filename of the saved file.
Source

pub async fn get_and_save_loved_tracks( &self, limit: impl Into<TrackLimit>, format: FileFormat, ) -> Result<String>

Get and save loved tracks to a file.

§Arguments
  • limit - The number of tracks to fetch. If None, fetch all tracks.
  • format - The file format to save the tracks in.
§Errors
  • FileError - If there was an error reading or writing the file
  • InvalidUtf8 - If the file path is not valid UTF-8
§Returns
  • Result<String, Box<dyn std::error::Error>> - The filename of the saved file.
Source

pub async fn get_user_recent_tracks_since( &self, from: i64, to: Option<i64>, limit: impl Into<TrackLimit>, ) -> Result<Vec<RecentTrack>>

Get recent tracks for a user since a given timestamp.

§Arguments
  • from - The timestamp to fetch tracks since.
  • to - Optional timestamp to fetch tracks until.
  • limit - The number of tracks to fetch. If None, fetch all tracks.
§Errors
  • FileError - If there was an error reading or writing the file
  • InvalidUtf8 - If the file path is not valid UTF-8
§Returns
  • Vec<RecentTrack> - The fetched tracks.
Source

pub async fn get_user_recent_tracks_with_options( &self, limit: impl Into<TrackLimit>, from: Option<i64>, to: Option<i64>, extended: bool, ) -> Result<Vec<RecentTrack>>

👎Deprecated since 2.0.0: Use RecentTracksClient from the api module instead

Get recent tracks for a user with all available options.

This is the most flexible method for fetching recent tracks, exposing all parameters supported by the Last.fm API’s user.getrecenttracks method.

§Arguments
  • limit - The number of tracks to fetch. Use None or TrackLimit::Unlimited to fetch all tracks.
  • from - Optional timestamp (Unix timestamp in seconds) to fetch tracks from this time onwards.
  • to - Optional timestamp (Unix timestamp in seconds) to fetch tracks up until this time.
  • extended - If true, fetches extended track information including additional artist details.
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<RecentTrack>> - The fetched tracks (normal format).
  • Result<Vec<RecentTrackExtended>> - The fetched tracks (extended format) if extended is true.
§Examples
// Get last 50 tracks
let tracks = handler.get_user_recent_tracks_with_options(Some(50), None, None, false).await?;

// Get tracks from the last week
let one_week_ago = (Utc::now() - Duration::days(7)).timestamp();
let tracks = handler.get_user_recent_tracks_with_options(None, Some(one_week_ago), None, false).await?;

// Get tracks between two dates with extended info
let tracks = handler.get_user_recent_tracks_with_options(None, Some(start), Some(end), true).await?;
Source

pub async fn get_user_recent_tracks_extended( &self, limit: impl Into<TrackLimit>, from: Option<i64>, to: Option<i64>, ) -> Result<Vec<RecentTrackExtended>>

👎Deprecated since 2.0.0: Use RecentTracksClient from the api module instead

Get recent tracks for a user with extended information.

This method is similar to get_user_recent_tracks_with_options but returns the extended track format which includes additional artist details.

§Arguments
  • limit - The number of tracks to fetch. Use None or TrackLimit::Unlimited to fetch all tracks.
  • from - Optional timestamp (Unix timestamp in seconds) to fetch tracks from this time onwards.
  • to - Optional timestamp (Unix timestamp in seconds) to fetch tracks up until this time.
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<RecentTrackExtended>> - The fetched tracks with extended information.
Source

pub async fn get_user_recent_tracks_between( &self, from: i64, to: i64, extended: bool, ) -> Result<Vec<RecentTrack>>

👎Deprecated since 2.0.0: Use RecentTracksClient from the api module instead

Get all recent tracks for a user between two dates.

Convenience method for fetching all tracks within a specific time range. This always fetches unlimited tracks (all tracks in the range).

§Arguments
  • from - Start timestamp (Unix timestamp in seconds) - tracks from this time onwards.
  • to - End timestamp (Unix timestamp in seconds) - tracks up until this time.
  • extended - If true, fetches extended track information including additional artist details.
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<RecentTrack>> - All fetched tracks in the date range (normal format).
§Examples
// Get all tracks from January 2024
let start = Utc.ymd(2024, 1, 1).and_hms(0, 0, 0).timestamp();
let end = Utc.ymd(2024, 2, 1).and_hms(0, 0, 0).timestamp();
let tracks = handler.get_user_recent_tracks_between(start, end, false).await?;

// Get all tracks from last week with extended info
let one_week_ago = (Utc::now() - Duration::days(7)).timestamp();
let now = Utc::now().timestamp();
let tracks = handler.get_user_recent_tracks_between(one_week_ago, now, true).await?;
Source

pub async fn get_user_recent_tracks_between_extended( &self, from: i64, to: i64, ) -> Result<Vec<RecentTrackExtended>>

👎Deprecated since 2.0.0: Use RecentTracksClient from the api module instead

Get all recent tracks for a user between two dates with extended information.

Convenience method for fetching all tracks with extended information within a specific time range. This always fetches unlimited tracks (all tracks in the range) with extended data.

§Arguments
  • from - Start timestamp (Unix timestamp in seconds) - tracks from this time onwards.
  • to - End timestamp (Unix timestamp in seconds) - tracks up until this time.
§Errors

Returns an error if the API request fails.

§Returns
  • Result<Vec<RecentTrackExtended>> - All fetched tracks in the date range with extended information.
§Examples
// Get all tracks from January 2024 with extended info
let start = Utc.ymd(2024, 1, 1).and_hms(0, 0, 0).timestamp();
let end = Utc.ymd(2024, 2, 1).and_hms(0, 0, 0).timestamp();
let tracks = handler.get_user_recent_tracks_between_extended(start, end).await?;
Source

pub async fn get_user_loved_tracks_since( &self, timestamp: u32, limit: impl Into<TrackLimit>, ) -> Result<Vec<LovedTrack>>

Get loved tracks for a user since a given timestamp.

§Arguments
  • timestamp - The timestamp to fetch tracks since.
  • limit - The number of tracks to fetch. If None, fetch all tracks.
§Errors
  • FileError - If there was an error reading or writing the file
  • InvalidUtf8 - If the file path is not valid UTF-8
§Returns
  • Vec<LovedTrack> - The fetched tracks.
Source

pub async fn update_tracks_file<T: DeserializeOwned + Serialize + Timestamped>( &self, file_path: &Path, ) -> Result<String>

Update a tracks file with new tracks.

§Arguments
  • file_path - Path to the file to update.
  • fetch_since - Function to fetch tracks since a given timestamp.
§Errors
  • FileError - If there was an error reading or writing the file
§Panics
  • If the file path is not valid UTF-8
§Returns
  • Result<String, Box<dyn std::error::Error>> - The filename of the updated file.
Source

pub async fn export_recent_play_counts( &self, limit: impl Into<TrackLimit>, ) -> Result<String>

Export play counts for the last X songs with additional track information

§Arguments
  • limit - Number of recent tracks to analyze
  • file_path - Path to the file to save the play counts to
§Errors
  • FileError - If there was an error reading or writing the file
§Returns
  • Result<String> - Path to the saved JSON file containing play counts
Source

pub async fn update_recent_play_counts( &self, limit: impl Into<TrackLimit>, file_path: &str, ) -> Result<String>

Update or create a file with play counts for the last X songs with additional track information

§Arguments
  • limit - Number of recent tracks to analyze
  • file_path - Path to the file to update/create
§Errors
  • LastFmError::Api - If the API returns an error
  • LastFmError::Io - If there is an error reading or writing the file
§Returns
  • Result<String> - Path to the updated/created JSON file containing play counts
Source

pub async fn is_currently_playing(&self) -> Result<Option<RecentTrack>>

Check if the user is currently playing a track

§Errors
  • LastFmError - If there was an error communicating with Last.fm
§Returns
  • Result<Option<RecentTrack>> - The currently playing track if any
Source

pub async fn update_currently_listening( &self, file_path: &str, ) -> Result<Option<RecentTrack>>

Update a file with the currently playing track information

§Arguments
  • file_path - Path to the file to update
§Errors
  • LastFmError::Api - If the API returns an error
  • LastFmError::Io - If there is an error reading or writing the file
  • LastFmError::Parse - If there is an error parsing the JSON
§Returns
  • Result<Option<RecentTrack>> - The currently playing track if any

Trait Implementations§

Source§

impl Clone for LastFMHandler

Source§

fn clone(&self) -> LastFMHandler

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LastFMHandler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more