Skip to content

Commit

Permalink
Merge pull request #15 from jangerhard/add_request_date_sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Supergamer1337 authored Oct 31, 2024
2 parents 672992d + 349cdbd commit fb5a7fd
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 38 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ You can also pass an argument to the program to skip the sorting screen and go s
- `-n`: Sort by name
- `-nd`: Sort by name, in descending order
- `-t`: Sort by media type
- `-r`: Sort by request date
- `-rd`: Sort by request date, in descending order

#### Getting a list of all media

Expand Down
69 changes: 37 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use itertools::Itertools;
use overseerr::MediaRequest;
use shared::{Order, SortingOption, SortingValue};
use std::{io, process::Command};

use std::cmp::PartialEq;
use arguments::Arguments;
use config::Config;
use dialoguer::MultiSelect;
Expand All @@ -30,15 +30,17 @@ async fn main() -> Result<()> {

Arguments::read_args()?;

let mut deletion_items = get_deletion_items().await?;
let deletion_items = get_deletion_items().await?;

show_requests_result(&deletion_items)?;

clear_screen()?;

let chosen = choose_items_to_delete(&mut deletion_items)?;
let sorted_requests = choose_sorting(deletion_items)?;

let chosen_indexes = choose_items_to_delete(&sorted_requests)?;

delete_chosen_items(&mut deletion_items, &chosen).await?;
delete_chosen_items(sorted_requests, chosen_indexes).await?;

Ok(())
}
Expand Down Expand Up @@ -168,16 +170,14 @@ fn show_requests_result(requests: &Vec<CompleteMediaItem>) -> Result<()> {
Ok(())
}

fn choose_items_to_delete(requests: &mut Vec<CompleteMediaItem>) -> Result<Vec<usize>> {
choose_sorting(requests)?;

fn choose_items_to_delete(requests: &Vec<CompleteMediaItem>) -> Result<Vec<usize>> {
clear_screen()?;

let items_to_show = Config::global().items_shown;
let chosen: Vec<usize> = MultiSelect::new()
.with_prompt("Choose what media to delete (SPACE to select, ENTER to confirm selection)")
.max_length(items_to_show)
.items(&requests)
.items(requests)
.interact()?;

if chosen.len() == 0 {
Expand All @@ -187,14 +187,13 @@ fn choose_items_to_delete(requests: &mut Vec<CompleteMediaItem>) -> Result<Vec<u

clear_screen()?;

verify_chosen(requests, &chosen)?;
verify_chosen(&requests, &chosen)?;

Ok(chosen)
}

fn choose_sorting(requests: &mut Vec<CompleteMediaItem>) -> Result<()> {
fn choose_sorting(mut requests: Vec<CompleteMediaItem>) -> Result<Vec<CompleteMediaItem>> {
clear_screen()?;

let args = Arguments::get_args();

let sort = match args.sorting {
Expand All @@ -205,15 +204,16 @@ fn choose_sorting(requests: &mut Vec<CompleteMediaItem>) -> Result<()> {
match sort.sorting_value {
SortingValue::Name => (),
SortingValue::Size => requests.sort_by_key(|req| req.get_disk_size()),
SortingValue::Type => requests.sort_by_key(|req| req.media_type),
};
SortingValue::Type => requests.sort_by_key(|req| req.media_type.clone()),
SortingValue::RequestedDate => requests.sort_by_key(|req| req.get_requested_date()),
}

match sort.sorting_direction {
Order::Asc => (),
Order::Desc => requests.reverse(),
};
// Reverse if sorting direction is descending
if sort.sorting_direction == Order::Desc {
requests.reverse();
}

Ok(())
Ok(requests)
}

fn choose_sorting_dialogue() -> Result<SortingOption> {
Expand All @@ -224,6 +224,8 @@ fn choose_sorting_dialogue() -> Result<SortingOption> {
println!("Size - Descending: s");
println!("Size - Ascending: sa");
println!("Type - Descending: t");
println!("Requested Date - Ascending: r");
println!("Requested Date - Descending: rd");

let input = get_user_input()?;

Expand Down Expand Up @@ -275,32 +277,35 @@ fn verify_chosen(requests: &Vec<CompleteMediaItem>, chosen: &Vec<usize>) -> Resu
}

async fn delete_chosen_items(
requests: &mut Vec<CompleteMediaItem>,
chosen: &Vec<usize>,
mut requests: Vec<CompleteMediaItem>,
chosen: Vec<usize>,
) -> Result<()> {
let mut errs: Vec<(String, Report)> = Vec::new();

for selection in chosen.into_iter().rev() {
let media_item = requests.swap_remove(*selection);
let media_item = requests.swap_remove(selection);
let title = media_item.title.clone();
if let Err(err) = media_item.remove_from_server().await {
errs.push((title, err));
}
}

if errs.len() > 0 {
println!("Had some errors deleting items:\n");
errs.iter().for_each(|err| {
println!(
"Got the following error while deleting {}: {}",
err.0, err.0
);
print_line();
});

wait(None)?;
// If there are no errors, return early
if errs.is_empty() {
return Ok(());
}

// Log errors if there are any
println!("Had some errors deleting items:\n");
errs.iter().for_each(|(title, err)| {
println!(
"Got the following error while deleting {}: {}",
title, err
);
print_line();
});

wait(None)?;
Ok(())
}

Expand Down
5 changes: 5 additions & 0 deletions src/media_item.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use color_eyre::{eyre::eyre, owo_colors::OwoColorize, Result};
use std::fmt::{Debug, Display};
use chrono::{DateTime, Utc};
use tokio::try_join;

use crate::{
Expand Down Expand Up @@ -175,6 +176,10 @@ impl CompleteMediaItem {
Ok(())
}

pub fn get_requested_date(&self) -> Option<DateTime<Utc>> {
self.request.as_ref().map(|request| request.created_at)
}

pub fn get_disk_size(&self) -> i64 {
match (self.arr_data.as_ref(), self.arr_4k_data.as_ref()) {
(Some(arr_data), None) => arr_data.get_disk_size(),
Expand Down
2 changes: 1 addition & 1 deletion src/overseerr/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use color_eyre::owo_colors::OwoColorize;
use serde::Deserialize;
use serde_repr::Deserialize_repr;
use std::fmt::Display;

use openssl::pkey::Public;

Check warning on line 5 in src/overseerr/responses.rs

View workflow job for this annotation

GitHub Actions / linux-armv7

unused import: `openssl::pkey::Public`

Check warning on line 5 in src/overseerr/responses.rs

View workflow job for this annotation

GitHub Actions / linux-x86_64

unused import: `openssl::pkey::Public`

Check warning on line 5 in src/overseerr/responses.rs

View workflow job for this annotation

GitHub Actions / windows-x86_64

unused import: `openssl::pkey::Public`
use crate::shared::MediaType;

#[derive(Debug, Deserialize)]
Expand Down
33 changes: 28 additions & 5 deletions src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,22 @@ pub enum Order {
Asc,
}

impl PartialEq for Order {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Order::Asc, Order::Asc) => true,
(Order::Desc, Order::Desc) => true,
_ => false,
}
}
}

#[derive(Debug, Clone, Copy)]
pub enum SortingValue {
Name,
Size,
Type,
RequestedDate,
}

#[derive(Debug, Clone)]
Expand All @@ -50,35 +61,47 @@ impl SortingOption {
pub fn from_str(s: &str) -> Result<Self> {
match s {
"nd" => {
return Ok(SortingOption {
Ok(SortingOption {
sorting_value: SortingValue::Name,
sorting_direction: Order::Desc,
})
}
"n" => {
return Ok(SortingOption {
Ok(SortingOption {
sorting_value: SortingValue::Name,
sorting_direction: Order::Asc,
})
}
"sa" => {
return Ok(SortingOption {
Ok(SortingOption {
sorting_value: SortingValue::Size,
sorting_direction: Order::Asc,
})
}
"s" => {
return Ok(SortingOption {
Ok(SortingOption {
sorting_value: SortingValue::Size,
sorting_direction: Order::Desc,
})
}
"t" => {
return Ok(SortingOption {
Ok(SortingOption {
sorting_value: SortingValue::Type,
sorting_direction: Order::Desc,
})
}
"r" => {
Ok(SortingOption {
sorting_value: SortingValue::RequestedDate,
sorting_direction: Order::Asc,
})
}
"rd" => {
Ok(SortingOption {
sorting_value: SortingValue::RequestedDate,
sorting_direction: Order::Desc,
})
}
_ => Err(eyre!("Not a valid Sorting Option")),
}
}
Expand Down

0 comments on commit fb5a7fd

Please sign in to comment.