Browse Source

Setup proper logging, now downloads into a folder

master
Jamie Quigley 9 months ago
parent
commit
880245bd4d
Signed by: jamie GPG Key ID: 8E8FF66E2AE8D970
  1. 152
      Cargo.lock
  2. 7
      Cargo.toml
  3. 50
      src/main.rs

152
Cargo.lock

@ -6,6 +6,15 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
@ -36,7 +45,7 @@ dependencies = [
"futures-util",
"log",
"pin-project 1.0.3",
"tokio",
"tokio 0.2.24",
"tokio-rustls",
"tungstenite",
"webpki-roots",
@ -195,10 +204,13 @@ dependencies = [
name = "emoji-archiver"
version = "0.1.0"
dependencies = [
"chrono",
"log",
"pretty_env_logger",
"reqwest",
"serenity",
"structopt",
"tokio",
"tokio 1.0.1",
]
[[package]]
@ -210,6 +222,19 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "flate2"
version = "1.0.19"
@ -387,7 +412,7 @@ dependencies = [
"http",
"indexmap",
"slab",
"tokio",
"tokio 0.2.24",
"tokio-util",
"tracing",
"tracing-futures",
@ -450,6 +475,15 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]]
name = "hyper"
version = "0.13.9"
@ -468,7 +502,7 @@ dependencies = [
"itoa",
"pin-project 1.0.3",
"socket2",
"tokio",
"tokio 0.2.24",
"tower-service",
"tracing",
"want",
@ -485,7 +519,7 @@ dependencies = [
"hyper",
"log",
"rustls",
"tokio",
"tokio 0.2.24",
"tokio-rustls",
"webpki",
]
@ -499,7 +533,7 @@ dependencies = [
"bytes",
"hyper",
"native-tls",
"tokio",
"tokio 0.2.24",
"tokio-tls",
]
@ -711,6 +745,16 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.5.2"
@ -832,6 +876,16 @@ version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "pretty_env_logger"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
dependencies = [
"env_logger",
"log",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -877,6 +931,12 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "1.0.8"
@ -933,6 +993,24 @@ version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "regex"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]
[[package]]
name = "regex-syntax"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
@ -971,7 +1049,7 @@ dependencies = [
"serde",
"serde_json",
"serde_urlencoded",
"tokio",
"tokio 0.2.24",
"tokio-rustls",
"tokio-tls",
"url",
@ -1119,7 +1197,7 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
"tokio",
"tokio 0.2.24",
"tracing",
"tracing-futures",
"typemap_rev",
@ -1217,6 +1295,15 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@ -1226,6 +1313,15 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
dependencies = [
"lazy_static",
]
[[package]]
name = "time"
version = "0.1.44"
@ -1267,7 +1363,19 @@ dependencies = [
"mio",
"pin-project-lite 0.1.11",
"slab",
"tokio-macros",
"tokio-macros 0.2.6",
]
[[package]]
name = "tokio"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d258221f566b6c803c7b4714abadc080172b272090cdc5e244a6d4dd13c3a6bd"
dependencies = [
"autocfg",
"num_cpus",
"pin-project-lite 0.2.1",
"tokio-macros 1.0.0",
]
[[package]]
@ -1281,6 +1389,17 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-macros"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42517d2975ca3114b22a16192634e8241dc5cc1f130be194645970cc1c371494"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-rustls"
version = "0.14.1"
@ -1289,7 +1408,7 @@ checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a"
dependencies = [
"futures-core",
"rustls",
"tokio",
"tokio 0.2.24",
"webpki",
]
@ -1300,7 +1419,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a70f4fcd7b3b24fb194f837560168208f669ca8cb70d0c4b862944452396343"
dependencies = [
"native-tls",
"tokio",
"tokio 0.2.24",
]
[[package]]
@ -1314,7 +1433,7 @@ dependencies = [
"futures-sink",
"log",
"pin-project-lite 0.1.11",
"tokio",
"tokio 0.2.24",
]
[[package]]
@ -1637,6 +1756,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

7
Cargo.toml

@ -7,11 +7,14 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.19"
log = "0.4.11"
pretty_env_logger = "0.4.0"
reqwest = "0.10.10"
structopt = "0.3"
tokio = {version = "0.2", features = ["macros"]}
tokio = {version = "1.0", features = ["macros", "rt-multi-thread"]}
[dependencies.serenity]
default-features=false
features=["client", "gateway", "rustls_backend", "model"]
features=["client", "gateway", "rustls_backend", "model", "cache"]
version = "0.9.3"

50
src/main.rs

@ -1,3 +1,4 @@
use log::*;
use std::env;
use std::fs::File;
use std::io;
@ -14,6 +15,11 @@ const PATH_PREFIX: &str = "/dev/shm/";
#[tokio::main]
async fn main() {
// Set default log level to info unless otherwise specified.
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "emoji_archiver=info");
}
pretty_env_logger::init();
let opts = Opt::from_args();
let token = if opts.token.is_some() {
opts.token.unwrap()
@ -37,7 +43,7 @@ async fn main() {
// Shards will automatically attempt to reconnect, and will perform
// exponential backoff until it reconnects.
if let Err(why) = client.start().await {
println!("Client error: {:?}", why);
error!("Client error: {:?}", why);
}
}
@ -47,18 +53,35 @@ struct Handler;
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!archive" {
let guild_name = msg.guild(&ctx.cache).await.unwrap().name;
info!(
"Archive started by {} in server '{}'",
msg.author.name, guild_name
);
let server_id = msg.guild_id.unwrap();
let emojis = server_id.emojis(&ctx.http).await.unwrap();
let download_subdirectory = format!(
"{}-{}",
guild_name.replace(" ", "-").to_lowercase(),
chrono::Utc::now().format("%Y-%m-%dT%H-%M-%S")
);
for emoji in emojis {
println!("{:?}", emoji);
let url = emoji.url();
println!("{}", url);
let name = &emoji.name;
let ext = if emoji.animated { "gif" } else { "png" };
let path = format!("{}/{}.{}", PATH_PREFIX, name, ext);
let ext = &url[url
.rfind(".")
.expect("Emoji url does not have a file extension")..];
let path = format!("{}/{}/{}{}", PATH_PREFIX, download_subdirectory, name, ext);
info!(
"Downloading emoji {} from url {} to location {}...",
name, url, path
);
download_url(&url, &path).await.unwrap();
}
msg.reply(&ctx, "Archive complete.").await.expect("Error sending message");
msg.reply(&ctx, "Archive complete.")
.await
.expect("Error sending message");
info!("Archive complete.");
}
}
@ -69,7 +92,7 @@ impl EventHandler for Handler {
//
// In this case, just print what the current user's username is.
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
info!("{} is connected!", ready.user.name);
}
}
@ -78,21 +101,28 @@ async fn download_url<P: AsRef<Path>>(
destination: P,
) -> Result<(), Box<dyn std::error::Error>> {
let response = reqwest::get(url).await.unwrap();
let mut dest = {
let destdir = destination
.as_ref()
.parent()
.expect("Destination path did not have a parent");
if !destdir.is_dir() {
std::fs::create_dir(destdir)?;
};
let fname = response
.url()
.path_segments()
.and_then(|segments| segments.last())
.and_then(|name| if name.is_empty() { None } else { Some(name) })
.unwrap_or("<unnamed>");
println!("file to download: '{}'", fname);
println!("Will be stored in '{:?}'", destination.as_ref());
trace!("file to download: '{}'", fname);
trace!("Will be stored in '{:?}'", destination.as_ref());
File::create(destination.as_ref())?
};
let mut content = io::Cursor::new(response.bytes().await?);
std::io::copy(&mut content, &mut dest)?;
info!("Download complete");
Ok(())
}

Loading…
Cancel
Save