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
181
182
183
184
185
use self::req::{Json, Mode, Req};
use crate::{
cfg::{self, Config},
err::Error,
plugins::chrome,
};
use std::{
collections::HashMap,
str::FromStr,
time::Duration,
};
use reqwest::{
Client,
ClientBuilder,
Response,
header::{
HeaderMap,
HeaderName,
HeaderValue,
}
};
#[derive(Clone)]
pub struct LeetCode {
pub conf: Config,
client: Client,
default_headers: HeaderMap,
}
impl LeetCode {
fn headers(mut headers: HeaderMap, ts: Vec<(&str, &str)>) -> HeaderMap {
for (k, v) in ts.into_iter() {
headers.insert(
HeaderName::from_str(k).unwrap(),
HeaderValue::from_str(v).unwrap(),
);
}
headers
}
pub fn new() -> LeetCode {
debug!("Building reqwest client...");
let conf = cfg::locate();
let cookies = chrome::cookies();
let default_headers = LeetCode::headers(
HeaderMap::new(),
vec![
("Cookie", cookies.to_string().as_str()),
("x-csrftoken", &cookies.csrf),
("x-requested-with", "XMLHttpRequest"),
("Origin", &conf.sys.urls["base"])
],
);
let client = ClientBuilder::new()
.gzip(true)
.connect_timeout(Duration::from_secs(30))
.cookie_store(true)
.build()
.expect("Reqwest client build failed");
LeetCode {
conf,
client,
default_headers,
}
}
pub fn get_category_problems(self, category: &str) -> Result<Response, Error> {
let pre_url = &self.conf.sys.urls["problems"];
let url = &pre_url.replace("$category", category);
Req {
default_headers: self.default_headers,
refer: None,
info: false,
json: None,
mode: Mode::Get,
name: "get_category_problems",
url: url.to_string(),
}.send(&self.client)
}
pub fn get_question_detail(self, slug: &str) -> Result<Response, Error> {
let pre_refer = &self.conf.sys.urls["problems"];
let refer = pre_refer.replace("$slug", slug);
let mut json: Json = HashMap::new();
json.insert(
"query",
vec![
"query getQuestionDetail($titleSlug: String!) {",
" question(titleSlug: $titleSlug) {",
" content",
" stats",
" codeDefinition",
" sampleTestCase",
" enableRunCode",
" metaData",
" translatedContent",
" }",
"}"
].join("\n")
);
json.insert(
"variables",
r#"{"titleSlug": "$titleSlug"}"#.replace("$titleSlug", &slug)
);
json.insert("operationName", "getQuestionDetail".to_string());
Req {
default_headers: self.default_headers,
refer: Some(refer),
info: false,
json: Some(json),
mode: Mode::Post,
name: "get_problem_detail",
url: (&self.conf.sys.urls["graphql"]).to_string(),
}.send(&self.client)
}
}
mod req {
use super::LeetCode;
use crate::err::Error;
use std::collections::HashMap;
use reqwest::{
Client,
header::HeaderMap,
Response,
};
pub type Json = HashMap<&'static str, String>;
pub enum Mode {
Get,
Post
}
pub struct Req {
pub default_headers: HeaderMap,
pub refer: Option<String>,
pub json: Option<Json>,
pub info: bool,
pub mode: Mode,
pub name: &'static str,
pub url: String,
}
impl Req {
pub fn send<'req>(self, client: &'req Client) -> Result<Response, Error> {
debug!("Running leetcode::{}...", &self.name);
if self.info {
info!("Downloading {} deps...", &self.name);
}
let headers = LeetCode::headers(
self.default_headers,
vec![("Referer", &self.refer.unwrap_or(self.url.to_owned()))],
);
let req = match self.mode {
Mode::Get => client.get(&self.url),
Mode::Post => client.post(&self.url).json(&self.json),
};
Ok(req.headers(headers).send()?)
}
}
}