Fix naming and structure

This commit is contained in:
IndyV 2022-11-01 22:15:02 +01:00
parent 32c03321ae
commit a640c8a4eb
8 changed files with 506 additions and 281 deletions

169
Cargo.lock generated
View File

@ -2,6 +2,15 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
@ -64,6 +73,22 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1"
dependencies = [
"iana-time-zone",
"js-sys",
"num-integer",
"num-traits",
"serde",
"time",
"wasm-bindgen",
"winapi",
]
[[package]]
name = "clap"
version = "4.0.17"
@ -101,6 +126,16 @@ dependencies = [
"os_str_bytes",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width",
]
[[package]]
name = "console"
version = "0.15.2"
@ -131,6 +166,50 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "cxx"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b7d4e43b25d3c994662706a1d4fcfc32aaa6afd287502c111b237093bb23f3a"
dependencies = [
"cc",
"cxxbridge-flags",
"cxxbridge-macro",
"link-cplusplus",
]
[[package]]
name = "cxx-build"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84f8829ddc213e2c1368e51a2564c552b65a8cb6a28f31e576270ac81d5e5827"
dependencies = [
"cc",
"codespan-reporting",
"once_cell",
"proc-macro2",
"quote",
"scratch",
"syn",
]
[[package]]
name = "cxxbridge-flags"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e72537424b474af1460806647c41d4b6d35d09ef7fe031c5c2fa5766047cc56a"
[[package]]
name = "cxxbridge-macro"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "309e4fb93eed90e1e14bea0da16b209f81813ba9fc7830c20ed151dd7bc0a4d7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "dialoguer"
version = "0.10.2"
@ -323,7 +402,7 @@ checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
@ -437,6 +516,30 @@ dependencies = [
"tokio-native-tls",
]
[[package]]
name = "iana-time-zone"
version = "0.1.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"winapi",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca"
dependencies = [
"cxx",
"cxx-build",
]
[[package]]
name = "idna"
version = "0.3.0"
@ -516,6 +619,15 @@ version = "0.2.135"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c"
[[package]]
name = "link-cplusplus"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369"
dependencies = [
"cc",
]
[[package]]
name = "lock_api"
version = "0.4.9"
@ -555,7 +667,7 @@ checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf"
dependencies = [
"libc",
"log",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys",
]
@ -577,6 +689,25 @@ dependencies = [
"tempfile",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
@ -843,6 +974,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "scratch"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898"
[[package]]
name = "security-framework"
version = "2.7.0"
@ -914,6 +1051,7 @@ name = "sharextended"
version = "0.2.0"
dependencies = [
"ansi_term",
"chrono",
"clap",
"console",
"dialoguer",
@ -928,7 +1066,6 @@ dependencies = [
"serde_json",
"tinyfiledialogs",
"tokio",
"toml",
]
[[package]]
@ -1035,6 +1172,17 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]]
name = "tinyfiledialogs"
version = "3.9.1"
@ -1115,15 +1263,6 @@ dependencies = [
"tracing",
]
[[package]]
name = "toml"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]
[[package]]
name = "tower-service"
version = "0.3.2"
@ -1216,6 +1355,12 @@ dependencies = [
"try-lock",
]
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View File

@ -7,18 +7,18 @@ edition = "2021"
[dependencies]
eyre = "0.6.8"
toml = "0.5.9"
tokio = { version = "1", features = ["full"] }
futures = "0.3.24"
serde = { version = "1.0.104", features = ["derive"] }
serde_json = "1.0.86"
clap = { version = "4.0.17", features = ["derive"] }
reqwest = { version = "0.11", features = ["json"] }
ansi_term = "0.12.1"
chrono = { version = "0.4.22", features = ["serde"] }
indicatif = "0.17.1"
dialoguer = "0.10.2"
tinyfiledialogs = "3.9.1"
open = "3.0.3"
lazy_static = "1.4.0"
console = "0.15.2"
futures = "0.3.24"
directories = "4.0.1"

View File

@ -1,23 +1,31 @@
# ShareXtended
A simple program that deletes all images that are hosted on online image hosting by sending POST requests to all DeletionURL's.
A Rusty program that provides ShareX utility commands.
## Usage
```ps1
./sharextended.exe --help
```
Usage: sharextended.exe [COMMAND]
### TODO
- Unwrap and expect should be replaced with proper matching or flow (make good use of Option/Result).
- Add GitHub action to build and release binaries (Windows only, since ShareX is Windows only). See [actions/rust-release](https://github.com/marketplace/actions/rust-release) or [actions/rust-release-binary](https://github.com/marketplace/actions/rust-release-binary)
- Implement concurrent or parallel requests to become "Blazingly Fast™ 🔥" (see https://stackoverflow.com/a/51047786)
Commands:
purge-online Deletes all images that are hosted on Imgur
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help information
-V, --version Print version information
```
> If `[COMMAND]` is not specified, the program will run in interactive mode.
## Motivation
### Why Rust?
I wanted to try out the most loved language for myself.
While it's a bit hard to get started with due to these new concepts, like ownership, borrowing, and lifetimes, I really like it.
While this project could have also been done in many other ways like:
- C#, powerful and also matching the language of the ShareX project
- Or even simpler as a Powershell or Python script probably
I just wanted to try out the most loved language for myself. It also allows for easy distribution with easy cross-platform compilation.
It's a bit hard to get started with due to these new concepts, like ownership, borrowing, and lifetimes, but I really like it.
It's a very powerful language with great error messages and many useful crates. I'm looking forward to learn more about it.
### Code
@ -32,3 +40,19 @@ For now I omit the `clippy::unwrap_used`. It's not great to use unwrap as often
I tried to store the static strings in the `Cargo.toml`, but I figured it's not meant to live outside the code. Also tried to resolve `%USERPROFILE%` in the code but solved it with the `directories` crate.
I used https://transform.tools/json-to-rust-serde as a reference for the JSON structure.
#### Crates used
- `eyre`: For simple error handling
- `tokyo` & `futures`: For writing async Rust
- `serde` & `serde_json`: For JSON parsing
- `clap`: For CLI parsing
- `reqwest`: For HTTP requests
- `ansi_term`: For colored output
- `chrono`: For time parsing and formatting
- `indicatif`: For progress bars
- `dialoguer`: For interactive prompts
- `tinyfiledialogs`: For file dialogs
- `open`: For opening files in the default application
- `lazy_static`: For static variables that require evaluation at runtime
- `console`: For console input/output
- `directories`: For getting the user's home directory

View File

@ -1,4 +1,4 @@
mod mass_delete;
mod purge_online;
mod util;
use clap::Parser;
@ -11,8 +11,8 @@ async fn main() {
let command = args.command;
match command {
Some(util::Command::MassDelete { path }) => {
mass_delete::handler(path).await.unwrap();
Some(util::Command::PurgeOnline { path }) => {
purge_online::handler(path).await.unwrap();
}
None => {
show_menu().await.unwrap();
@ -24,9 +24,9 @@ async fn show_menu() -> Result<()> {
loop {
println!();
let menu_response: usize = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Pick a option (use arrow keys to select, enter to confirm)")
.with_prompt("Pick an option (use arrow keys to select, enter to confirm)")
.items(&[
"1. Mass delete online history items",
"1. Purge online history items",
"2. Open ShareX Website",
"3. View source code (GitHub)",
"Exit",
@ -43,7 +43,7 @@ async fn show_menu() -> Result<()> {
async fn handle_option(number: usize) -> Result<()> {
match number {
0 => {
mass_delete::handler(None).await?;
purge_online::handler(None).await?;
}
1 => {
util::open_webpage(util::SHAREX_URL.to_string().as_str());

View File

@ -1,249 +0,0 @@
use crate::util;
use ansi_term::{self, Colour};
use clap::Parser;
use directories::{BaseDirs, UserDirs};
use eyre::Result;
use serde::{Deserialize, Serialize};
use std::thread;
use std::{
io::Read,
path::{Path, PathBuf},
time::Duration,
};
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct HistoryItem {
#[serde(rename = "FileName")]
pub file_name: String,
#[serde(rename = "FilePath")]
pub file_path: Option<String>,
#[serde(rename = "DateTime")]
pub date_time: String,
#[serde(rename = "Type")]
pub type_field: String,
#[serde(rename = "Host")]
pub host: String,
#[serde(rename = "Tags")]
pub tags: Option<Tags>,
#[serde(rename = "URL")]
pub url: Option<String>,
#[serde(rename = "ThumbnailURL")]
pub thumbnail_url: Option<String>,
#[serde(rename = "DeletionURL")]
pub deletion_url: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Tags {
#[serde(rename = "WindowTitle")]
pub window_title: Option<String>,
#[serde(rename = "ProcessName")]
pub process_name: String,
}
pub async fn handler(pathflag: Option<PathBuf>) -> Result<()> {
// TODO
// If a pathflag is Some() it means it's passed via a flag, use that path and skip the prompting
// Otherwise, prompt the user to select a default path (depending on type of installation), or tinyfiledialog for a path, or manual input
let path = match pathflag {
Some(pathflag) => pathflag,
None => get_path_input(),
};
let file = prompt_history_file(&path);
if !Path::new(&file).exists() {
eprintln!(
"{} The directory does not exist. Given path: {:?}",
Colour::Red.paint("Error:"),
&file
);
std::process::exit(1);
}
let history_urls = get_history_urls(&file);
delete_urls(history_urls).await?;
Ok(())
}
fn prompt_history_file(path: &Path) -> PathBuf {
// TODO: Use dialoguer select to prompt the user to select the history file, either with tinyfiledialogs or manual input
return tinyfiledialogs::open_file_dialog(
"Choose where sharex history is stored",
path.to_str().unwrap(),
Some((&["History.json", "*.json"], "History.json")),
)
.map_or_else(
|| {
eprintln!("No file selected, exiting...");
std::process::exit(1);
},
PathBuf::from,
);
}
fn get_history_urls(path: &PathBuf) -> Result<Vec<String>> {
let spinner = util::setup_spinner("Reading and parsing JSON...");
let history_json = read_history_json(path)?;
let history_items = parse_history_json(&history_json)?;
let deletion_urls = get_deletion_urls(&history_items);
// spinner.finish_and_clear();
spinner.finish_with_message(format!("Done! {} items found", deletion_urls.len()));
Ok(deletion_urls)
}
fn get_path_input() -> PathBuf {
let args = util::Args::parse();
let path = match args.command {
Some(util::Command::MassDelete { path }) => path,
None => None,
};
let default_history_path = get_default_history_path();
match path {
Some(path) => path,
None => default_history_path,
}
}
fn get_default_history_path() -> PathBuf {
let document_directory: PathBuf = UserDirs::new().map_or_else(
|| BaseDirs::new().unwrap().home_dir().join("Documents"),
|user_dirs| user_dirs.document_dir().unwrap().to_path_buf(),
);
let default_history_path: PathBuf = document_directory.join("ShareX").join("History.json");
default_history_path
}
fn read_history_json(path: &PathBuf) -> Result<String> {
let mut file = std::fs::File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
// Since ShareX history is invalid JSON we add brackets to make it valid JSON
contents = format!("[{}]", contents);
Ok(contents)
}
fn parse_history_json(json: &str) -> Result<Vec<HistoryItem>, serde_json::Error> {
let history_items: Vec<HistoryItem> = serde_json::from_str(json)?;
Ok(history_items)
}
fn get_deletion_urls(items: &[HistoryItem]) -> Vec<String> {
items
.iter()
.filter(|item| item.deletion_url.is_some() && item.deletion_url != Some("".to_string()))
.map(|item| item.deletion_url.clone().unwrap())
.collect()
}
async fn delete_urls(deletion_urls: Result<Vec<String>>) -> Result<()> {
let deletion_urls = deletion_urls?;
let progress_bar = util::setup_progressbar(deletion_urls.len());
// progress_bar.enable_steady_tick(Duration::from_millis(500)); // This only visually updates the ticker once every 500ms instead of when the tick occurs
// ? Maybe use Rayon to parallelize the requests and run them through public proxies to prevent rate limiting?
for url in deletion_urls {
let (remaining, limit, reset) = send_deletion(&url).await?;
println!(
"Remaining: {} Limit: {} Reset: {}",
Colour::Green.paint(remaining),
Colour::Yellow.paint(limit),
Colour::Red.paint(reset)
);
progress_bar.inc(1);
thread::sleep(Duration::from_millis(100));
}
// let client = reqwest::Client::new();
// let mut futures = Vec::new();
// for url in deletion_urls {
// let future = client.delete(&url).send();
// futures.push(future);
// }
// let results = futures::future::join_all(futures).await;
// for result in results {
// match result {
// Ok(response) => {
// if response.status().is_success() {
// println!("Deleted {}", response.url());
// } else {
// eprintln!("Failed to delete {}", response.url());
// }
// }
// Err(e) => {
// eprintln!("Failed to delete {}", e);
// }
// }
// }
progress_bar.finish_with_message("Done!");
Ok(())
}
async fn send_deletion(url: &str) -> Result<(String, String, String)> {
let client = reqwest::Client::new();
let params = [("confirm", true)];
let resp = client.post(url).form(&params).send().await?;
println!("{:#?}", resp);
match resp.status() {
reqwest::StatusCode::OK => {
println!("OK");
}
reqwest::StatusCode::TOO_MANY_REQUESTS => {
println!("TOO MANY REQUESTS");
}
reqwest::StatusCode::BAD_GATEWAY => {
println!("BAD GATEWAY");
}
_ => {
println!("Not OK");
}
}
// I don't understand Rust enough so the stuff below looks kinda cursed
let headers = resp.headers().clone();
let remaining = headers
.get("x-post-rate-limit-remaining")
.unwrap()
.to_str()?
.to_owned();
let limit = headers
.get("x-post-rate-limit-limit")
.unwrap()
.to_str()?
.to_owned();
let reset = headers
.get("x-post-rate-limit-Reset")
.unwrap()
.to_str()?
.to_owned();
println!(
"Remaining: {} Limit: {} Reset: {}",
Colour::Green.paint(&remaining),
Colour::Yellow.paint(&limit),
Colour::Red.paint(&reset)
);
print!("{:?}", headers);
Ok((remaining, limit, reset))
}

View File

@ -1,2 +1,2 @@
mod mass_delete;
mod purge_online;
mod util;

304
src/purge_online.rs Normal file
View File

@ -0,0 +1,304 @@
use crate::util;
use ansi_term::{self, Colour};
use chrono::prelude::*;
use chrono::Duration;
use clap::Parser;
use dialoguer::{theme::ColorfulTheme, Input, Select};
use directories::{BaseDirs, UserDirs};
use eyre::Result;
use serde::{Deserialize, Serialize};
use std::{
io::Read,
path::{Path, PathBuf},
};
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct HistoryItem {
#[serde(rename = "FileName")]
pub file_name: String,
#[serde(rename = "FilePath")]
pub file_path: Option<String>,
#[serde(rename = "DateTime")]
pub date_time: DateTime<Local>,
#[serde(rename = "Type")]
pub type_field: String,
#[serde(rename = "Host")]
pub host: String,
#[serde(rename = "Tags")]
pub tags: Option<Tags>,
#[serde(rename = "URL")]
pub url: Option<String>,
#[serde(rename = "ThumbnailURL")]
pub thumbnail_url: Option<String>,
#[serde(rename = "DeletionURL")]
pub deletion_url: Option<String>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Tags {
#[serde(rename = "WindowTitle")]
pub window_title: Option<String>,
#[serde(rename = "ProcessName")]
pub process_name: String,
}
pub async fn handler(pathflag: Option<PathBuf>) -> Result<()> {
// If a pathflag is Some() it means it's passed via a flag, use that path and skip the prompting
// Otherwise, prompt the user to select a default path (depending on type of installation), or tinyfiledialog for a path, or manual input
// Maybe call prompt_history_file in the Some and None. Call get_path_input in prompt_history_file
let path = match pathflag {
Some(pathflag) => pathflag,
None => get_path_input(),
};
let file = prompt_history_file(&path);
if file == None {
return Ok(());
}
let history_urls = get_history_urls(file.unwrap());
delete_urls(history_urls).await?;
Ok(())
}
fn prompt_history_file(path: &Path) -> Option<PathBuf> {
println!();
let menu_response: usize = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Pick an option (use arrow keys to select, enter to confirm)")
.items(&[
"Use default path",
"Use file picker",
"Manual input",
"Cancel",
])
.default(0)
.interact()
.unwrap();
println!();
match menu_response {
0 => Some(path.to_path_buf()),
1 => Some(
tinyfiledialogs::open_file_dialog(
"Choose where sharex history is stored",
path.to_str().unwrap(),
Some((&["History.json", "*.json"], "History.json")),
)
.map_or_else(
|| {
eprintln!("No file selected, exiting...");
std::process::exit(1);
},
PathBuf::from,
),
),
2 => {
// TODO: While till valid path given or exit?
match Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("Enter the exact path to your history file")
.interact_on(&console::Term::stdout())
{
Ok(path) => {
if !PathBuf::from(&path).exists() {
eprintln!("A valid path was not specified. Exiting.");
std::process::exit(1);
}
Some(PathBuf::from(path))
}
Err(e) => {
eprintln!("An error occurred: {}", e);
std::process::exit(1);
}
}
}
3 => {
println!("Canceling operation...");
None
}
_ => {
println!("Invalid option");
std::process::exit(1);
}
}
}
fn get_history_urls(path: PathBuf) -> Result<Vec<String>> {
let spinner = util::setup_spinner("Reading and parsing JSON...");
let history_json = read_history_json(path)?;
let history_items = parse_history_json(history_json)?;
let deletion_urls = filter_deletion_urls(&history_items, None);
spinner.finish_with_message(format!("Done! {} items found", deletion_urls.len()));
Ok(deletion_urls)
}
fn get_path_input() -> PathBuf {
let args = util::Args::parse();
let path = match args.command {
Some(util::Command::PurgeOnline { path }) => path,
None => None,
};
let default_history_path = get_default_history_path();
match path {
Some(path) => path,
None => default_history_path,
}
}
fn get_default_history_path() -> PathBuf {
let document_directory: PathBuf = UserDirs::new().map_or_else(
|| BaseDirs::new().unwrap().home_dir().join("Documents"),
|user_dirs| user_dirs.document_dir().unwrap().to_path_buf(),
);
let default_history_path: PathBuf = document_directory.join("ShareX").join("History.json");
default_history_path
}
fn read_history_json(path: PathBuf) -> Result<String> {
let mut file = std::fs::File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
// Since ShareX history is invalid JSON we add brackets to make it valid JSON
contents = format!("[{}]", contents);
Ok(contents)
}
fn parse_history_json(json: String) -> Result<Vec<HistoryItem>> {
let history_items: Vec<HistoryItem> = serde_json::from_str(&json)?;
Ok(history_items)
}
fn filter_deletion_urls(items: &[HistoryItem], from_date: Option<DateTime<Local>>) -> Vec<String> {
/*
TODO: Filter out items that don't have a deletion url but return the type like &[HistoryItem]
This is because we need to match on HistoryItem Host when calling send_deletion
*/
items
.iter()
.filter(|item| {
item.deletion_url.is_some()
&& item.deletion_url != Some("".to_string())
&& item.host == "Imgur".to_string()
&& item.date_time > from_date.unwrap_or_else(|| Local::now() - Duration::days(1))
})
.map(|item| item.deletion_url.clone().unwrap())
.collect()
}
async fn delete_urls(deletion_urls: Result<Vec<String>>) -> Result<()> {
let deletion_urls = deletion_urls?;
if deletion_urls.len() == 0 {
println!("No items to delete");
return Ok(());
}
if deletion_urls.len() > 1250 {
println!("Amount of items to delete is too high for Imgur API, exiting...");
return Ok(());
}
let progress_bar = util::setup_progressbar(deletion_urls.len());
// progress_bar.enable_steady_tick(Duration::from_millis(500)); // This only visually updates the ticker once every 500ms instead of when the tick occurs
let client = reqwest::Client::new();
let mut futures = Vec::new();
// Maybe limit size to 1,250 POST requests per hour
// https://api.imgur.com/#limits
for url in deletion_urls {
// let future = client.delete(&url).send();
let params = [("confirm", true)];
let future = client.post(&url).form(&params).send();
futures.push(future);
}
let results = futures::future::join_all(futures).await;
for result in results {
progress_bar.inc(1);
// Check the headers here for rate limits?
match result {
Ok(response) => {
if response.status().is_success() {
println!("Deleted {}", response.url());
} else {
eprintln!("Failed to delete {}", response.url());
}
}
Err(e) => {
eprintln!("Failed to delete {}", e);
}
}
}
progress_bar.finish_with_message("Done!");
Ok(())
}
// async fn send_deletion(url: &str) -> Result<(String, String, String)> {
// // TODO: Only have 1 client (for multiple requests) and use a proxy to prevent rate limiting
// // TODO: Send different type of body by matching on the host
// let client = reqwest::Client::new();
// let params = [("confirm", true)];
// let resp = client.post(url).form(&params).send().await?;
// println!("{:#?}", resp);
// match resp.status() {
// reqwest::StatusCode::OK => {
// println!("OK");
// }
// reqwest::StatusCode::TOO_MANY_REQUESTS => {
// println!("TOO MANY REQUESTS");
// }
// reqwest::StatusCode::BAD_GATEWAY => {
// println!("BAD GATEWAY");
// }
// _ => {
// println!("Not OK");
// }
// }
// // I don't understand Rust enough so the stuff below looks kinda cursed
// let headers = resp.headers().clone();
// let remaining = headers
// .get("x-post-rate-limit-remaining")
// .unwrap()
// .to_str()?
// .to_owned();
// let limit = headers
// .get("x-post-rate-limit-limit")
// .unwrap()
// .to_str()?
// .to_owned();
// let reset = headers
// .get("x-post-rate-limit-Reset")
// .unwrap()
// .to_str()?
// .to_owned();
// println!(
// "Remaining: {} Limit: {} Reset: {}",
// Colour::Green.paint(&remaining),
// Colour::Yellow.paint(&limit),
// Colour::Red.paint(&reset)
// );
// print!("{:?}", headers);
// Ok((remaining, limit, reset))
// }

View File

@ -11,7 +11,7 @@ lazy_static! {
}
#[derive(Parser, Debug)]
#[clap(version = "1.0", author = "IndyV", about = "Delete all ShareX online history items", long_about = None)]
#[clap(version = "1.0", author = "IndyV", about = None, long_about = None)]
pub struct Args {
#[command(subcommand)]
pub command: Option<Command>,
@ -19,7 +19,8 @@ pub struct Args {
#[derive(clap::Subcommand, Clone, Debug)]
pub enum Command {
MassDelete {
#[clap(about = "Deletes all images that are hosted on Imgur")]
PurgeOnline {
#[clap(short, long)]
path: Option<PathBuf>,
},