Add rocksdb
This commit is contained in:
94
src/cli/sync.rs
Normal file
94
src/cli/sync.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
use std::path::Path;
|
||||
use clap::{Args, Command, Parser, Subcommand};
|
||||
use color_eyre::eyre::eyre;
|
||||
use color_eyre::eyre::Result;
|
||||
use colored::Colorize;
|
||||
use crate::config::types::ApplicationConfig;
|
||||
use crate::constants::{DB_CF_OPTIONS, DB_OPTIONS};
|
||||
use crate::crawler::{dlsite, DLSiteCrawler};
|
||||
use crate::helpers::db::RocksDB;
|
||||
use crate::models::DLSiteManiax;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub(super) struct SyncCommand {
|
||||
#[command(subcommand)]
|
||||
pub(super) subcommand: SyncSubCommand,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub(super) enum SyncSubCommand {
|
||||
DLSite(SyncDLSiteCommand)
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub(super) struct SyncDLSiteCommand;
|
||||
|
||||
impl Subcommand for SyncCommand {
|
||||
fn augment_subcommands(cmd: Command) -> Command {
|
||||
cmd.subcommand(SyncDLSiteCommand::augment_args(Command::new("dlsite")))
|
||||
.subcommand_required(true)
|
||||
}
|
||||
|
||||
fn augment_subcommands_for_update(cmd: Command) -> Command {
|
||||
cmd.subcommand(SyncDLSiteCommand::augment_args(Command::new("dlsite")))
|
||||
.subcommand_required(true)
|
||||
}
|
||||
|
||||
fn has_subcommand(name: &str) -> bool {
|
||||
matches!(name, "dlsite")
|
||||
}
|
||||
}
|
||||
|
||||
impl SyncSubCommand {
|
||||
pub async fn handle(&self) -> color_eyre::Result<()> {
|
||||
match self {
|
||||
Self::DLSite(cmd) => cmd.handle().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SyncDLSiteCommand {
|
||||
pub async fn handle(&self) -> color_eyre::Result<()> {
|
||||
let app_conf = ApplicationConfig::get_config()?;
|
||||
let db = RocksDB::new(DB_OPTIONS.clone(), DB_CF_OPTIONS.clone())?;
|
||||
Self::sync_genres(&app_conf).await?;
|
||||
Self::sync_works(&app_conf, &db).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn sync_genres(app_conf: &ApplicationConfig) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn sync_works(app_conf: &ApplicationConfig, db: &RocksDB) -> Result<()> {
|
||||
let crawler = DLSiteCrawler::new();
|
||||
let mut rj_nums: Vec<String> = Vec::new();
|
||||
for path_str in app_conf.path_config.dlsite_paths.iter() {
|
||||
let path = Path::new(path_str);
|
||||
if !path.exists() {
|
||||
return Err(eyre!("{} {}", path_str.blue(), "does not exist".red()));
|
||||
}
|
||||
let dir_paths = path.read_dir()?
|
||||
.filter_map(Result::ok)
|
||||
.map(|e| e.path())
|
||||
.collect::<Vec<_>>();
|
||||
for dir_path in dir_paths.iter() {
|
||||
if !dir_path.is_dir() {
|
||||
println!("{dir_path:?} is not a directory");
|
||||
continue;
|
||||
}
|
||||
let dir_name = dir_path
|
||||
.file_name().unwrap()
|
||||
.to_str().unwrap();
|
||||
if !dlsite::is_valid_rj_number(dir_name) {
|
||||
println!("{} {}", dir_path.to_str().unwrap().blue(), "is not a valid rj number, please add it manually".red());
|
||||
continue;
|
||||
}
|
||||
rj_nums.push(dir_name.to_string());
|
||||
}
|
||||
}
|
||||
let maniaxes = crawler.get_game_infos(rj_nums).await?;
|
||||
db.set_values(&maniaxes)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user