From 7e3ef6868ec138992ca22e96539acf385afb8a1c Mon Sep 17 00:00:00 2001 From: Jokler Date: Tue, 14 Jan 2020 20:55:46 +0100 Subject: Use youtube-dl json output for metadata access (#3) * Use youtube-dl json output for metadata access * Fix: meta_data -> metadata --- src/youtube_dl.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/youtube_dl.rs') 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, +} + +pub async fn get_audio_download_url(uri: String) -> Result { 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::>(); - 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) } -- cgit v1.2.3-70-g09d2