Skip to content

Commit b00b252

Browse files
authored
Pick daily leetcode problem (#66)
* add graphql query for getting the Daily Problem * add wrapper code in `cache` module for getting the Daily Problem id * add --daily cli option for `pick` subcommand
1 parent 60a9e88 commit b00b252

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

src/cache/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ impl Cache {
9393
Ok(p)
9494
}
9595

96+
/// Get daily problem
97+
pub async fn get_daily_problem_id(&self) -> Result<i32, Error> {
98+
parser::daily(
99+
self.clone()
100+
.0
101+
.get_question_daily()
102+
.await?
103+
.json()
104+
.await?
105+
).ok_or(Error::NoneError)
106+
}
107+
96108
/// Get problems from cache
97109
pub fn get_problems(&self) -> Result<Vec<Problem>, Error> {
98110
Ok(problems.load::<Problem>(&self.conn()?)?)

src/cache/parser.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ pub fn tags(v: Value) -> Option<Vec<String>> {
7777
Some(res)
7878
}
7979

80+
/// daily parser
81+
pub fn daily(v: Value) -> Option<i32> {
82+
trace!("Parse daily...");
83+
v.as_object()?
84+
.get("data")?.as_object()?
85+
.get("activeDailyCodingChallengeQuestion")?.as_object()?
86+
.get("question")?.as_object()?
87+
.get("questionFrontendId")?.as_str()?
88+
.parse().ok()
89+
}
90+
8091
pub use ss::ssr;
8192
/// string or squence
8293
mod ss {

src/cmds/pick.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ impl Command for PickCommand {
6969
.takes_value(true)
7070
.help("Filter questions by tag"),
7171
)
72+
.arg(
73+
Arg::with_name("daily")
74+
.short("d")
75+
.long("daily")
76+
.takes_value(false)
77+
.help("Pick today's daily challenge"),
78+
)
7279
}
7380

7481
/// `pick` handler
@@ -109,9 +116,14 @@ impl Command for PickCommand {
109116
crate::helper::filter(&mut problems, query.to_string());
110117
}
111118

119+
let daily_id = if m.is_present("daily") {
120+
Some(cache.get_daily_problem_id().await?)
121+
} else { None };
122+
112123
let fid = m
113124
.value_of("id")
114125
.and_then(|id| id.parse::<i32>().ok())
126+
.or(daily_id)
115127
.unwrap_or_else(|| {
116128
// Pick random without specify id
117129
let problem = &problems[rand::thread_rng().gen_range(0, problems.len())];

src/plugins/leetcode.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,39 @@ impl LeetCode {
121121
.await
122122
}
123123

124+
/// Get daily problem
125+
pub async fn get_question_daily(self) -> Result<Response, Error> {
126+
trace!("Requesting daily problem...");
127+
let url = &self.conf.sys.urls.get("graphql").ok_or(Error::NoneError)?;
128+
let mut json: Json = HashMap::new();
129+
json.insert("operationName", "daily".to_string());
130+
json.insert(
131+
"query",
132+
vec![
133+
"query daily {",
134+
" activeDailyCodingChallengeQuestion {",
135+
" question {",
136+
" questionFrontendId",
137+
" }",
138+
" }",
139+
"}",
140+
]
141+
.join("\n"),
142+
);
143+
144+
Req {
145+
default_headers: self.default_headers,
146+
refer: None,
147+
info: false,
148+
json: Some(json),
149+
mode: Mode::Post,
150+
name: "get_question_daily",
151+
url: (*url).to_string(),
152+
}
153+
.send(&self.client)
154+
.await
155+
}
156+
124157
/// Get specific problem detail
125158
pub async fn get_question_detail(self, slug: &str) -> Result<Response, Error> {
126159
trace!("Requesting {} detail...", &slug);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy