Add spotify token refresh & improve logging
This commit is contained in:
parent
3f22c21d80
commit
9c6b18af93
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -678,9 +678,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.4"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb"
|
||||
checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109"
|
||||
dependencies = [
|
||||
"scopeguard",
|
||||
]
|
||||
|
@ -798,9 +798,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e0d047c1062aa51e256408c560894e5251f08925980e53cf1aa5bd00eec6512"
|
||||
checksum = "76e97c412795abf6c24ba30055a8f20642ea57ca12875220b854cfa501bf1e48"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
|
@ -932,9 +932,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.11.1"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb"
|
||||
checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
|
||||
dependencies = [
|
||||
"instant",
|
||||
"lock_api",
|
||||
|
@ -943,9 +943,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.8.3"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018"
|
||||
checksum = "467fce6df07c66afb48b6284f13c724a1d5a13cbfcfdf5d5ce572c8313e0c722"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"instant",
|
||||
|
|
46
src/main.rs
46
src/main.rs
|
@ -15,7 +15,10 @@ fn calculate_playtime(secs: u64) -> (u64, u64) {
|
|||
(dur_min, dur_sec)
|
||||
}
|
||||
|
||||
async fn resolve_spotify(spotify: &ClientCredsSpotify, resource_type: &str, resource_id: &str) -> anyhow::Result<String> {
|
||||
async fn resolve_spotify(spotify: &mut ClientCredsSpotify, resource_type: &str, resource_id: &str) -> anyhow::Result<String> {
|
||||
if spotify.token.as_ref().unwrap().is_expired() {
|
||||
spotify.request_token().await?;
|
||||
}
|
||||
match resource_type {
|
||||
"track" => {
|
||||
let track = spotify.track(Id::from_id(resource_id)?).await?;
|
||||
|
@ -57,12 +60,22 @@ async fn resolve_spotify(spotify: &ClientCredsSpotify, resource_type: &str, reso
|
|||
async fn main() -> anyhow::Result<()> {
|
||||
if let Some(filename) = std::env::args().nth(1) {
|
||||
let config = Config::load(filename)?;
|
||||
println!("Connecting to \"{}/{}\" {} SSL as nick \"{}\" on channels \"{}\" with umodes \"{}\"...", config.server.as_ref().unwrap(), config.port.unwrap(), match config.use_tls.unwrap() {
|
||||
true => "with",
|
||||
false => "without"
|
||||
}, config.nickname.as_ref().unwrap(), config.channels.join(", "), config.umodes.as_ref().unwrap());
|
||||
let mut spotify = ClientCredsSpotify::new(Credentials::from_env().unwrap());
|
||||
spotify.request_token().await.unwrap();
|
||||
let mut client = Client::from_config(config).await?;
|
||||
client.identify()?;
|
||||
|
||||
println!("Connected to IRC!");
|
||||
let vlog = match std::env::var("TITLEBOT_VERBOSE_LOG") {
|
||||
Ok(o) => o.eq_ignore_ascii_case("true"),
|
||||
Err(_) => false
|
||||
};
|
||||
if vlog {
|
||||
println!("Verbose logging enabled.");
|
||||
}
|
||||
println!("Connected!");
|
||||
|
||||
let mut stream = client.stream()?;
|
||||
|
||||
|
@ -73,36 +86,37 @@ async fn main() -> anyhow::Result<()> {
|
|||
let mut cache: HashMap<String, String> = HashMap::new();
|
||||
|
||||
while let Some(message) = stream.next().await.transpose()? {
|
||||
print!("{}", message);
|
||||
if vlog { print!("[IRC] {}", message) }
|
||||
|
||||
if let Command::PRIVMSG(channel, message) = message.command {
|
||||
if let Some(m) = spotify_regex.captures(&message) {
|
||||
let tp_group = m.pos(1).unwrap();
|
||||
let id_group = m.pos(2).unwrap();
|
||||
match resolve_spotify(&spotify, &message[tp_group.0..tp_group.1], &message[id_group.0..id_group.1]).await {
|
||||
match resolve_spotify(&mut spotify, &message[tp_group.0..tp_group.1], &message[id_group.0..id_group.1]).await {
|
||||
Ok(o) => client.send_privmsg(&channel, o)?,
|
||||
Err(e) => {
|
||||
client.send_privmsg(&channel, "\x037[Spotify]\x03 An error has occurred.")?;
|
||||
println!("Spotify Error! {}", e);
|
||||
println!("[Spotify] Error! {}", e);
|
||||
}
|
||||
}
|
||||
} else if let Some(m) = url_regex.find(&message) {
|
||||
let url = &message[m.0..m.1];
|
||||
if let Some(entry) = cache.get(&url.to_string()) {
|
||||
client.send_privmsg(&channel, format!("\x039[Title]\x03 \x0311{}", entry))?;
|
||||
client.send_privmsg(&channel, format!("\x039[Title]\x0311 {}", entry))?;
|
||||
continue;
|
||||
}
|
||||
if let Ok(o) = reqwest::get(url).await {
|
||||
let body = o.text().await?;
|
||||
if let Some(tm) = title_regex.find(&body) {
|
||||
let title_match = &body[tm.0..tm.1];
|
||||
let result = decode_html(title_match).unwrap_or_else(|_| title_match.to_string());
|
||||
client.send_privmsg(&channel, format!("\x039[Title]\x03 \x0311{}", result))?;
|
||||
cache.insert(url.to_string(), result.to_string());
|
||||
continue;
|
||||
match reqwest::get(url).await {
|
||||
Ok(o) => {
|
||||
let body = o.text().await?;
|
||||
if let Some(tm) = title_regex.find(&body) {
|
||||
let title_match = &body[tm.0..tm.1];
|
||||
let result = decode_html(title_match).unwrap_or_else(|_| title_match.to_string());
|
||||
client.send_privmsg(&channel, format!("\x039[Title]\x0311 {}", result))?;
|
||||
cache.insert(url.to_string(), result.to_string());
|
||||
}
|
||||
}
|
||||
Err(e) => println!("[Title] Error! {}", e)
|
||||
}
|
||||
client.send_privmsg(&channel, "\x039[Title]\x03 Could not get the title.")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue