Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit 2b373c0

Browse files
authored
* add graphql query for user info * add parser for graphql user info * return `CookieError` when leetcode rejects exec/test `Run` * add the `is_session_bad` method to somewhat-accurately determine when a LEETCODE_SESSION becomes invalid * When json parsing fails, if the underlying request requires user authentication, use `is_session_bad()` to check if LEETCODE_SESSION is valid. * get rid of ZWSPs in problem descriptions (see clearloop#56) * add Error::PremiumError * throw PremiumError when locked questions are queried for details
1 parent 59f075b commit 2b373c0

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

‎src/cache/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,16 @@ impl Cache {
177177
.json()
178178
.await?;
179179
debug!("{:#?}", &json);
180-
parser::desc(&mut rdesc, json).ok_or(Error::NoneError)?;
180+
match parser::desc(&mut rdesc, json) {
181+
None => return Err(Error::NoneError),
182+
Some(false) => return
183+
if self.is_session_bad().await {
184+
Err(Error::CookieError)
185+
} else {
186+
Err(Error::PremiumError)
187+
},
188+
Some(true) => ()
189+
}
181190

182191
// update the question
183192
let sdesc = serde_json::to_string(&rdesc)?;

‎src/cache/parser.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,22 @@ pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Option<()> {
2929
}
3030

3131
/// desc parser
32-
pub fn desc(q: &mut Question, v: Value) -> Option<()> {
32+
pub fn desc(q: &mut Question, v: Value) -> Option<bool> {
33+
/* None - parsing failed
34+
* Some(false) - content was null (premium?)
35+
* Some(true) - content was parsed
36+
*/
3337
let o = &v
3438
.as_object()?
3539
.get("data")?
3640
.as_object()?
3741
.get("question")?
3842
.as_object()?;
3943

44+
if *o.get("content")? == Value::Null {
45+
return Some(false);
46+
}
47+
4048
*q = Question {
4149
content: o.get("content")?.as_str().unwrap_or("").to_string(),
4250
stats: serde_json::from_str(o.get("stats")?.as_str()?).ok()?,
@@ -55,7 +63,7 @@ pub fn desc(q: &mut Question, v: Value) -> Option<()> {
5563
.to_string(),
5664
};
5765

58-
Some(())
66+
Some(true)
5967
}
6068

6169
/// tag parser

‎src/err.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub enum Error {
1414
FeatureError(String),
1515
ScriptError(String),
1616
CookieError,
17+
PremiumError,
1718
DecryptError,
1819
SilentError,
1920
NoneError,
@@ -37,6 +38,13 @@ impl std::fmt::Debug for Error {
3738
.yellow()
3839
.bold(),
3940
),
41+
Error::PremiumError => write!(
42+
f,
43+
"{} \
44+
Your leetcode account lacks a premium subscription, which the given problem requires.\n \
45+
If this looks like a mistake, please open a new issue at: {}",
46+
e,
47+
"https://github.com/clearloop/leetcode-cli/".underline()),
4048
Error::DownloadError(s) => write!(f, "{} Download {} failed, please try again", e, s),
4149
Error::NetworkError(s) => write!(f, "{} {}, please try again", e, s),
4250
Error::ParseError(s) => write!(f, "{} {}", e, s),

‎src/helper.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ mod html {
147147
impl HTML for String {
148148
fn ser(&self) -> Vec<Token> {
149149
// empty tags
150-
let tks = self.to_string();
150+
let tks = {
151+
let mut s = self.clone();
152+
// some problems (e.g. 1653) have ZWSPs.
153+
s.retain(|x| x != '\u{200B}');
154+
s };
151155
let res: Vec<Token>;
152156
// styled
153157
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /