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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
extern crate reqwest;
extern crate select;

pub use user_agent_string::{
    browser::Browsers,
};

mod user_agent_string;

// --- custom ---
use user_agent_string::UserAgentString;

pub struct UserAgents(Vec<String>);

impl UserAgents {
    pub fn new() -> UserAgents {
        // --- std ---
        use std::{
            env::current_dir,
            fs::File,
            io::Read,
            path::Path,
        };

        let path = format!("{}/user_agents", current_dir().unwrap().to_str().unwrap());
        let path = Path::new(&path);
        if path.is_file() {
            let mut file = File::open(path).unwrap();
            let mut s = String::new();
            file.read_to_string(&mut s).unwrap();

            UserAgents(s.lines().map(|user_agent| user_agent.to_owned()).collect())
        } else { UserAgentsBuilder::new().all_browsers().build() }
    }

    pub fn from_cache(path: &str) -> UserAgents {
        // --- std ---
        use std::{
            fs::File,
            io::Read,
        };

        let mut file = File::open(path).unwrap();
        let mut s = String::new();
        file.read_to_string(&mut s).unwrap();

        UserAgents(s.lines().map(|user_agent| user_agent.to_owned()).collect())
    }

    pub fn random(&self) -> &str {
        // --- external ---
        use rand::{
            thread_rng,
            Rng,
        };

        &self.0[thread_rng().gen_range(0, self.0.len())]
    }
}

pub struct UserAgentsBuilder<'a> {
    cache: bool,
    thread: u32,
    dir: String,
    user_agents: Vec<String>,
    kinds: [Option<Box<dyn UserAgentString + 'a>>; 13],
}

macro_rules! set_unset {
    ($self_:ident, $setter:ident, $unsetter:ident, $i:expr) => {
        pub fn $setter(mut $self_, browsers: Browsers<'a>) -> Self {
            $self_.kinds[$i] = Some(Box::new(browsers));
            $self_
        }

        pub fn $unsetter(mut $self_) -> Self {
            $self_.kinds[$i] = None;
            $self_
        }
    };
}

impl<'a> UserAgentsBuilder<'a> {
    pub fn new() -> UserAgentsBuilder<'a> {
        UserAgentsBuilder {
            cache: true,
            thread: 20,
            dir: std::env::current_dir()
                .unwrap()
                .to_str()
                .unwrap()
                .to_owned(),
            user_agents: vec![],
            kinds: [None, None, None, None, None, None, None, None, None, None, None, None, None],
        }
    }

    pub fn all_browsers(mut self) -> Self {
        self.kinds[1] = Some(Box::new(Browsers::new().set_all()));
        self
    }
    set_unset!(self, set_browsers, unset_browsers, 1);


    fn get(url: &str) -> String {
        let client = reqwest::ClientBuilder::new()
            .danger_accept_invalid_certs(true)
            .danger_accept_invalid_hostnames(true)
            .gzip(true)
            .build()
            .unwrap();

        loop {
            match client.get(url).send() {
                Ok(mut resp) => match resp.text() {
                    Ok(html) => return html,
                    Err(e) => println!("{:?}", e)
                }
                Err(e) => println!("{:?}", e)
            }
        }
    }

    fn parse(html: String) -> Vec<String> {
        // --- external ---
        use select::{
            document::Document,
            predicate::{Attr, Name, Predicate},
        };

        let mut user_agents = vec![];
        let document = Document::from(html.as_str());
        for node in document.find(Attr("id", "liste").descendant(Name("ul"))) {
            for user_agent in node.find(Name("li").descendant(Name("a"))) {
                user_agents.push(user_agent.text());
            }
        }

        user_agents
    }

    pub fn cache(mut self, cache: bool) -> Self {
        self.cache = cache;
        self
    }

    pub fn thread(mut self, num: u32) -> Self {
        if num >= 382 { self.thread = 382; } else if num > 0 { self.thread = num; }
        self
    }

    pub fn dir(mut self, path: &str) -> Self {
        self.dir = path.trim_end_matches('/').to_owned();
        self
    }

    fn store(&self) {
        // --- std ---
        use std::{
            fs::File,
            io::Write,
        };

        let mut file = File::create(format!("{}/user_agents", self.dir)).unwrap();
        file.write_all(self.user_agents.join("\n").as_bytes()).unwrap();
        file.sync_all().unwrap();
    }

    pub fn build(mut self) -> UserAgents {
        for user_agent_string in self.kinds.iter() {
            if let Some(user_agent_string) = user_agent_string {
                user_agent_string.fetch(self.thread, &mut self.user_agents);
            }
        }

        if self.cache { self.store(); }

        UserAgents(self.user_agents)
    }
}