Explorar o código

Get video information in JSON format

This allows us to also display duration and artist information.
Frans Bergman %!s(int64=2) %!d(string=hai) anos
pai
achega
fb800f7bcb
Modificáronse 4 ficheiros con 25 adicións e 20 borrados
  1. 13 6
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 5 6
      src/audio/song.rs
  4. 6 8
      src/audio/subprocess.rs

+ 13 - 6
Cargo.lock

@@ -506,7 +506,7 @@ checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11"
 dependencies = [
  "bytes 1.0.1",
  "fnv",
- "itoa",
+ "itoa 0.4.7",
 ]
 
 [[package]]
@@ -547,7 +547,7 @@ dependencies = [
  "http-body",
  "httparse",
  "httpdate",
- "itoa",
+ "itoa 0.4.7",
  "pin-project",
  "socket2",
  "tokio",
@@ -631,6 +631,12 @@ version = "0.4.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
 
+[[package]]
+name = "itoa"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
+
 [[package]]
 name = "js-sys"
 version = "0.3.51"
@@ -812,6 +818,7 @@ dependencies = [
  "futures",
  "lazy_static",
  "rand 0.8.3",
+ "serde_json",
  "serenity",
  "songbird",
  "symphonia-core",
@@ -1211,11 +1218,11 @@ dependencies = [
 
 [[package]]
 name = "serde_json"
-version = "1.0.64"
+version = "1.0.81"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
+checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c"
 dependencies = [
- "itoa",
+ "itoa 1.0.2",
  "ryu",
  "serde",
 ]
@@ -1238,7 +1245,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9"
 dependencies = [
  "form_urlencoded",
- "itoa",
+ "itoa 0.4.7",
  "ryu",
  "serde",
 ]

+ 1 - 0
Cargo.toml

@@ -14,3 +14,4 @@ serenity = {version = "0.10", features = ["standard_framework", "voice", "rustls
 songbird = "0.2.0-beta.4"
 tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "time", "sync"] }
 symphonia-core = "0.2.0"
+serde_json = "1.0.81"

+ 5 - 6
src/audio/song.rs

@@ -15,14 +15,13 @@ impl Song {
             format!("ytsearch:{}", query)
         };
 
-        let stdout = ytdl(&query).await?;
-        let mut lines = stdout.lines();
+        let video = ytdl(&query).await?;
 
         let song = Song {
-            artist: None,
-            title: lines.next().map(|t| t.to_string()),
-            url: lines.next().unwrap().to_string(),
-            duration: None,
+            artist: video["channel"].as_str().map(|t| t.to_string()),
+            title: video["title"].as_str().map(|t| t.to_string()),
+            url: video["url"].as_str().unwrap().to_string(),
+            duration: video["duration"].as_u64(),
         };
         Ok(song)
     }

+ 6 - 8
src/audio/subprocess.rs

@@ -1,3 +1,4 @@
+use serde_json::Value;
 use songbird::input::reader::MediaSource;
 use std::{
     io::BufReader,
@@ -6,15 +7,12 @@ use std::{
 use symphonia_core::io::ReadOnlySource;
 use tokio::process::Command as TokioCommand;
 
-pub async fn ytdl(query: &str) -> Result<String, std::io::Error> {
+pub async fn ytdl(query: &str) -> Result<Value, std::io::Error> {
     let mut cmd = TokioCommand::new("youtube-dl");
     let cmd = cmd
-        .arg("-x")
-        .arg("--skip-download")
-        .arg("--get-title")
-        .arg("--get-url")
-        .arg("--audio-quality")
-        .arg("128k")
+        .arg("-j")
+        .arg("-f")
+        .arg("bestaudio")
         .arg(query)
         .stdout(Stdio::piped())
         .stderr(Stdio::piped());
@@ -23,7 +21,7 @@ pub async fn ytdl(query: &str) -> Result<String, std::io::Error> {
     let stdout = String::from_utf8(output.stdout).unwrap();
     let stderr = String::from_utf8(output.stderr).unwrap();
     if output.status.success() {
-        Ok(stdout)
+        Ok(serde_json::from_str(&stdout)?)
     } else {
         Err(std::io::Error::new(
             std::io::ErrorKind::Other,