Add dlsite cli add folder

This commit is contained in:
2025-10-10 23:57:29 +08:00
parent 473ef8452b
commit 4a0fa965c6
11 changed files with 443 additions and 50 deletions

View File

@@ -1,13 +1,12 @@
use std::path::PathBuf;
use crate::event::{Event, EventHandler};
use ratatui::widgets::{Block, Borders, Paragraph};
use ratatui::{DefaultTerminal};
use std::time::Duration;
use color_eyre::Result;
use crossterm::event;
use crossterm::event::KeyCode;
use crossterm::event::KeyCode::Char;
use diesel::{Connection, SqliteConnection};
use lazy_static::lazy_static;
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::prelude::{Widget};
@@ -16,11 +15,24 @@ use ratatui::text::{Line, Span, Text};
use crate::config::types::ApplicationConfig;
use crate::constants::{APP_CONFIG_DIR, APP_CONIFG_FILE_PATH, APP_DATA_DIR};
enum AppState {
Running,
Exiting,
Input
}
enum AppPopUp {
AddFolder,
None
}
pub(crate) struct App {
running: bool,
state: AppState,
popup: AppPopUp,
events: EventHandler,
db_connection: SqliteConnection,
app_config: ApplicationConfig,
current_event: Option<Event>
}
impl App {
@@ -31,10 +43,12 @@ impl App {
Self::initialize();
let db_conn = Self::establish_db_connection(app_conf.clone());
Self {
running: true,
state: AppState::Running,
popup: AppPopUp::None,
events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)),
db_connection: db_conn,
app_config: app_conf
app_config: app_conf,
current_event: None
}
}
@@ -58,16 +72,29 @@ impl App {
terminal.draw(|frame| frame.render_widget(&mut self, frame.area()))?;
let event = self.events.next().await?;
self.update(event)?;
if !self.running {
if matches!(self.state, AppState::Exiting) {
break Ok(())
}
}
}
fn update(&mut self, event: Event) -> Result<()> {
if let Event::Key(key) = event && event::KeyEventKind::is_press(&key.kind) {
self.current_event = Some(event.clone());
if let Event::Key(key) = event && matches!(self.state, AppState::Input) {
match key.code {
KeyCode::Esc => {
self.state = AppState::Running;
},
_ => {}
}
}
if let Event::Key(key) = event &&
event::KeyEventKind::is_press(&key.kind) &&
!matches!(self.state, AppState::Input) {
match key.code {
Char('q') => self.quit()?,
Char('a') => self.folder_popup(),
_ => {}
}
}
@@ -84,7 +111,7 @@ impl Widget for &mut App {
.constraints([
Constraint::Length(1),
Constraint::Min(1),
Constraint::Length(3),
Constraint::Length(1),
])
.split(area);
@@ -92,6 +119,17 @@ impl Widget for &mut App {
self.render_game_list(chunks[1], buf);
self.render_footer(chunks[2], buf);
let popup_area = Rect {
x: area.width / 4,
y: area.height / 3,
width: area.width / 2,
height: area.height / 3,
};
match self.popup {
AppPopUp::AddFolder => self.render_folder_popup(popup_area, buf),
AppPopUp::None => {},
};
}
}
@@ -117,23 +155,40 @@ impl App {
}
fn render_footer(&mut self, area: Rect, buf: &mut Buffer) {
let navigation_text = vec![
let mut navigation_text = vec![
Span::styled("(q) quit / (a) add folders", Style::default().fg(Color::Green)),
];
if matches!(self.state, AppState::Input) {
navigation_text[0] = Span::styled("Input Mode", Style::default().fg(Color::Green));
}
let line = Line::from(navigation_text);
let footer = Paragraph::new(line);
footer.render(area, buf);
}
fn render_folder_popup(&mut self, area: Rect, buf: &mut Buffer) {
todo!()
}
}
// event handlers
impl App {
fn quit(&mut self) -> Result<()> {
self.running = false;
self.app_config
.clone()
.write_to_file(APP_CONIFG_FILE_PATH.to_path_buf())?;
if matches!(self.popup, AppPopUp::None) {
self.state = AppState::Exiting;
self.app_config
.clone()
.write_to_file(&APP_CONIFG_FILE_PATH.to_path_buf())?;
}
else {
self.popup = AppPopUp::None;
}
Ok(())
}
fn folder_popup(&mut self) {
self.popup = AppPopUp::AddFolder;
self.state = AppState::Input;
}
}

114
src/cli.rs Normal file
View File

@@ -0,0 +1,114 @@
use std::path::PathBuf;
use clap::{command, Args, Command, Parser, Subcommand};
use color_eyre::Result;
use crate::app;
use crate::config::types::ApplicationConfig;
use crate::constants::APP_CONIFG_FILE_PATH;
#[derive(Parser, Debug)]
struct FolderAddCommand {
path: String
}
#[derive(Parser, Debug)]
enum FolderSubCommand {
Add(FolderAddCommand)
}
#[derive(Parser, Debug)]
struct FolderCommand {
#[command(subcommand)]
subcommand: FolderSubCommand
}
#[derive(Parser, Debug)]
enum CliSubCommand {
Folder(FolderCommand),
}
#[derive(Parser, Debug)]
#[command(version, about)]
pub(crate) struct Cli {
#[command(subcommand)]
subcommand: Option<CliSubCommand>
}
impl Subcommand for Cli {
fn augment_subcommands(cmd: Command) -> Command {
cmd.subcommand(FolderCommand::augment_args(Command::new("folder")))
.subcommand_required(true)
}
fn augment_subcommands_for_update(cmd: Command) -> Command {
cmd.subcommand(FolderCommand::augment_args(Command::new("folder")))
.subcommand_required(true)
}
fn has_subcommand(name: &str) -> bool {
matches!(name, "folder")
}
}
impl Subcommand for FolderCommand {
fn augment_subcommands(cmd: Command) -> Command {
cmd.subcommand(FolderAddCommand::augment_args(Command::new("add")))
.subcommand_required(true)
}
fn augment_subcommands_for_update(cmd: Command) -> Command {
cmd.subcommand(FolderAddCommand::augment_args(Command::new("add")))
.subcommand_required(true)
}
fn has_subcommand(name: &str) -> bool {
matches!(name, "add")
}
}
impl Cli {
pub async fn run(&self) -> Result<()> {
if self.subcommand.is_none() {
return self.start_tui().await
}
if let Some(sub_command) = &self.subcommand {
return sub_command.handle().await
}
Ok(())
}
async fn start_tui(&self) -> Result<()> {
let terminal = ratatui::init();
let app = app::App::new();
let result = app.run(terminal).await;
ratatui::restore();
result
}
}
impl CliSubCommand {
pub async fn handle(&self) -> Result<()> {
match self {
CliSubCommand::Folder(cmd) => {
cmd.subcommand.handle().await
}
}
}
}
impl FolderSubCommand {
pub async fn handle(&self) -> Result<()> {
match self {
FolderSubCommand::Add(cmd) => cmd.handle().await,
}
}
}
impl FolderAddCommand {
pub async fn handle(&self) -> Result<()> {
let mut config = ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH.to_path_buf())?;
let path = PathBuf::from(&self.path);
let abs_path = path.canonicalize()?;
config.path_config.dlsite_paths.push(abs_path.to_str().unwrap().to_string());
config.write_to_file(&APP_CONIFG_FILE_PATH.to_path_buf())
}
}

View File

@@ -1,7 +1,7 @@
use std::path::{PathBuf};
use ini::Ini;
use color_eyre::Result;
use crate::config::types::{ApplicationConfig, BasicConfig};
use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig};
use crate::constants::{APP_CONIFG_FILE_PATH, APP_DATA_DIR};
pub mod types;
@@ -14,8 +14,14 @@ impl ApplicationConfig {
db_path: basic_conf_section.get("DBPath").unwrap().to_string(),
tick_rate: basic_conf_section.get("TickRate").unwrap().parse()?,
};
let path_conf_section = conf.section(Some("Path")).unwrap();
let path_conf = PathConfig {
dlsite_paths: path_conf_section.get("DLSite").unwrap()
.split(":").map(|s| s.to_string()).collect(),
};
Ok(Self {
basic_config: basic_conf
basic_config: basic_conf,
path_config: path_conf
})
}
@@ -24,17 +30,23 @@ impl ApplicationConfig {
basic_config: BasicConfig {
db_path: APP_DATA_DIR.clone().join("games.db").to_str().unwrap().to_string(),
tick_rate: 250
},
path_config: PathConfig {
dlsite_paths: vec![]
}
};
conf.clone().write_to_file(APP_CONIFG_FILE_PATH.to_path_buf()).unwrap();
conf.clone().write_to_file(&APP_CONIFG_FILE_PATH.to_path_buf()).unwrap();
conf
}
pub fn write_to_file(self, path: PathBuf) -> Result<()> {
pub fn write_to_file(self, path: &PathBuf) -> Result<()> {
let mut conf = Ini::new();
conf.with_section(Some("Basic"))
.set("DBPath", self.basic_config.db_path)
.set("TickRate", self.basic_config.tick_rate.to_string());
conf.with_section(Some("Path"))
.set("DLSite", self.path_config.dlsite_paths.into_iter()
.filter(|x| !x.is_empty()).collect::<Vec<String>>().join(":"));
conf.write_to_file(path)?;
Ok(())
}

View File

@@ -1,10 +1,16 @@
#[derive(Clone)]
pub(crate) struct ApplicationConfig {
pub(crate) basic_config: BasicConfig,
pub(crate) path_config: PathConfig
}
#[derive(Clone)]
pub(crate) struct BasicConfig {
pub(crate) db_path: String,
pub(crate) tick_rate: u64
}
#[derive(Clone)]
pub(crate) struct PathConfig {
pub(crate) dlsite_paths: Vec<String>
}

10
src/crawler/dlsite.rs Normal file
View File

@@ -0,0 +1,10 @@
use crate::crawler::Crawler;
#[derive(Clone)]
pub(crate) struct DLSiteCrawler {
crawler: Crawler,
}
impl DLSiteCrawler {
}

View File

@@ -1,6 +1,9 @@
mod dlsite;
use reqwest::{Client, Url};
use robotstxt::{DefaultMatcher};
use color_eyre::Result;
use scraper::Html;
use crate::constants::{APP_CACHE_PATH};
#[derive(Clone)]
@@ -20,8 +23,10 @@ impl Crawler {
base_url,
};
let mut matcher = DefaultMatcher::default();
let access_allowed = matcher.one_agent_allowed_by_robots(&crawler.robots_txt, "reqwest", crawler.base_url.as_str());
assert_eq!(true, access_allowed);
let is_access_allowed = matcher.one_agent_allowed_by_robots(&crawler.robots_txt, "reqwest", crawler.base_url.as_str());
if !is_access_allowed {
panic!("Crawler cannot access site {}", crawler.base_url.as_str());
}
crawler
}
@@ -43,4 +48,11 @@ impl Crawler {
Ok(tokio::fs::read_to_string(&local_robots_path).await?)
}
}
pub async fn get_html(&self, path: &str) -> Result<Html> {
let mut url = self.base_url.clone();
url.set_path(path);
let html_text = &self.client.get(url).send().await?.text().await?;
Ok(Html::parse_document(html_text))
}
}

View File

@@ -7,6 +7,7 @@ use std::time::Duration;
use tokio::sync::mpsc::UnboundedSender;
use tokio::task::JoinHandle;
#[derive(Clone)]
pub(crate) enum Event {
Error,
Tick,

View File

@@ -6,22 +6,16 @@ mod config;
mod helpers;
mod crawler;
mod constants;
mod cli;
use clap::{command, Command, Parser};
use color_eyre::Result;
use reqwest::Url;
use tokio;
use crate::crawler::Crawler;
use crate::cli::Cli;
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
let crawler = Crawler::new("dlsite", Url::parse("https://www.dlsite.com/")?).await;
let terminal = ratatui::init();
let app = app::App::new();
let result = app.run(terminal).await;
ratatui::restore();
result
let cli = Cli::parse();
cli.run().await
}