diff options
| author | kilmanio <f.vastenhout@live.nl> | 2020-10-03 16:55:36 +0000 |
|---|---|---|
| committer | kilmanio <f.vastenhout@live.nl> | 2020-10-04 00:03:50 +0200 |
| commit | 50087bc3bec74069d21f972c246ca22a6d70c1ed (patch) | |
| tree | 9703a9073ed7fdfa237c6d678960896cb6349c46 | |
| parent | d0a0c7c541d2566c6b8f6504f9453e501bc2b1d9 (diff) | |
| download | pokebot-50087bc3bec74069d21f972c246ca22a6d70c1ed.tar.gz pokebot-50087bc3bec74069d21f972c246ca22a6d70c1ed.zip | |
Added retry functionality for youtube-dl failing
| -rw-r--r-- | src/bot/music.rs | 2 | ||||
| -rw-r--r-- | src/youtube_dl.rs | 29 |
2 files changed, 24 insertions, 7 deletions
diff --git a/src/bot/music.rs b/src/bot/music.rs index 19b1c3f..90305d0 100644 --- a/src/bot/music.rs +++ b/src/bot/music.rs @@ -185,7 +185,7 @@ impl MusicBot { } pub async fn add_audio(&self, url: String, user: String) { - match crate::youtube_dl::get_audio_download_url(url).await { + match crate::youtube_dl::get_audio_download_from_url(url).await { Ok(mut metadata) => { metadata.added_by = user; info!("Found audio url: {}", metadata.url); diff --git a/src/youtube_dl.rs b/src/youtube_dl.rs index cc708af..496d0b4 100644 --- a/src/youtube_dl.rs +++ b/src/youtube_dl.rs @@ -28,23 +28,40 @@ where Ok(dur.map(Duration::from_secs_f64)) } -pub async fn get_audio_download_url(uri: String) -> Result<AudioMetadata, String> { - let ytdl_args = ["--no-playlist", "-f", "bestaudio/best", "-j", &uri]; +pub async fn get_audio_download_from_url(uri: String) -> Result<AudioMetadata, String> { + //youtube-dl sometimes just fails, so we give it a second try + let ytdl_output = match run_youtube_dl(&uri).await { + Ok(o) => o, + Err(e) => { + if e.contains("Unable to extract video data") { + run_youtube_dl(&uri).await? + } else { + return Err(e); + } + } + }; + + let output = serde_json::from_str(&ytdl_output).map_err(|e| e.to_string())?; + + Ok(output) +} + +async fn run_youtube_dl(url: &str) -> Result<String, String> { + let ytdl_args = ["--no-playlist", "-f", "bestaudio/best", "-j", &url]; let mut cmd = Command::new("youtube-dl"); cmd.args(&ytdl_args); cmd.stdin(Stdio::null()); debug!("yt-dl command: {:?}", cmd); - let ytdl_output = cmd.output().await.unwrap(); if !ytdl_output.status.success() { - return Err(String::from_utf8(ytdl_output.stderr).unwrap()); + let s = String::from_utf8(ytdl_output.stderr).unwrap(); + return Err(s); } let output_str = String::from_utf8(ytdl_output.stdout).unwrap(); - let output = serde_json::from_str(&output_str).map_err(|e| e.to_string())?; - Ok(output) + Ok(output_str) } |
