Switch to youtube-metadata for basic video information fetching

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/22/head
trivernis 3 years ago
parent 9cc9b720e2
commit 6f4d9df4b7
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

187
Cargo.lock generated

@ -314,6 +314,12 @@ dependencies = [
"syn",
]
[[package]]
name = "convert_case"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]]
name = "core-foundation"
version = "0.9.1"
@ -406,6 +412,33 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "cssparser"
version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "754b69d351cdc2d8ee09ae203db831e005560fc6030da058f86ad60c92a9cb0a"
dependencies = [
"cssparser-macros",
"dtoa-short",
"itoa",
"matches",
"phf",
"proc-macro2",
"quote",
"smallvec",
"syn",
]
[[package]]
name = "cssparser-macros"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfae75de57f2b2e85e8768c3ea840fd159c8f33e2b6522c7835b7abac81be16e"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "dashmap"
version = "4.0.2"
@ -416,6 +449,18 @@ dependencies = [
"num_cpus",
]
[[package]]
name = "derive_more"
version = "0.99.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f82b1b72f1263f214c0f823371768776c4f5841b942c9883aa8e5ec584fd0ba6"
dependencies = [
"convert_case",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "diesel"
version = "1.4.6"
@ -484,6 +529,27 @@ version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "dtoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0"
[[package]]
name = "dtoa-short"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bde03329ae10e79ede66c9ce4dc930aa8599043b0743008548680f25b91502d6"
dependencies = [
"dtoa",
]
[[package]]
name = "ego-tree"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591"
[[package]]
name = "either"
version = "1.6.1"
@ -675,6 +741,15 @@ dependencies = [
"slab",
]
[[package]]
name = "fxhash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
dependencies = [
"byteorder",
]
[[package]]
name = "generator"
version = "0.6.25"
@ -698,6 +773,15 @@ dependencies = [
"version_check",
]
[[package]]
name = "getopts"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
dependencies = [
"unicode-width",
]
[[package]]
name = "getrandom"
version = "0.1.16"
@ -1218,6 +1302,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
[[package]]
name = "nodrop"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "ntapi"
version = "0.3.6"
@ -1356,7 +1446,9 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"
dependencies = [
"phf_macros",
"phf_shared",
"proc-macro-hack",
]
[[package]]
@ -1379,6 +1471,20 @@ dependencies = [
"rand 0.7.3",
]
[[package]]
name = "phf_macros"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6fde18ff429ffc8fe78e2bf7f8b7a5a5a6e2a8b58bc5a9ac69198bbda9189c"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "phf_shared"
version = "0.8.0"
@ -1677,9 +1783,9 @@ dependencies = [
[[package]]
name = "reqwest"
version = "0.11.2"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf12057f289428dbf5c591c74bf10392e4a8003f993405a902f20117019022d4"
checksum = "2296f2fac53979e8ccbc4a1136b25dcefd37be9ed7e4a1f6b05a6029c84ff124"
dependencies = [
"base64 0.13.0",
"bytes 1.0.1",
@ -1837,6 +1943,22 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "scraper"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48e02aa790c80c2e494130dec6a522033b6a23603ffc06360e9fe6c611ea2c12"
dependencies = [
"cssparser",
"ego-tree",
"getopts",
"html5ever",
"matches",
"selectors",
"smallvec",
"tendril",
]
[[package]]
name = "sct"
version = "0.6.0"
@ -1881,6 +2003,26 @@ dependencies = [
"markup5ever_rcdom",
]
[[package]]
name = "selectors"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df320f1889ac4ba6bc0cdc9c9af7af4bd64bb927bccdf32d81140dc1f9be12fe"
dependencies = [
"bitflags 1.2.1",
"cssparser",
"derive_more",
"fxhash",
"log 0.4.14",
"matches",
"phf",
"phf_codegen",
"precomputed-hash",
"servo_arc",
"smallvec",
"thin-slice",
]
[[package]]
name = "semver"
version = "0.9.0"
@ -1999,6 +2141,16 @@ dependencies = [
"serde_repr",
]
[[package]]
name = "servo_arc"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d98238b800e0d1576d8b6e3de32827c2d74bee68bb97748dcf5071fb53965432"
dependencies = [
"nodrop",
"stable_deref_trait",
]
[[package]]
name = "sha-1"
version = "0.9.4"
@ -2105,6 +2257,12 @@ dependencies = [
"lock_api",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "static_assertions"
version = "1.1.0"
@ -2270,6 +2428,12 @@ dependencies = [
"winapi 0.2.8",
]
[[package]]
name = "thin-slice"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c"
[[package]]
name = "thiserror"
version = "1.0.24"
@ -2348,6 +2512,7 @@ dependencies = [
"tokio",
"trigram",
"typemap_rev",
"youtube-metadata",
]
[[package]]
@ -2573,6 +2738,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.0.3"
@ -2849,6 +3020,18 @@ dependencies = [
"zeroize",
]
[[package]]
name = "youtube-metadata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c3fe87fc0ceab2e8b36d76c273dacb9841aff323800f40f5ac01130cf3bf2d1"
dependencies = [
"lazy_static",
"reqwest",
"scraper",
"thiserror",
]
[[package]]
name = "zeroize"
version = "1.2.0"

@ -37,4 +37,5 @@ chrono-tz = "0.5.3"
sauce-api = "0.7.1"
rustc_version_runtime = "0.2.0"
trigram = "0.4.4"
typemap_rev = "0.1.5"
typemap_rev = "0.1.5"
youtube-metadata = "0.1.1"

@ -44,6 +44,7 @@ use bot_database::Database;
use futures::future::BoxFuture;
use futures::FutureExt;
use serenity::framework::standard::{Args, CommandOptions, Reason};
use youtube_metadata::get_video_information;
mod clear_queue;
mod current;
@ -330,7 +331,7 @@ async fn get_songs_for_query(ctx: &Context, msg: &Message, query: &str) -> BotRe
// if no songs were found fetch the song as a video
if songs.len() == 0 {
log::debug!("Query is youtube video");
let mut song: Song = youtube_dl::get_video_information(&query).await?.into();
let mut song: Song = get_video_information(&query).await?.into();
added_one_msg(&ctx, msg, &mut song).await?;
add_youtube_song_to_database(&store, &database, &mut song).await?;
songs.push(song);
@ -474,10 +475,7 @@ async fn get_youtube_song_for_track(database: &Database, track: Track) -> BotRes
if let Some(song) = &entry {
// check if the video is still available
if youtube_dl::check_video_available(&song.url)
.await
.unwrap_or(false)
{
if get_video_information(&song.url).await.is_err() {
log::debug!("Video '{}' is not available. Deleting entry", song.url);
database.delete_song(song.id).await?;
return Ok(None);

@ -217,3 +217,15 @@ impl From<YoutubeSong> for Song {
}
}
}
impl From<youtube_metadata::VideoInformation> for Song {
fn from(i: youtube_metadata::VideoInformation) -> Self {
Self {
title: i.title,
author: i.uploader,
url: Some(i.url.clone()),
thumbnail: i.thumbnail,
source: SongSource::YouTube(i.url),
}
}
}

@ -11,7 +11,7 @@ pub(crate) struct PlaylistEntry {
#[derive(Deserialize, Clone, Debug)]
pub(crate) struct VideoInformation {
id: String,
pub(crate) id: String,
pub title: String,
pub thumbnail: Option<String>,
pub webpage_url: String,

@ -28,25 +28,6 @@ pub(crate) async fn get_videos_for_playlist(url: &str) -> BotResult<Vec<Playlist
Ok(videos)
}
/// Runs youtube-dl to check if a video is available
/// Doesn't parse the output
pub async fn check_video_available(url: &str) -> BotResult<bool> {
log::debug!("Checking if video {} is available", url);
let output = youtube_dl(&["--no-warnings", "--skip-download", "--get-id", "-i", url]).await?;
Ok(output.is_empty())
}
/// Returns information for a single video by using youtube-dl
pub(crate) async fn get_video_information(url: &str) -> BotResult<VideoInformation> {
log::debug!("Fetching information for '{}'", url);
let output = youtube_dl(&["--no-warnings", "--dump-json", "-i", url]).await?;
let information = serde_json::from_str(&*output)?;
Ok(information)
}
/// Searches for a video
pub(crate) async fn search_video_information(query: String) -> BotResult<Option<VideoInformation>> {
log::debug!("Searching for video '{}'", query);

@ -41,6 +41,9 @@ pub enum BotError {
#[error("JoinError: {0}")]
JoinError(#[from] songbird::error::JoinError),
#[error("YouTube Error: {0}")]
YoutubeError(#[from] youtube_metadata::error::YoutubeError),
#[error("{0}")]
Msg(String),
}

@ -61,6 +61,11 @@ pub fn init_logger() {
.level_for("mio", log::LevelFilter::Warn)
.level_for("songbird", log::LevelFilter::Warn)
.level_for("html5ever", log::LevelFilter::Warn)
.level_for("scraper", log::LevelFilter::Warn)
.level_for("html5ever", log::LevelFilter::Warn)
.level_for("cssparser", log::LevelFilter::Warn)
.level_for("selectors", log::LevelFilter::Warn)
.level_for("matches", log::LevelFilter::Warn)
.chain(std::io::stdout())
.chain(
fern::log_file(log_dir.join(PathBuf::from(format!(

Loading…
Cancel
Save