Replace own youtube-dl impl by youtube_dl crate (refs: #8)

* Drop the depend on the `tokio` crate, because we don't need to
  run our own processes anymore.
* Remove unnecessary error variant for command failure
This commit is contained in:
Paul van Tilburg 2022-05-26 20:37:27 +02:00
parent a67df934bf
commit 3ec1879932
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
4 changed files with 33 additions and 28 deletions

24
Cargo.lock generated
View File

@ -1703,7 +1703,7 @@ dependencies = [
"rss",
"tempfile",
"thiserror",
"tokio",
"youtube_dl",
]
[[package]]
@ -2664,6 +2664,15 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wait-timeout"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
dependencies = [
"libc",
]
[[package]]
name = "walkdir"
version = "2.3.2"
@ -2883,3 +2892,16 @@ name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
[[package]]
name = "youtube_dl"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3e3d041033acf677f28d7d79dc1f9207dfadf86ec05b82c5492126254d90a5d"
dependencies = [
"log",
"serde",
"serde_json",
"tokio",
"wait-timeout",
]

View File

@ -17,7 +17,7 @@ rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["tera"] }
rss = "2.0.1"
tempfile = "3"
thiserror = "1.0.31"
tokio = { version = "1.6.1", features = ["process"] }
youtube_dl = { version = "0.7.0", features = ["tokio"] }
[package.metadata.deb]
maintainer = "Paul van Tilburg <paul@luon.net>"

View File

@ -10,7 +10,6 @@
#![deny(missing_docs)]
use std::path::PathBuf;
use std::process::ExitStatus;
use chrono::{DateTime, NaiveDateTime, Utc};
use rocket::fairing::AdHoc;
@ -26,16 +25,12 @@ use rss::extension::itunes::{
use rss::{
CategoryBuilder, ChannelBuilder, EnclosureBuilder, GuidBuilder, ImageBuilder, ItemBuilder,
};
use tokio::process::Command;
pub(crate) mod mixcloud;
/// The possible errors that can occur.
#[derive(Debug, thiserror::Error)]
pub(crate) enum Error {
#[error("Command failed: {0:?} exited with {1}")]
CommandFailed(Command, ExitStatus),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
@ -47,6 +42,9 @@ pub(crate) enum Error {
#[error("Unknown supported back-end: {0}")]
UnsupportedBackend(String),
#[error("Youtube_dl failed: {0}")]
YoutubeDl(#[from] youtube_dl::Error),
}
impl<'r, 'o: 'r> rocket::response::Responder<'r, 'o> for Error {

View File

@ -3,12 +3,10 @@
//! It uses the Mixcloud API to retrieve the feed (user) and items (cloudcasts)).
//! See also: <https://www.mixcloud.com/developers/>
use std::process::Stdio;
use chrono::{DateTime, Utc};
use reqwest::Url;
use rocket::serde::Deserialize;
use tokio::process::Command;
use youtube_dl::{YoutubeDl, YoutubeDlOutput};
use super::{Error, Result};
@ -135,27 +133,14 @@ pub(crate) async fn get_cloudcasts(username: &str) -> Result<Vec<Cloudcast>> {
/// Retrieves the redirect URL for the provided Mixcloud cloudcast key.
pub(crate) async fn redirect_url(key: &str) -> Result<String> {
let mut cmd = Command::new("youtube-dl");
cmd.args(&["--format", "http"])
.arg("--get-url")
.arg(&format!("{FILES_BASE_URL}{key}"))
.stdout(Stdio::piped());
let url = format!("{FILES_BASE_URL}{key}");
println!("🌍 Determining direct URL for {key}...");
let output = cmd.output().await?;
if output.status.success() {
let direct_url = String::from_utf8_lossy(&output.stdout)
.trim_end()
.to_owned();
let output = YoutubeDl::new(&url).run_async().await?;
if direct_url.is_empty() {
Err(Error::NoRedirectUrlFound)
} else {
println!(" Found direct URL: {direct_url}");
Ok(direct_url)
}
if let YoutubeDlOutput::SingleVideo(yt_item) = output {
yt_item.url.ok_or(Error::NoRedirectUrlFound)
} else {
Err(Error::CommandFailed(cmd, output.status))
Err(Error::NoRedirectUrlFound)
}
}