-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resolver/engine): add yt-dlp support
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
pub mod bilibili; | ||
pub mod pyncm; | ||
pub mod ytdlp; | ||
|
||
pub use async_trait::async_trait; | ||
use reqwest::Proxy; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//! UNM Resolver [Engine]: yt-dlp | ||
//! | ||
//! It can fetch audio from YouTube. | ||
|
||
use serde::Deserialize; | ||
|
||
use super::{Provider, Song, Proxy}; | ||
|
||
/// The response that `yt-dlp` will return. | ||
#[derive(Deserialize)] | ||
struct YtDlpResponse { | ||
/// The audio URL. | ||
url: String, | ||
} | ||
|
||
/// The search and track engine powered by `yt-dlp`. | ||
pub struct YtDlpEngine; | ||
|
||
#[async_trait::async_trait] | ||
impl Provider for YtDlpEngine { | ||
// TODO: allow specifying proxy | ||
async fn check(&self, info: &Song, _: Option<Proxy>) -> anyhow::Result<Option<String>> { | ||
Ok(fetch_from_youtube(&info.keyword()).await?.map(|r| r.url)) | ||
} | ||
} | ||
|
||
/// Get the response from `yt-dlp`. | ||
/// | ||
/// ```plain | ||
/// yt-dlp -f bestaudio --dump-json ytsearch1:{keyword} | ||
/// -f bestaudio choose the best quality of the audio | ||
/// --dump-json dump the information as JSON without downloading it | ||
/// ``` | ||
async fn fetch_from_youtube(keyword: &str) -> anyhow::Result<Option<YtDlpResponse>> { | ||
let mut cmd = tokio::process::Command::new("yt-dlp"); | ||
|
||
let child = cmd | ||
.args(&["-f", "bestaudio", "--dump-json"]) | ||
.arg(format!("ytsearch1:{keyword}")) | ||
.kill_on_drop(true) | ||
.output() | ||
.await?; | ||
|
||
if child.status.success() { | ||
let response = String::from_utf8_lossy(&child.stdout); | ||
|
||
Ok(if response.is_empty() { | ||
None | ||
} else { | ||
let json = serde_json::from_str::<'_, YtDlpResponse>(&response)?; | ||
Some(json) | ||
}) | ||
} else { | ||
log::error!("Failed to run `youtube-dl`."); | ||
log::error!("Code: {code:?}", code = child.status.code()); | ||
log::error!("Stderr: {}", String::from_utf8_lossy(&child.stderr)); | ||
|
||
Err(anyhow::anyhow!("Failed to run `youtube-dl`.")) | ||
} | ||
} |