-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
123 lines (113 loc) · 4.02 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::path::PathBuf;
use clap::{Parser, Subcommand};
use gce_scraper::{config_gen::{handle_generate, GenerationConfig, PaperGenerationConfig}, configuration::{PaperType, Season}, download::{handle_download, DownloadConfiguration}};
use log::debug;
#[derive(Parser, Debug)]
#[command(version, about="A GCE-Guide Scraper. Download all the A-Levels Past Papers in bulk!", author = "Sushant Pangeni, [email protected]")]
struct Args {
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,
#[arg(
short,
long,
value_name = "threads",
default_value = "4",
long_help = "Number of threads to use for I/O operations."
)]
threads: u8,
#[command(subcommand)]
generate: Subs,
}
#[derive(Subcommand, Debug)]
enum Subs {
#[command(about = "Generate a configuration file consisting of all download values.")]
GenerateConfig {
#[arg(short, long, value_name = "output", default_value = "config.toml")]
output: PathBuf,
#[arg(short = 'p', long, value_name = "paper", value_delimiter=',', default_value="qp,ms,er")]
papers: Vec<PaperType>,
#[arg(short, long, value_name = "years", value_delimiter=',')]
years: Option<Vec<String>>,
#[arg(short, long, value_name = "subjects", value_delimiter=',')]
subjects: Option<Vec<String>>,
#[arg(long, value_name = "seasons", value_delimiter=',' , default_value = "winter,summer")]
seasons: Option<Vec<Season>>
},
#[command(about = "Download the files specified in the configuration file.")]
Download {
#[arg(short, long, value_name = "config", default_value = "config.toml")]
config: PathBuf,
#[arg(
short,
long,
value_name = "output-folder",
default_value = "Past Papers",
long_help = "Name of the directory to store in/create."
)]
output: PathBuf
},
}
fn main() {
let args = Args::parse();
// Check verbosity flag and set RUST_LOG env variable
match args.verbose.is_present() {
true => {
let level = args.verbose.log_level().unwrap_or(log::Level::Info);
std::env::set_var("RUST_LOG", level.to_string());
}
false => {
std::env::set_var("RUST_LOG", "info");
}
}
// Initialize logger
pretty_env_logger::init();
debug!(
"Logger started with level: {}",
std::env::var("RUST_LOG").unwrap()
);
// Handle subcommands
match args.generate {
Subs::Download {
config,
output,
} => {
debug!("Selected Download subcommand.");
handle_download(match DownloadConfiguration::new(config, output, args.threads) {
Ok(config) => config,
Err(e) => {
match e {
gce_scraper::download::DownloadError::ConfigNotFound => {
log::error!("Configuration file not found.");
},
gce_scraper::download::DownloadError::DownloadFolderCannotBeCreated => {
log::error!("Output folder cannot be created.");
},
gce_scraper::download::DownloadError::ConfigParseError(e) => {
log::error!("Error parsing configuration file: {}", e);
}
}
std::process::exit(1);
}
});
}
Subs::GenerateConfig {
output,
papers,
years,
subjects,
seasons,
} => {
debug!("Selected GenerateConfig subcommand.");
handle_generate(GenerationConfig::new(
output,
PaperGenerationConfig {
papers,
years,
subjects,
seasons,
},
args.threads,
));
}
}
}