Skip to main content

pkuinfo_spider/
lib.rs

1mod articles;
2pub mod cli;
3mod client;
4mod login;
5mod scrape;
6mod search;
7mod session;
8
9use anyhow::Result;
10use clap::Parser;
11use cli::{Cli, Commands};
12
13fn init_tracing() {
14    use tracing_subscriber::{fmt, EnvFilter};
15    let filter = EnvFilter::try_from_default_env()
16        .unwrap_or_else(|_| EnvFilter::new("info_spider=info,warn"));
17    fmt()
18        .with_env_filter(filter)
19        .with_target(false)
20        .without_time()
21        .init();
22}
23
24pub async fn run() -> Result<()> {
25    init_tracing();
26    let cli = Cli::parse();
27    dispatch(cli.command).await
28}
29
30pub async fn run_from<I, T>(args: I) -> Result<()>
31where
32    I: IntoIterator<Item = T>,
33    T: Into<std::ffi::OsString> + Clone,
34{
35    let cli = Cli::try_parse_from(args)?;
36    dispatch(cli.command).await
37}
38
39pub async fn dispatch(command: Commands) -> Result<()> {
40    match command {
41        Commands::Login => login::run().await?,
42        Commands::Logout => login::logout()?,
43        Commands::Status => login::status()?,
44        Commands::Search {
45            query,
46            count,
47            format,
48        } => search::run(query, count, format).await?,
49        Commands::Articles {
50            name,
51            fakeid,
52            begin,
53            count,
54            limit,
55            delay_ms,
56            format,
57        } => {
58            articles::run(name, fakeid, begin, count, limit, delay_ms, format).await?;
59        }
60        Commands::Scrape { url, output } => scrape::run(url, output).await?,
61    }
62    Ok(())
63}
64
65pub const VERSION: &str = env!("CARGO_PKG_VERSION");