Skip to content

Commit

Permalink
implemented workaround for failing lyrics downloads
Browse files Browse the repository at this point in the history
For some reason there started to be failures when downloading tracks.
The errors have the following message:
	Client specified an invalid argument { Response status code: 400 Bad Request }
I tracked this down to the lyrics download and I don't since this is being made
by the spotify library that I don't controll or know much about the workaround
implemented will instead just return no lyrics and print a warning instead of
failing to fetch metadata from the track.
  • Loading branch information
diogo464 committed Apr 20, 2024
1 parent f64a2eb commit 48b6d66
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions src/fetcher/spotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,42 @@ impl SpotifyMetadataFetcher {
}
}
}

async fn get_lyrics_for(
&self,
id: crate::SpotifyId,
track: &librespot::metadata::Track,
) -> std::io::Result<Option<Lyrics>> {
if !track.has_lyrics {
return Ok(None);
}
let lyrics = self.get_librespot_lyrics(id).await?;
let inner = lyrics.lyrics;
let kind = match inner.sync_type {
librespot::metadata::lyrics::SyncType::Unsynced => {
let lines = inner.lines.into_iter().map(|l| l.words).collect::<Vec<_>>();

LyricsKind::Unsynchronized(lines)
}
librespot::metadata::lyrics::SyncType::LineSynced => {
let lines = inner
.lines
.into_iter()
.map(|l| SyncedLine {
start_time: Duration::from_millis(l.start_time_ms.parse().unwrap()),
end_time: Duration::from_millis(l.end_time_ms.parse().unwrap()),
text: l.words,
})
.collect::<Vec<_>>();

LyricsKind::Synchronized(lines)
}
};
Ok(Some(Lyrics {
language: inner.language,
kind,
}))
}
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -200,35 +236,15 @@ impl MetadataFetcher for SpotifyMetadataFetcher {

async fn get_track(&self, id: crate::SpotifyId) -> std::io::Result<crate::metadata::Track> {
let track = self.get_librespot_track(id).await?;
let lyrics = if track.has_lyrics {
let lyrics = self.get_librespot_lyrics(id).await?;
let inner = lyrics.lyrics;
let kind = match inner.sync_type {
librespot::metadata::lyrics::SyncType::Unsynced => {
let lines = inner.lines.into_iter().map(|l| l.words).collect::<Vec<_>>();

LyricsKind::Unsynchronized(lines)
}
librespot::metadata::lyrics::SyncType::LineSynced => {
let lines = inner
.lines
.into_iter()
.map(|l| SyncedLine {
start_time: Duration::from_millis(l.start_time_ms.parse().unwrap()),
end_time: Duration::from_millis(l.end_time_ms.parse().unwrap()),
text: l.words,
})
.collect::<Vec<_>>();

LyricsKind::Synchronized(lines)
}
};
Some(Lyrics {
language: inner.language,
kind,
})
} else {
None

let lyrics = match self.get_lyrics_for(id, &track).await {
Ok(v) => v,
Err(err) => {
tracing::warn!(
"failed to fetch lyrics for {id}, returning None instead of failing, error: {err}"
);
None
}
};

let artists = track
Expand Down

0 comments on commit 48b6d66

Please sign in to comment.