Skip to content

Commit

Permalink
patches for #60 and #56 (#68)
Browse files Browse the repository at this point in the history
* 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 #56)

* add Error::PremiumError

* throw PremiumError when locked questions are queried for details
  • Loading branch information
152334H authored May 20, 2022
1 parent 59f075b commit 2b373c0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,16 @@ impl Cache {
.json()
.await?;
debug!("{:#?}", &json);
parser::desc(&mut rdesc, json).ok_or(Error::NoneError)?;
match parser::desc(&mut rdesc, json) {
None => return Err(Error::NoneError),
Some(false) => return
if self.is_session_bad().await {
Err(Error::CookieError)
} else {
Err(Error::PremiumError)
},
Some(true) => ()
}

// update the question
let sdesc = serde_json::to_string(&rdesc)?;
Expand Down
12 changes: 10 additions & 2 deletions src/cache/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@ pub fn problem(problems: &mut Vec<Problem>, v: Value) -> Option<()> {
}

/// desc parser
pub fn desc(q: &mut Question, v: Value) -> Option<()> {
pub fn desc(q: &mut Question, v: Value) -> Option<bool> {
/* None - parsing failed
* Some(false) - content was null (premium?)
* Some(true) - content was parsed
*/
let o = &v
.as_object()?
.get("data")?
.as_object()?
.get("question")?
.as_object()?;

if *o.get("content")? == Value::Null {
return Some(false);
}

*q = Question {
content: o.get("content")?.as_str().unwrap_or("").to_string(),
stats: serde_json::from_str(o.get("stats")?.as_str()?).ok()?,
Expand All @@ -55,7 +63,7 @@ pub fn desc(q: &mut Question, v: Value) -> Option<()> {
.to_string(),
};

Some(())
Some(true)
}

/// tag parser
Expand Down
8 changes: 8 additions & 0 deletions src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum Error {
FeatureError(String),
ScriptError(String),
CookieError,
PremiumError,
DecryptError,
SilentError,
NoneError,
Expand All @@ -37,6 +38,13 @@ impl std::fmt::Debug for Error {
.yellow()
.bold(),
),
Error::PremiumError => write!(
f,
"{} \
Your leetcode account lacks a premium subscription, which the given problem requires.\n \
If this looks like a mistake, please open a new issue at: {}",
e,
"https://github.com/clearloop/leetcode-cli/".underline()),
Error::DownloadError(s) => write!(f, "{} Download {} failed, please try again", e, s),
Error::NetworkError(s) => write!(f, "{} {}, please try again", e, s),
Error::ParseError(s) => write!(f, "{} {}", e, s),
Expand Down
6 changes: 5 additions & 1 deletion src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ mod html {
impl HTML for String {
fn ser(&self) -> Vec<Token> {
// empty tags
let tks = self.to_string();
let tks = {
let mut s = self.clone();
// some problems (e.g. 1653) have ZWSPs.
s.retain(|x| x != '\u{200B}');
s };
let res: Vec<Token>;
// styled
{
Expand Down

0 comments on commit 2b373c0

Please sign in to comment.