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
use lazy_static::lazy_static;
use regex::Regex;
use reqwest;
#[derive(Debug)]
pub struct FbVideo {
url: String,
content: String,
}
#[derive(Debug)]
pub enum Quality {
Sd,
Hd,
}
#[derive(Debug)]
pub enum Error {
HttpError,
RedirectError,
ClientError,
ServerError,
TimeoutError,
UnknownError,
}
impl FbVideo {
pub fn new(url: &str) -> Self {
FbVideo {
url: String::from(url),
content: String::new(),
}
}
pub fn get_video_url(&mut self, quality: Quality) -> Result<&str, Error> {
self.crawl_page_source()?;
Ok(FbVideo::grep_video_url(&self.content, quality))
}
pub fn get_video_title(&mut self) -> Result<&str, Error> {
self.crawl_page_source()?;
Ok(FbVideo::grep_video_title(&self.content))
}
fn grep_video_url(content: &str, quality: Quality) -> &str {
const SD: &str = r#"sd_src_no_ratelimit:\s*"([^"]+)""#;
const HD: &str = r#"hd_src_no_ratelimit:\s*"([^"]+)""#;
lazy_static! {
static ref URL_SD_REGEX: Regex = Regex::new(SD).unwrap();
static ref URL_HD_REGEX: Regex = Regex::new(HD).unwrap();
};
match quality {
Quality::Sd => &*URL_SD_REGEX,
Quality::Hd => &*URL_HD_REGEX,
}
.captures(content)
.unwrap()
.get(1)
.unwrap()
.as_str()
}
fn grep_video_title(content: &str) -> &str {
const TITLE: &str = r#"title id="pageTitle">(.+?)<\/title>"#;
lazy_static! {
static ref TITLE_REGEX: Regex = Regex::new(TITLE).unwrap();
}
TITLE_REGEX
.captures(content)
.unwrap()
.get(1)
.unwrap()
.as_str()
}
fn crawl_page_source(&mut self) -> Result<(), Error> {
if self.content.is_empty() {
self.content = match FbVideo::make_request(&self.url) {
Ok(body) => body,
Err(e) => return Err(FbVideo::handler_error(e)),
};
}
Ok(())
}
fn make_request(url: &str) -> Result<String, reqwest::Error> {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::USER_AGENT,
reqwest::header::HeaderValue::from_static(
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
),
);
reqwest::Client::builder()
.gzip(true)
.default_headers(headers)
.build()?
.get(url)
.send()?
.text()
}
fn handler_error(e: reqwest::Error) -> Error {
if e.is_http() {
Error::HttpError
} else if e.is_timeout() {
Error::TimeoutError
} else if e.is_redirect() {
Error::RedirectError
} else if e.is_client_error() {
Error::ClientError
} else if e.is_server_error() {
Error::ServerError
} else {
Error::UnknownError
}
}
}