Skip to content

Commit

Permalink
feat: add relative date and relative date short formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Beastwick18 committed Oct 2, 2024
1 parent c37d5cf commit 2ce5820
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ impl App {
search,
ctx.config.sources.clone(),
ctx.theme.clone(),
ctx.config.date_format.clone(),
ctx.config.clone().into(),
));
last_load_abort = Some(task.abort_handle());
continue; // Redraw
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct Config {
pub source: Sources,
pub download_client: Client,
pub date_format: Option<String>,
pub relative_date: Option<bool>,
pub relative_date_short: Option<bool>,
pub request_proxy: Option<String>,
pub timeout: u64,
pub scroll_padding: usize,
Expand All @@ -75,6 +77,8 @@ impl Default for Config {
download_client: Client::Cmd,
theme: Theme::default().name,
date_format: None,
relative_date: None,
relative_date_short: None,
request_proxy: None,
timeout: 30,
scroll_padding: 3,
Expand Down
66 changes: 38 additions & 28 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use torrent_galaxy::TgxTheme;

use crate::{
app::{Context, LoadType, Widgets},
config::Config,
results::{ResultResponse, ResultTable, Results},
sync::SearchQuery,
theme::Theme,
Expand Down Expand Up @@ -67,6 +68,22 @@ pub struct SourceConfig {
pub tgx: Option<TgxConfig>,
}

pub struct SourceExtraConfig {
pub date_format: Option<String>,
pub relative_date: Option<bool>,
pub relative_date_short: Option<bool>,
}

impl From<Config> for SourceExtraConfig {
fn from(c: Config) -> Self {
SourceExtraConfig {
date_format: c.date_format,
relative_date: c.relative_date,
relative_date_short: c.relative_date_short,
}
}
}

#[derive(Clone)]
pub struct SourceInfo {
pub cats: Vec<CatStruct>,
Expand Down Expand Up @@ -171,32 +188,32 @@ pub trait Source {
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> impl std::future::Future<Output = Result<SourceResponse, Box<dyn Error + Send + Sync>>> + Send;
fn sort(
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> impl std::future::Future<Output = Result<SourceResponse, Box<dyn Error + Send + Sync>>> + Send;
fn filter(
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> impl std::future::Future<Output = Result<SourceResponse, Box<dyn Error + Send + Sync>>> + Send;
fn categorize(
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> impl std::future::Future<Output = Result<SourceResponse, Box<dyn Error + Send + Sync>>> + Send;
fn solve(
solution: String,
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> impl std::future::Future<Output = Result<SourceResponse, Box<dyn Error + Send + Sync>>> + Send;
fn info() -> SourceInfo;
fn load_config(config: &mut SourceConfig);
Expand All @@ -221,61 +238,54 @@ impl Sources {
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> Result<SourceResponse, Box<dyn Error + Send + Sync>> {
match self {
Sources::Nyaa => match load_type {
LoadType::Searching | LoadType::Sourcing => {
NyaaHtmlSource::search(client, search, config, date_format).await
}
LoadType::Sorting => {
NyaaHtmlSource::sort(client, search, config, date_format).await
}
LoadType::Filtering => {
NyaaHtmlSource::filter(client, search, config, date_format).await
NyaaHtmlSource::search(client, search, config, extra).await
}
LoadType::Sorting => NyaaHtmlSource::sort(client, search, config, extra).await,
LoadType::Filtering => NyaaHtmlSource::filter(client, search, config, extra).await,
LoadType::Categorizing => {
NyaaHtmlSource::categorize(client, search, config, date_format).await
NyaaHtmlSource::categorize(client, search, config, extra).await
}
LoadType::SolvingCaptcha(solution) => {
NyaaHtmlSource::solve(solution, client, search, config, date_format).await
NyaaHtmlSource::solve(solution, client, search, config, extra).await
}
LoadType::Downloading | LoadType::Batching => unreachable!(),
},
Sources::SukebeiNyaa => match load_type {
LoadType::Searching | LoadType::Sourcing => {
SukebeiHtmlSource::search(client, search, config, date_format).await
}
LoadType::Sorting => {
SukebeiHtmlSource::sort(client, search, config, date_format).await
SukebeiHtmlSource::search(client, search, config, extra).await
}
LoadType::Sorting => SukebeiHtmlSource::sort(client, search, config, extra).await,
LoadType::Filtering => {
SukebeiHtmlSource::filter(client, search, config, date_format).await
SukebeiHtmlSource::filter(client, search, config, extra).await
}
LoadType::Categorizing => {
SukebeiHtmlSource::categorize(client, search, config, date_format).await
SukebeiHtmlSource::categorize(client, search, config, extra).await
}
LoadType::SolvingCaptcha(solution) => {
SukebeiHtmlSource::solve(solution, client, search, config, date_format).await
SukebeiHtmlSource::solve(solution, client, search, config, extra).await
}
LoadType::Downloading | LoadType::Batching => unreachable!(),
},
Sources::TorrentGalaxy => match load_type {
LoadType::Searching | LoadType::Sourcing => {
TorrentGalaxyHtmlSource::search(client, search, config, date_format).await
TorrentGalaxyHtmlSource::search(client, search, config, extra).await
}
LoadType::Sorting => {
TorrentGalaxyHtmlSource::sort(client, search, config, date_format).await
TorrentGalaxyHtmlSource::sort(client, search, config, extra).await
}
LoadType::Filtering => {
TorrentGalaxyHtmlSource::filter(client, search, config, date_format).await
TorrentGalaxyHtmlSource::filter(client, search, config, extra).await
}
LoadType::Categorizing => {
TorrentGalaxyHtmlSource::categorize(client, search, config, date_format).await
TorrentGalaxyHtmlSource::categorize(client, search, config, extra).await
}
LoadType::SolvingCaptcha(solution) => {
TorrentGalaxyHtmlSource::solve(solution, client, search, config, date_format)
.await
TorrentGalaxyHtmlSource::solve(solution, client, search, config, extra).await
}
LoadType::Downloading | LoadType::Batching => unreachable!(),
},
Expand Down
48 changes: 31 additions & 17 deletions src/source/nyaa_html.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Write;
use std::{cmp::max, error::Error, time::Duration};

use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
Expand All @@ -11,6 +12,7 @@ use serde::{Deserialize, Serialize};
use strum::{Display, FromRepr, VariantArray};
use urlencoding::encode;

use crate::util;
use crate::{
cats, cond_vec,
results::{ResultColumn, ResultHeader, ResultResponse, ResultRow, ResultTable},
Expand All @@ -26,7 +28,8 @@ use crate::{
};

use super::{
add_protocol, nyaa_rss, Item, ItemType, Source, SourceConfig, SourceInfo, SourceResponse,
add_protocol, nyaa_rss, Item, ItemType, Source, SourceConfig, SourceExtraConfig, SourceInfo,
SourceResponse,
};

#[derive(Serialize, Deserialize, Clone, Copy, Default)]
Expand Down Expand Up @@ -273,7 +276,7 @@ impl Source for NyaaHtmlSource {
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> Result<SourceResponse, Box<dyn Error + Send + Sync>> {
let nyaa = config.nyaa.to_owned().unwrap_or_default();
if nyaa.rss {
Expand All @@ -282,7 +285,7 @@ impl Source for NyaaHtmlSource {
nyaa.timeout,
client,
search,
date_format,
extra,
)
.await;
}
Expand Down Expand Up @@ -370,13 +373,24 @@ impl Source for NyaaHtmlSource {
let bytes = to_bytes(&size);

const DEFAULT_DATE_FORMAT: &str = "%Y-%m-%d %H:%M";
let mut date = inner(e, date_sel, "");
let naive =
NaiveDateTime::parse_from_str(&date, DEFAULT_DATE_FORMAT).unwrap_or_default();
let date_raw = inner(e, date_sel, "");
let naive = NaiveDateTime::parse_from_str(&date_raw, DEFAULT_DATE_FORMAT)
.unwrap_or_default();
let date_time: DateTime<Local> = Local.from_utc_datetime(&naive);
date = date_time
.format(date_format.as_deref().unwrap_or(DEFAULT_DATE_FORMAT))
.to_string();
let date_format = extra.date_format.as_deref().unwrap_or(DEFAULT_DATE_FORMAT);

let date = if extra.relative_date.unwrap_or(false) {
util::conv::to_relative_date(
date_time,
extra.relative_date_short.unwrap_or(false),
)
} else {
let mut newstr = String::new();
if write!(newstr, "{}", date_time.format(date_format)).is_err() {
newstr = format!("Invalid format string: `{}`", date_format);
}
newstr
};

let seeders = as_type(inner(e, seed_sel, "0")).unwrap_or_default();
let leechers = as_type(inner(e, leech_sel, "0")).unwrap_or_default();
Expand Down Expand Up @@ -429,11 +443,11 @@ impl Source for NyaaHtmlSource {
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> Result<SourceResponse, Box<dyn Error + Send + Sync>> {
let nyaa = config.nyaa.to_owned().unwrap_or_default();
let sort = search.sort;
let mut res = NyaaHtmlSource::search(client, search, config, date_format).await;
let mut res = NyaaHtmlSource::search(client, search, config, extra).await;

if nyaa.rss {
if let Ok(SourceResponse::Results(res)) = &mut res {
Expand All @@ -446,26 +460,26 @@ impl Source for NyaaHtmlSource {
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> Result<SourceResponse, Box<dyn Error + Send + Sync>> {
NyaaHtmlSource::search(client, search, config, date_format).await
NyaaHtmlSource::search(client, search, config, extra).await
}
async fn categorize(
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> Result<SourceResponse, Box<dyn Error + Send + Sync>> {
NyaaHtmlSource::search(client, search, config, date_format).await
NyaaHtmlSource::search(client, search, config, extra).await
}
async fn solve(
_solution: String,
client: &reqwest::Client,
search: &SearchQuery,
config: &SourceConfig,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> Result<SourceResponse, Box<dyn Error + Send + Sync>> {
NyaaHtmlSource::search(client, search, config, date_format).await
NyaaHtmlSource::search(client, search, config, extra).await
}

fn info() -> SourceInfo {
Expand Down
37 changes: 21 additions & 16 deletions src/source/nyaa_rss.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Write;
use std::{cmp::Ordering, collections::BTreeMap, error::Error, str::FromStr, time::Duration};

use chrono::{DateTime, Local};
Expand All @@ -8,11 +9,13 @@ use urlencoding::encode;
use crate::{
results::ResultResponse,
sync::SearchQuery,
util::conv::to_bytes,
util::{self, conv::to_bytes},
widget::sort::{SelectedSort, SortDir},
};

use super::{add_protocol, nyaa_html::NyaaSort, Item, ItemType, Source, SourceResponse};
use super::{
add_protocol, nyaa_html::NyaaSort, Item, ItemType, Source, SourceExtraConfig, SourceResponse,
};

type ExtensionMap = BTreeMap<String, Vec<Extension>>;

Expand Down Expand Up @@ -44,7 +47,7 @@ pub async fn search_rss<S: Source>(
timeout: Option<u64>,
client: &reqwest::Client,
search: &SearchQuery,
date_format: Option<String>,
extra: &SourceExtraConfig,
) -> Result<SourceResponse, Box<dyn Error + Send + Sync>> {
let query = search.query.to_owned();
let cat = search.category;
Expand Down Expand Up @@ -94,8 +97,8 @@ pub async fn search_rss<S: Source>(
.replace('i', "")
.replace("Bytes", "B");
let pub_date = item.pub_date().unwrap_or("");
let date = DateTime::parse_from_rfc2822(pub_date).unwrap_or_default();
let date = date.with_timezone(&Local);
let date_time = DateTime::parse_from_rfc2822(pub_date).unwrap_or_default();
let date_time = date_time.with_timezone(&Local);
let torrent_link = base_url
.join(&format!("/download/{}.torrent", id))
.map(Into::into)
Expand All @@ -107,13 +110,24 @@ pub async fn search_rss<S: Source>(
(_, true) => ItemType::Remake,
_ => ItemType::None,
};
let date_format = date_format
let date_format = extra
.date_format
.to_owned()
.unwrap_or("%Y-%m-%d %H:%M".to_owned());

let date = if extra.relative_date.unwrap_or(false) {
util::conv::to_relative_date(date_time, extra.relative_date_short.unwrap_or(false))
} else {
let mut newstr = String::new();
if write!(newstr, "{}", date_time.format(&date_format)).is_err() {
newstr = format!("Invalid format string: `{}`", date_format);
}
newstr
};

Some(Item {
id: format!("nyaa-{}", id_usize),
date: date.format(&date_format).to_string(),
date,
seeders: get_ext_value(ext, "seeders"),
leechers: get_ext_value(ext, "leechers"),
downloads: get_ext_value(ext, "downloads"),
Expand All @@ -138,13 +152,4 @@ pub async fn search_rss<S: Source>(
last_page,
total_results,
}))
// Ok(items)
// Ok(nyaa_table(
// items,
// &theme,
// &search.sort,
// nyaa.columns,
// last_page,
// total_results,
// ))
}
Loading

0 comments on commit 2ce5820

Please sign in to comment.