lastfm_edit/discovery/
artist_tracks.rs

1use super::common::filter_by_original_album_artist;
2use crate::{
3    AsyncDiscoveryIterator, AsyncPaginatedIterator, ExactScrobbleEdit, LastFmEditClientImpl,
4    Result, ScrobbleEdit,
5};
6use async_trait::async_trait;
7
8/// Case 4: Artist tracks discovery (neither track nor album specified)
9///
10/// This discovers all tracks by an artist by iterating through the artist's catalog
11/// and for each track, loading its scrobbles incrementally. This is the most complex
12/// case as it involves nested iteration.
13pub struct ArtistTracksDiscovery {
14    client: LastFmEditClientImpl,
15    edit: ScrobbleEdit,
16    tracks_iterator: crate::ArtistTracksIterator,
17    current_track_results: Vec<ExactScrobbleEdit>,
18    current_track_index: usize,
19}
20
21impl ArtistTracksDiscovery {
22    pub fn new(client: LastFmEditClientImpl, edit: ScrobbleEdit) -> Self {
23        let tracks_iterator =
24            crate::ArtistTracksIterator::new(client.clone(), edit.artist_name_original.clone());
25
26        Self {
27            client,
28            edit,
29            tracks_iterator,
30            current_track_results: Vec::new(),
31            current_track_index: 0,
32        }
33    }
34}
35
36#[async_trait(?Send)]
37impl AsyncDiscoveryIterator<ExactScrobbleEdit> for ArtistTracksDiscovery {
38    async fn next(&mut self) -> Result<Option<ExactScrobbleEdit>> {
39        // If we have results from the current track, return the next one
40        if self.current_track_index < self.current_track_results.len() {
41            let result = self.current_track_results[self.current_track_index].clone();
42            self.current_track_index += 1;
43            return Ok(Some(result));
44        }
45
46        // Get the next track from the iterator
47        while let Some(track) = self.tracks_iterator.next().await? {
48            // Get scrobble data for this track
49            match self
50                .client
51                .load_edit_form_values_internal(&track.name, &self.edit.artist_name_original)
52                .await
53            {
54                Ok(track_scrobbles) => {
55                    // Apply user's changes and filtering
56                    let mut modified_edits = Vec::new();
57                    for scrobble in track_scrobbles {
58                        let mut modified_edit = scrobble.clone();
59                        if let Some(new_track_name) = &self.edit.track_name {
60                            modified_edit.track_name = new_track_name.clone();
61                        }
62                        if let Some(new_album_name) = &self.edit.album_name {
63                            modified_edit.album_name = new_album_name.clone();
64                        }
65                        modified_edit.artist_name = self.edit.artist_name.clone();
66                        if let Some(new_album_artist_name) = &self.edit.album_artist_name {
67                            modified_edit.album_artist_name = new_album_artist_name.clone();
68                        }
69                        modified_edit.edit_all = self.edit.edit_all;
70                        modified_edits.push(modified_edit);
71                    }
72
73                    let filtered_edits =
74                        filter_by_original_album_artist(modified_edits, &self.edit);
75
76                    if !filtered_edits.is_empty() {
77                        // Store results and return the first one
78                        self.current_track_results = filtered_edits;
79                        self.current_track_index = 1; // We'll return the first result below
80                        return Ok(Some(self.current_track_results[0].clone()));
81                    }
82                }
83                Err(e) => {
84                    log::warn!(
85                        "Failed to get scrobble data for track '{}': {}",
86                        track.name,
87                        e
88                    );
89                    // Continue with next track
90                }
91            }
92        }
93
94        // No more tracks
95        Ok(None)
96    }
97}