diff options
| author | Jokler <jokler@protonmail.com> | 2020-01-14 20:55:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-14 20:55:46 +0100 |
| commit | 7e3ef6868ec138992ca22e96539acf385afb8a1c (patch) | |
| tree | 7c78c590577e2837107be3534730fcb850fcebad /src/youtube_dl.rs | |
| parent | 99fbb0b30d0b684fb38890843aa5ac30218ded6d (diff) | |
| download | pokebot-7e3ef6868ec138992ca22e96539acf385afb8a1c.tar.gz pokebot-7e3ef6868ec138992ca22e96539acf385afb8a1c.zip | |
Use youtube-dl json output for metadata access (#3)
* Use youtube-dl json output for metadata access
* Fix: meta_data -> metadata
Diffstat (limited to 'src/youtube_dl.rs')
| -rw-r--r-- | src/youtube_dl.rs | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/src/youtube_dl.rs b/src/youtube_dl.rs index d588760..a917c54 100644 --- a/src/youtube_dl.rs +++ b/src/youtube_dl.rs @@ -2,17 +2,22 @@ use std::process::{Command, Stdio}; use tokio_process::CommandExt; use futures::compat::Future01CompatExt; +use serde::{Serialize, Deserialize}; + use log::debug; -pub async fn get_audio_download_url(uri: String) -> Result<(String, String), String> { +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct AudioMetadata { + pub url: String, + pub title: Option<String>, +} + +pub async fn get_audio_download_url(uri: String) -> Result<AudioMetadata, String> { let ytdl_args = [ "--no-playlist", "-f", "bestaudio/best", - "-g", - "--get-filename", - "-o", - "%(title)s", + "-j", &uri, ]; @@ -24,15 +29,12 @@ pub async fn get_audio_download_url(uri: String) -> Result<(String, String), Str let ytdl_output = cmd.output_async().compat().await.unwrap(); - let output = String::from_utf8(ytdl_output.stdout.clone()).unwrap(); - if ytdl_output.status.success() == false { return Err(String::from_utf8(ytdl_output.stderr.clone()).unwrap()); } - let lines = output.lines().collect::<Vec<_>>(); - let url = lines[0].to_owned(); - let title = lines[1].to_owned(); + let output_str = String::from_utf8(ytdl_output.stdout.clone()).unwrap(); + let output = serde_json::from_str(&output_str).map_err(|e| e.to_string())?; - Ok((url, title)) + Ok(output) } |
