aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 88e301c94c08bc9b09fd0570a28fefa9b96c660b (plain) (blame)
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
#[macro_use]
extern crate clap;
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate reqwest;

use clap::{App, ArgMatches};
use indicatif::{ProgressBar, ProgressStyle};
use regex::Regex;
use reqwest::{Client, Error};

use std::env;
use std::fs::create_dir_all;
use std::fs::File;
use std::io::copy;

fn main() {
    let yaml = load_yaml!("cli.yml");
    let matches = App::from_yaml(yaml).get_matches();

    let thread = matches.value_of("thread").unwrap();
    let client = Client::new();
    download_thread(thread, &matches, &client);
}

fn load(url: &str, client: &Client) -> Result<String, Error> {
    let mut response = client.get(url).send()?;
    Ok(response.text().unwrap())
}

fn save_image(url: &str, name: &str, client: &Client) -> Result<String, Error> {
    let mut response = client.get(url).send()?;

    if response.status().is_success() {
        let mut dest = File::create(name).unwrap();
        copy(&mut response, &mut dest).unwrap();
    }
    Ok(String::from(name))
}

fn download_thread(thread_link: &str, matches: &ArgMatches, client: &Client) {
    let workpath = env::current_dir().unwrap();

    lazy_static! {
        static ref RE: Regex =
            Regex::new(r"(//i(?:s)?\d*\.(?:4cdn|4chan)\.org/\w+/(\d+\.(?:jpg|png|gif|webm)))")
                .unwrap();
    }

    let url_vec: Vec<&str> = thread_link.split('/').collect();
    let board = url_vec[3];
    let thread_vec: Vec<&str> = url_vec[5].split('#').collect();
    let mut thread = thread_vec[0];

    if url_vec.len() > 6 {
        let thread_tmp_vec: Vec<&str> = url_vec[6].split('#').collect();
        let thread_tmp = thread_tmp_vec[0];

        let path = workpath.join("downloads").join(board).join(thread_tmp);

        if matches.is_present("names") || path.exists() {
            thread = thread_tmp;
        }
    }

    let directory = workpath.join("downloads").join(board).join(thread);
    if !directory.exists() {
        match create_dir_all(&directory) {
            Ok(_) => {}
            Err(err) => eprintln!("Failed to create new directory: {}", err),
        }
    }

    match load(thread_link, client) {
        Ok(page_string) => {
            let links_iter = RE.captures_iter(page_string.as_str());
            let number_of_links = RE.captures_iter(page_string.as_str()).count() / 2;
            let pb = ProgressBar::new(number_of_links as u64);
            pb.set_style(ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg} ({eta})")
                .progress_chars("#>-"));

            pb.tick();
            for cap in links_iter.step_by(2) {
                let img_path = directory.join(&cap[2]);
                if !img_path.exists() {
                    match save_image(
                        format!("{}{}", "https:", &cap[1]).as_str(),
                        img_path.to_str().unwrap(),
                        client,
                    ) {
                        Ok(_) => {}
                        Err(err) => eprintln!("Error: {}", err),
                    }
                }
                pb.set_message(&cap[2].to_string());
                pb.inc(1);
            }
            pb.finish_with_message("Done");
        }
        Err(err) => eprintln!("Error: {}", err),
    }
}