Spawn a new task to fetch the URL
This commit is contained in:
parent
fb2e39c361
commit
09bbb7afdd
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1463,7 +1463,7 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "titlebot"
|
name = "titlebot"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"futures",
|
"futures",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "titlebot"
|
name = "titlebot"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
authors = ["Yash Karandikar <nerdstep710@gmail.com>"]
|
authors = ["Yash Karandikar <nerdstep710@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -6,6 +6,23 @@ use rspotify::model::{Id, PlayableItem};
|
||||||
use rspotify::prelude::BaseClient;
|
use rspotify::prelude::BaseClient;
|
||||||
use rspotify::{ClientCredsSpotify, Credentials};
|
use rspotify::{ClientCredsSpotify, Credentials};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|
||||||
|
#[allow(unused_macros)]
|
||||||
|
macro_rules! cloned {
|
||||||
|
($i:ident => $e:expr) => {{
|
||||||
|
let $i = $i.clone();
|
||||||
|
$e
|
||||||
|
}};
|
||||||
|
(($($i:ident),+) => $e:expr) => {{
|
||||||
|
$(
|
||||||
|
let $i = $i.clone();
|
||||||
|
)*
|
||||||
|
|
||||||
|
$e
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
fn calculate_playtime(secs: u64) -> (u64, u64) {
|
fn calculate_playtime(secs: u64) -> (u64, u64) {
|
||||||
let mut dur_sec = secs;
|
let mut dur_sec = secs;
|
||||||
|
@ -81,7 +98,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
}, config.nickname.as_ref().unwrap(), config.channels.join(", "), config.umodes.as_ref().unwrap());
|
}, config.nickname.as_ref().unwrap(), config.channels.join(", "), config.umodes.as_ref().unwrap());
|
||||||
let mut spotify = ClientCredsSpotify::new(Credentials::from_env().unwrap());
|
let mut spotify = ClientCredsSpotify::new(Credentials::from_env().unwrap());
|
||||||
spotify.request_token().await.unwrap();
|
spotify.request_token().await.unwrap();
|
||||||
let mut client = Client::from_config(config).await?;
|
let mut client = Arc::new(Client::from_config(config).await?);
|
||||||
client.identify()?;
|
client.identify()?;
|
||||||
let vlog = match std::env::var("TITLEBOT_VERBOSE_LOG") {
|
let vlog = match std::env::var("TITLEBOT_VERBOSE_LOG") {
|
||||||
Ok(o) => o.eq_ignore_ascii_case("true"),
|
Ok(o) => o.eq_ignore_ascii_case("true"),
|
||||||
|
@ -92,10 +109,10 @@ async fn main() -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
println!("Connected!");
|
println!("Connected!");
|
||||||
|
|
||||||
let mut stream = client.stream()?;
|
let mut stream = Arc::get_mut(&mut client).unwrap().stream()?;
|
||||||
|
|
||||||
let url_regex = Regex::new(r"https?://\w+\.\w+[/\S+]*").unwrap();
|
let url_regex = Regex::new(r"https?://\w+\.\w+[/\S+]*").unwrap();
|
||||||
let title_regex = Regex::new(r"(?<=<title>)(.*)(?=</title>)").unwrap();
|
let title_regex = Arc::new(Regex::new(r"(?<=<title>)(.*)(?=</title>)").unwrap());
|
||||||
let spotify_regex = Regex::new(r"(?:https?|spotify):(?://open\.spotify\.com/)?(track|artist|album|playlist)[/:]([a-zA-Z0-9]*)").unwrap();
|
let spotify_regex = Regex::new(r"(?:https?|spotify):(?://open\.spotify\.com/)?(track|artist|album|playlist)[/:]([a-zA-Z0-9]*)").unwrap();
|
||||||
|
|
||||||
let hclient = reqwest::Client::new();
|
let hclient = reqwest::Client::new();
|
||||||
|
@ -126,10 +143,12 @@ async fn main() -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if let Some(m) = url_regex.find(&message) {
|
} else if let Some(m) = url_regex.find(&message) {
|
||||||
let url = &message[m.0..m.1];
|
let url = message[m.0..m.1].to_string();
|
||||||
if let Ok(title) = resolve_url(&hclient, url, &title_regex).await {
|
tokio::spawn(cloned!((client, hclient, title_regex, url) => async move {
|
||||||
client.send_privmsg(&channel, title)?;
|
if let Ok(title) = resolve_url(&hclient, &url, &title_regex).await {
|
||||||
}
|
client.send_privmsg(&channel, title).unwrap();
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue