diff options
| author | Jokler <jokler@protonmail.com> | 2020-01-14 19:59:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-14 19:59:27 +0100 |
| commit | 99fbb0b30d0b684fb38890843aa5ac30218ded6d (patch) | |
| tree | 86c731ad9bcfa18c9b81bdb7b1762c6065c2c7b8 | |
| parent | 6bc3285baa3e51c6fa65cfa5f37de3c56da940d6 (diff) | |
| parent | 58d7e8655d24a4f7ac846b1e27dd771717588849 (diff) | |
| download | pokebot-99fbb0b30d0b684fb38890843aa5ac30218ded6d.tar.gz pokebot-99fbb0b30d0b684fb38890843aa5ac30218ded6d.zip | |
Merge pull request #2 from Mavulp/async-ytdl
Make youtube-dl execution async again
| -rw-r--r-- | src/main.rs | 14 | ||||
| -rw-r--r-- | src/youtube_dl.rs | 6 |
2 files changed, 11 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index e19a9ae..18b9cc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,8 +115,8 @@ impl Application { self.player.play().unwrap(); } - pub fn add_audio(&self, url: String) { - match youtube_dl::get_audio_download_url(url) { + pub async fn add_audio(&self, url: String) { + match youtube_dl::get_audio_download_url(url).await { Ok((audio_url, audio_title)) => { info!("Found audio url: {}", audio_url); @@ -162,7 +162,7 @@ impl Application { self.with_teamspeak(|ts| ts.set_description(desc)); } - fn on_text(&self, message: Message) -> Result<(), AudioPlayerError> { + async fn on_text(&self, message: Message) -> Result<(), AudioPlayerError> { let msg = message.text; if msg.starts_with("!") { let tokens = msg[1..].split_whitespace().collect::<Vec<_>>(); @@ -173,7 +173,7 @@ impl Application { // strip bbcode tags from url let url = url.replace("[URL]", "").replace("[/URL]", ""); - self.add_audio(url.to_string()); + self.add_audio(url.to_string()).await; } } Some("play") => { @@ -257,14 +257,14 @@ impl Application { Ok(()) } - pub fn on_message(&self, message: ApplicationMessage) -> Result<(), AudioPlayerError> { + pub async fn on_message(&self, message: ApplicationMessage) -> Result<(), AudioPlayerError> { match message { ApplicationMessage::TextMessage(message) => { if let MessageTarget::Poke(who) = message.target { info!("Poked by {}, joining their channel", who); self.with_teamspeak(|ts| ts.join_channel_of_user(who)); } else { - self.on_text(message)?; + self.on_text(message).await?; } } ApplicationMessage::StateChange(state) => { @@ -357,7 +357,7 @@ async fn async_main() { loop { while let Some(msg) = rx.recv().await { - application.on_message(msg).unwrap(); + application.on_message(msg).await.unwrap(); } } } diff --git a/src/youtube_dl.rs b/src/youtube_dl.rs index ac4635a..d588760 100644 --- a/src/youtube_dl.rs +++ b/src/youtube_dl.rs @@ -1,8 +1,10 @@ use std::process::{Command, Stdio}; +use tokio_process::CommandExt; +use futures::compat::Future01CompatExt; use log::debug; -pub fn get_audio_download_url(uri: String) -> Result<(String, String), String> { +pub async fn get_audio_download_url(uri: String) -> Result<(String, String), String> { let ytdl_args = [ "--no-playlist", "-f", @@ -20,7 +22,7 @@ pub fn get_audio_download_url(uri: String) -> Result<(String, String), String> { debug!("yt-dl command: {:?}", cmd); - let ytdl_output = cmd.output().unwrap(); + let ytdl_output = cmd.output_async().compat().await.unwrap(); let output = String::from_utf8(ytdl_output.stdout.clone()).unwrap(); |
