pub struct LastFmEditClientImpl { /* private fields */ }Expand description
Main implementation for interacting with Last.fm’s web interface.
This implementation handles authentication, session management, and provides methods for browsing user libraries and editing scrobble data through web scraping.
§Examples
use lastfm_edit::{LastFmEditClient, LastFmEditClientImpl, Result};
#[tokio::main]
async fn main() -> Result<()> {
// Create client with any HTTP implementation
let http_client = http_client::native::NativeClient::new();
let mut client = LastFmEditClientImpl::new(Box::new(http_client));
// Login to Last.fm
client.login("username", "password").await?;
// Check if authenticated
assert!(client.is_logged_in());
Ok(())
}Implementations§
Source§impl LastFmEditClientImpl
impl LastFmEditClientImpl
Sourcepub fn new(client: Box<dyn HttpClient + Send + Sync>) -> Self
pub fn new(client: Box<dyn HttpClient + Send + Sync>) -> Self
Create a new LastFmEditClient with the default Last.fm URL.
Note: This creates an unauthenticated client. You must call login
or restore_session before using most functionality.
§Arguments
client- Any HTTP client implementation that implementsHttpClient
§Examples
use lastfm_edit::{LastFmEditClient, LastFmEditClientImpl, Result};
#[tokio::main]
async fn main() -> Result<()> {
let http_client = http_client::native::NativeClient::new();
let mut client = LastFmEditClientImpl::new(Box::new(http_client));
client.login("username", "password").await?;
Ok(())
}Sourcepub fn with_base_url(
client: Box<dyn HttpClient + Send + Sync>,
base_url: String,
) -> Self
pub fn with_base_url( client: Box<dyn HttpClient + Send + Sync>, base_url: String, ) -> Self
Create a new LastFmEditClient with a custom base URL.
Note: This creates an unauthenticated client. You must call login
or restore_session before using most functionality.
This is useful for testing or if Last.fm changes their domain.
§Arguments
client- Any HTTP client implementationbase_url- The base URL for Last.fm (e.g., https://www.last.fm)
Sourcepub fn with_rate_limit_patterns(
client: Box<dyn HttpClient + Send + Sync>,
base_url: String,
rate_limit_patterns: Vec<String>,
) -> Self
pub fn with_rate_limit_patterns( client: Box<dyn HttpClient + Send + Sync>, base_url: String, rate_limit_patterns: Vec<String>, ) -> Self
Create a new LastFmEditClient with custom rate limit detection patterns.
§Arguments
client- Any HTTP client implementationbase_url- The base URL for Last.fmrate_limit_patterns- Text patterns that indicate rate limiting in responses
Sourcepub async fn login_with_credentials(
client: Box<dyn HttpClient + Send + Sync>,
username: &str,
password: &str,
) -> Result<Self>
pub async fn login_with_credentials( client: Box<dyn HttpClient + Send + Sync>, username: &str, password: &str, ) -> Result<Self>
Create a new authenticated LastFmEditClient by logging in with username and password.
This is a convenience method that combines client creation and login into one step.
§Arguments
client- Any HTTP client implementationusername- Last.fm username or emailpassword- Last.fm password
§Returns
Returns an authenticated client on success, or LastFmError::Auth on failure.
§Examples
use lastfm_edit::{LastFmEditClient, LastFmEditClientImpl, Result};
#[tokio::main]
async fn main() -> Result<()> {
let client = LastFmEditClientImpl::login_with_credentials(
Box::new(http_client::native::NativeClient::new()),
"username",
"password"
).await?;
assert!(client.is_logged_in());
Ok(())
}Sourcepub fn from_session(
client: Box<dyn HttpClient + Send + Sync>,
session: LastFmEditSession,
) -> Self
pub fn from_session( client: Box<dyn HttpClient + Send + Sync>, session: LastFmEditSession, ) -> Self
Create a new LastFmEditClient by restoring a previously saved session.
This allows you to resume a Last.fm session without requiring the user to log in again.
§Arguments
client- Any HTTP client implementationsession- Previously savedLastFmEditSession
§Returns
Returns a client with the restored session.
§Examples
use lastfm_edit::{LastFmEditClient, LastFmEditClientImpl, LastFmEditSession};
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
// Assume we have a saved session
let session_json = std::fs::read_to_string("session.json")?;
let session = LastFmEditSession::from_json(&session_json)?;
let client = LastFmEditClientImpl::from_session(
Box::new(http_client::native::NativeClient::new()),
session
);
assert!(client.is_logged_in());
Ok(())
}Sourcepub fn get_session(&self) -> LastFmEditSession
pub fn get_session(&self) -> LastFmEditSession
Extract the current session state for persistence.
This allows you to save the authentication state and restore it later without requiring the user to log in again.
§Returns
Returns a LastFmEditSession that can be serialized and saved.
§Examples
use lastfm_edit::{LastFmEditClient, LastFmEditClientImpl, Result};
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let mut client = LastFmEditClientImpl::new(Box::new(http_client::native::NativeClient::new()));
client.login("username", "password").await?;
// Save session for later use
let session = client.get_session();
let session_json = session.to_json()?;
std::fs::write("session.json", session_json)?;
Ok(())
}Sourcepub fn restore_session(&self, session: LastFmEditSession)
pub fn restore_session(&self, session: LastFmEditSession)
Restore session state from a previously saved LastFmEditSession.
This allows you to restore authentication state without logging in again.
§Arguments
session- Previously saved session state
§Examples
use lastfm_edit::{LastFmEditClient, LastFmEditClientImpl, LastFmEditSession};
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let mut client = LastFmEditClientImpl::new(Box::new(http_client::native::NativeClient::new()));
// Restore from saved session
let session_json = std::fs::read_to_string("session.json")?;
let session = LastFmEditSession::from_json(&session_json)?;
client.restore_session(session);
assert!(client.is_logged_in());
Ok(())
}Sourcepub async fn login(&self, username: &str, password: &str) -> Result<()>
pub async fn login(&self, username: &str, password: &str) -> Result<()>
Authenticate with Last.fm using username and password.
This method:
- Fetches the login page to extract CSRF tokens
- Submits the login form with credentials
- Validates the authentication by checking for session cookies
- Stores session data for subsequent requests
§Arguments
username- Last.fm username or emailpassword- Last.fm password
§Returns
Returns [Ok(())] on successful authentication, or LastFmError::Auth on failure.
§Examples
let mut client = LastFmEditClientImpl::new(Box::new(http_client::native::NativeClient::new()));
client.login("username", "password").await?;
assert!(client.is_logged_in());Sourcepub fn username(&self) -> String
pub fn username(&self) -> String
Get the currently authenticated username.
Returns an empty string if not logged in.
Sourcepub fn is_logged_in(&self) -> bool
pub fn is_logged_in(&self) -> bool
Check if the client is currently authenticated.
Returns true if login was successful and session is active.
Sourcepub async fn get_recent_scrobbles(&self, page: u32) -> Result<Vec<Track>>
pub async fn get_recent_scrobbles(&self, page: u32) -> Result<Vec<Track>>
Fetch recent scrobbles from the user’s listening history This gives us real scrobble data with timestamps for editing
Sourcepub async fn find_recent_scrobble_for_track(
&self,
track_name: &str,
artist_name: &str,
max_pages: u32,
) -> Result<Option<Track>>
pub async fn find_recent_scrobble_for_track( &self, track_name: &str, artist_name: &str, max_pages: u32, ) -> Result<Option<Track>>
Find the most recent scrobble for a specific track This searches through recent listening history to find real scrobble data
pub async fn edit_scrobble(&self, edit: &ScrobbleEdit) -> Result<EditResponse>
Sourcepub async fn find_scrobble_by_timestamp(&self, timestamp: u64) -> Result<Track>
pub async fn find_scrobble_by_timestamp(&self, timestamp: u64) -> Result<Track>
Find a scrobble by its timestamp in recent scrobbles
pub async fn edit_scrobble_with_retry( &self, edit: &ScrobbleEdit, max_retries: u32, ) -> Result<EditResponse>
Sourcepub async fn load_edit_form_values(
&self,
track_name: &str,
artist_name: &str,
) -> Result<ScrobbleEdit>
pub async fn load_edit_form_values( &self, track_name: &str, artist_name: &str, ) -> Result<ScrobbleEdit>
Load prepopulated form values for editing a specific track This extracts scrobble data directly from the track page forms
Sourcepub async fn get_album_tracks(
&self,
album_name: &str,
artist_name: &str,
) -> Result<Vec<Track>>
pub async fn get_album_tracks( &self, album_name: &str, artist_name: &str, ) -> Result<Vec<Track>>
Get tracks from a specific album page This makes a single request to the album page and extracts track data
Sourcepub async fn edit_album(
&self,
old_album_name: &str,
new_album_name: &str,
artist_name: &str,
) -> Result<EditResponse>
pub async fn edit_album( &self, old_album_name: &str, new_album_name: &str, artist_name: &str, ) -> Result<EditResponse>
Edit album metadata by updating scrobbles with new album name This edits ALL tracks from the album that are found in recent scrobbles
Sourcepub async fn edit_artist(
&self,
old_artist_name: &str,
new_artist_name: &str,
) -> Result<EditResponse>
pub async fn edit_artist( &self, old_artist_name: &str, new_artist_name: &str, ) -> Result<EditResponse>
Edit artist metadata by updating scrobbles with new artist name This edits ALL tracks from the artist that are found in recent scrobbles
Sourcepub async fn edit_artist_for_track(
&self,
track_name: &str,
old_artist_name: &str,
new_artist_name: &str,
) -> Result<EditResponse>
pub async fn edit_artist_for_track( &self, track_name: &str, old_artist_name: &str, new_artist_name: &str, ) -> Result<EditResponse>
Edit artist metadata for a specific track only This edits only the specified track if found in recent scrobbles
Sourcepub async fn edit_artist_for_album(
&self,
album_name: &str,
old_artist_name: &str,
new_artist_name: &str,
) -> Result<EditResponse>
pub async fn edit_artist_for_album( &self, album_name: &str, old_artist_name: &str, new_artist_name: &str, ) -> Result<EditResponse>
Edit artist metadata for all tracks in a specific album This edits ALL tracks from the specified album that are found in recent scrobbles
pub async fn get_artist_tracks_page( &self, artist: &str, page: u32, ) -> Result<TrackPage>
Sourcepub fn extract_tracks_from_document(
&self,
document: &Html,
artist: &str,
album: Option<&str>,
) -> Result<Vec<Track>>
pub fn extract_tracks_from_document( &self, document: &Html, artist: &str, album: Option<&str>, ) -> Result<Vec<Track>>
Extract tracks from HTML document (delegates to parser)
Sourcepub fn parse_tracks_page(
&self,
document: &Html,
page_number: u32,
artist: &str,
album: Option<&str>,
) -> Result<TrackPage>
pub fn parse_tracks_page( &self, document: &Html, page_number: u32, artist: &str, album: Option<&str>, ) -> Result<TrackPage>
Parse tracks page (delegates to parser)
Sourcepub fn parse_recent_scrobbles(&self, document: &Html) -> Result<Vec<Track>>
pub fn parse_recent_scrobbles(&self, document: &Html) -> Result<Vec<Track>>
Parse recent scrobbles from HTML document (for testing)
Sourcepub async fn get(&self, url: &str) -> Result<Response>
pub async fn get(&self, url: &str) -> Result<Response>
Make an HTTP GET request with authentication and retry logic
pub async fn get_artist_albums_page( &self, artist: &str, page: u32, ) -> Result<AlbumPage>
Source§impl LastFmEditClientImpl
impl LastFmEditClientImpl
Sourcepub fn artist_tracks(&self, artist: &str) -> ArtistTracksIterator
pub fn artist_tracks(&self, artist: &str) -> ArtistTracksIterator
Create an iterator for browsing an artist’s tracks from the user’s library.
Sourcepub fn artist_albums(&self, artist: &str) -> ArtistAlbumsIterator
pub fn artist_albums(&self, artist: &str) -> ArtistAlbumsIterator
Create an iterator for browsing an artist’s albums from the user’s library.
Sourcepub fn recent_tracks(&self) -> RecentTracksIterator
pub fn recent_tracks(&self) -> RecentTracksIterator
Create an iterator for browsing the user’s recent tracks/scrobbles.
Sourcepub fn recent_tracks_from_page(
&self,
starting_page: u32,
) -> RecentTracksIterator
pub fn recent_tracks_from_page( &self, starting_page: u32, ) -> RecentTracksIterator
Create an iterator for browsing the user’s recent tracks starting from a specific page.
Trait Implementations§
Source§impl Clone for LastFmEditClientImpl
impl Clone for LastFmEditClientImpl
Source§fn clone(&self) -> LastFmEditClientImpl
fn clone(&self) -> LastFmEditClientImpl
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more