Skip to content

refactor(config): make config more simple #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(plugin): disable chrome plugin
  • Loading branch information
clearloop committed Jun 24, 2023
commit fae42dea0f6c074fec0361a1e036be06c32d8c89
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde_json = "1.0.82"
toml = "0.5.9"
regex = "1.6.0"
scraper = "0.13.0"
anyhow = "1.0.71"

[dependencies.diesel]
version = "2.0.3"
Expand Down
12 changes: 9 additions & 3 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use self::schemas::{problems::dsl::*, tags::dsl::*};
use self::sql::*;
use crate::helper::test_cases_path;
use crate::{config::Config, err::Error, plugins::LeetCode};
use anyhow::anyhow;
use colored::Colorize;
use diesel::prelude::*;
use reqwest::Response;
Expand Down Expand Up @@ -342,15 +343,20 @@ impl Cache {
) -> Result<VerifyResult, Error> {
trace!("Exec problem filter —— Test or Submit");
let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, test_case).await?;
trace!("Pre run code result {:?}, {:?}, {:?}", json, url, refer);
trace!("Pre run code result {:#?}, {}, {}", json, url, refer);

let run_res: RunCode = self
let text = self
.0
.clone()
.run_code(json.clone(), url.clone(), refer.clone())
.await?
.json() // does not require LEETCODE_SESSION (very oddly)
.text()
.await?;

let run_res: RunCode = serde_json::from_str(&text).map_err(|e| {
anyhow!("json error: {e}, plz double check your session and csrf config.")
})?;

trace!("Run code result {:#?}", run_res);

// Check if leetcode accepted the Run request
Expand Down
6 changes: 6 additions & 0 deletions src/config/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ pub struct Cookies {
pub csrf: String,
pub session: String,
}

impl std::string::ToString for Cookies {
fn to_string(&self) -> String {
format!("LEETCODE_SESSION={};csrftoken={};", self.session, self.csrf)
}
}
11 changes: 9 additions & 2 deletions src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::fmt;

// fixme: use this_error
/// Error enum
#[derive(Clone)]
pub enum Error {
MatchError,
DownloadError(String),
Expand All @@ -20,6 +19,7 @@ pub enum Error {
SilentError,
NoneError,
ChromeNotLogin,
Anyhow(anyhow::Error),
}

impl std::fmt::Debug for Error {
Expand Down Expand Up @@ -59,7 +59,8 @@ impl std::fmt::Debug for Error {
"json from response parse failed, please open a new issue at: {}.",
"https://github.com/clearloop/leetcode-cli/".underline(),
),
Error::ChromeNotLogin => write!(f, "maybe you not login on the Chrome, you can login and retry.")
Error::ChromeNotLogin => write!(f, "maybe you not login on the Chrome, you can login and retry."),
Error::Anyhow(e) => write!(f, "{} {}", e, e),
}
}
}
Expand Down Expand Up @@ -147,6 +148,12 @@ impl std::convert::From<openssl::error::ErrorStack> for Error {
}
}

impl From<anyhow::Error> for Error {
fn from(err: anyhow::Error) -> Self {
Error::Anyhow(err)
}
}

// pyo3
#[cfg(feature = "pym")]
impl std::convert::From<pyo3::PyErr> for Error {
Expand Down
9 changes: 2 additions & 7 deletions src/plugins/leetcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use self::req::{Json, Mode, Req};
use crate::{
config::{self, Config},
err::Error,
plugins::chrome,
// plugins::chrome,
};
use reqwest::{
header::{HeaderMap, HeaderName, HeaderValue},
Expand Down Expand Up @@ -37,7 +37,7 @@ impl LeetCode {
/// New LeetCode client
pub fn new() -> Result<LeetCode, crate::Error> {
let conf = config::Config::locate()?;
let cookies = chrome::cookies()?;
let cookies = conf.cookies.clone();
let default_headers = LeetCode::headers(
HeaderMap::new(),
vec![
Expand All @@ -53,11 +53,6 @@ impl LeetCode {
.connect_timeout(Duration::from_secs(30))
.build()?;

// Sync conf
if conf.cookies.csrf != cookies.csrf {
conf.sync()?;
}

Ok(LeetCode {
conf,
client,
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//! ## login to `leetcode.com`
//! Leetcode-cli use chrome cookie directly, do not need to login, please make sure you have loggined in `leetcode.com` before usnig `leetcode-cli`
//!
mod chrome;

// FIXME: Read cookies from local storage. (issue #122)
// mod chrome;
mod leetcode;
pub use leetcode::LeetCode;
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