Skip to content

Commit

Permalink
Imgur logging and cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Radiicall committed Apr 20, 2024
1 parent e21f29a commit a78707b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 50 deletions.
49 changes: 14 additions & 35 deletions jellyfin-rpc-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub use jellyfin_rpc::core::rpc::show_paused;
pub use jellyfin_rpc::prelude::*;
pub use jellyfin_rpc::services::imgur::*;
use log::{error, info, warn};
use simple_logger::SimpleLogger;
use retry::retry_with_index;
use simple_logger::SimpleLogger;
use time::macros::format_description;
#[cfg(feature = "updates")]
mod updates;
Expand Down Expand Up @@ -60,13 +60,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if std::env::var("RUST_LOG").is_err() {
let _ = tokio::task::spawn_blocking(move || {
std::env::set_var("RUST_LOG", &args.log_level);
}).await;
})
.await;
}

SimpleLogger::new()
.with_level(log::LevelFilter::Info)
.env()
.with_timestamp_format(format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
.with_timestamp_format(format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second]"
))
.init()
.unwrap();

Expand All @@ -90,26 +93,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.ok();

let config = Config::load(&config_path).unwrap_or_else(|e| {
error!("{} {:?}", "Config can't be loaded:".bold().red(), e);
error!(
"{} {}",
format!(
"Config can't be loaded: {:?}.\nConfig file should be located at:",
e
)
.red()
.bold(),
"Config file should be located at:".bold().red(),
config_path
);
std::process::exit(2)
});

if !args.suppress_warnings && config.jellyfin.self_signed_cert.is_some_and(|val| val) {
warn!(
"{}",
"Self-signed certificates are enabled!"
.bold()
.red()
);
warn!("{}", "Self-signed certificates are enabled!".bold().red());
}

if !args.suppress_warnings
Expand Down Expand Up @@ -230,14 +224,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
{
// Print what we're watching
if !connected {
info!(
"{}",
content.details.bright_cyan().bold()
);
info!(
"{}",
content.state_message.bright_cyan().bold()
);
info!("{}", content.details.bright_cyan().bold());
info!("{}", content.state_message.bright_cyan().bold());
// Set connected to true so that we don't try to connect again
connected = true;
}
Expand Down Expand Up @@ -338,14 +326,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.bright_green()
.bold(),
);
info!(
"{}",
content.details.bright_cyan().bold()
);
info!(
"{}",
content.state_message.bright_cyan().bold()
);
info!("{}", content.details.bright_cyan().bold());
info!("{}", content.state_message.bright_cyan().bold());
});
} else if connected {
// Disconnect from the client
Expand All @@ -354,10 +336,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.expect("Failed to clear activity");
// Set connected to false so that we dont try to disconnect again
connected = false;
info!(
"{}",
"Cleared Rich Presence".bright_red().bold(),
);
info!("{}", "Cleared Rich Presence".bright_red().bold(),);
}

tokio::time::sleep(tokio::time::Duration::from_secs(args.wait_time as u64)).await;
Expand Down
2 changes: 1 addition & 1 deletion jellyfin-rpc/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Config {

let data = std::fs::read_to_string(path)?;
let config: Config = serde_json::from_str(&data)?;

debug!("Config loaded successfully");

Ok(config)
Expand Down
4 changes: 2 additions & 2 deletions jellyfin-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub mod core;
pub mod prelude;
/// External connections
pub mod services;
use log::info;
use colored::Colorize;
pub use crate::core::error;
use colored::Colorize;
pub use core::rpc::setactivity;
use discord_rich_presence::DiscordIpc;
use discord_rich_presence::DiscordIpcClient;
use log::info;
use retry::retry_with_index;
#[cfg(test)]
mod tests;
Expand Down
16 changes: 13 additions & 3 deletions jellyfin-rpc/src/services/imgur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Imgur {

let mut json = Imgur::read_file(file.clone())?;
if let Some(value) = json.get(item_id).and_then(Value::as_str) {
debug!("Found imgur url {} for item id {}", value, item_id);
return Ok(Self {
url: value.to_string(),
});
Expand Down Expand Up @@ -129,6 +130,9 @@ impl Imgur {
.await?
.bytes()
.await?;

debug!("Got image bytes from url: {}", image_url);

let client = reqwest::Client::new();
let response = client
.post("https://api.imgur.com/3/image")
Expand All @@ -139,11 +143,17 @@ impl Imgur {
.body(img)
.send()
.await?;
let val: Value = serde_json::from_str(&response.text().await?)?;
let val: Value = response.json().await?;

debug!("Imgur response after upload: {:#?}", val);

Ok(val["data"]["link"]
let image_url = val["data"]["link"]
.as_str()
.ok_or(ImgurError::InvalidResponse)?
.to_string())
.to_string();

debug!("Image uploaded to {}", image_url);

Ok(image_url)
}
}
18 changes: 9 additions & 9 deletions jellyfin-rpc/src/services/jellyfin.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::core::config::{Config, Display, Username};
use crate::core::error::ContentError;
use async_recursion::async_recursion;
use log::{debug, error};
use serde::{de::Visitor, Deserialize, Serialize};
use serde_json::Value;
use log::{debug, error};

#[derive(Default, Clone)]
struct ContentBuilder {
Expand Down Expand Up @@ -248,23 +248,23 @@ impl Content {
debug!("Season {}, episode {}", season, first_episode_number);

let mut state;

if config.jellyfin.show_simple.unwrap_or(false) {
name = ""
};

if config.jellyfin.append_prefix.unwrap_or(false) {
if config.jellyfin.add_divider.unwrap_or(false) {
state = format!("S{:02} - E{:02}", season, first_episode_number);
state = format!("S{:02} - E{:02}", season, first_episode_number);
} else {
state = format!("S{:02}E{:02}", season, first_episode_number);
}
} else {
if config.jellyfin.add_divider.unwrap_or(false) {
state = format!("S{} - E{}", season, first_episode_number);
} else {
state = format!("S{}E{}", season, first_episode_number);
}
if config.jellyfin.add_divider.unwrap_or(false) {
state = format!("S{} - E{}", season, first_episode_number);
} else {
state = format!("S{}E{}", season, first_episode_number);
}
};

// Does this if statement work?
Expand All @@ -287,7 +287,7 @@ impl Content {
}

if !config.jellyfin.show_simple.unwrap_or(false) {
state += &(" ".to_string() + name);
state += &(" ".to_string() + name);
}

content.media_type(MediaType::Episode);
Expand Down

0 comments on commit a78707b

Please sign in to comment.