Skip to content
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

no fetch operation when solve local problems #45

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
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
87 changes: 49 additions & 38 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ serde_json = "1.0"
serde_derive = "1.0"
rand = "0.6.5"
regex = "1.3.4"
futures = { version = "0.3.3", features = ["thread-pool"] }
futures = { version = "0.3.5", features = ["thread-pool"] }
surf = "1.0.3"

[lib]
49 changes: 46 additions & 3 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use serde_json::Value;
use std::fmt::{Display, Error, Formatter};

const PROBLEMS_URL: &str = "https://leetcode.com/api/problems/algorithms/";
const GRAPHQL_URL: &str = "https://leetcode.com/graphql";
const GRAPHQL_URL: &str = "https://leetcode.cn/graphql";
const QUESTION_QUERY_STRING: &str = r#"
query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
@@ -18,6 +18,15 @@ query questionData($titleSlug: String!) {
}"#;
const QUESTION_QUERY_OPERATION: &str = "questionData";

const QUESTION_TRANSLATIONS_QUERY_STRING: &str = r#"
query questionTranslations($titleSlug: String!) {
question(titleSlug: $titleSlug) {
translatedTitle
translatedContent
}
}"#;
const QUESTION_TRANSLATIONS_QUERY_OPERATION: &str = "questionTranslations";

pub fn get_problem(frontend_question_id: u32) -> Option<Problem> {
let problems = get_problems().unwrap();
for problem in problems.stat_status_pairs.iter() {
@@ -36,11 +45,20 @@ pub fn get_problem(frontend_question_id: u32) -> Option<Problem> {
.unwrap()
.json()
.unwrap();
let resp_translation: RawTranslationProblem = client
.post(GRAPHQL_URL)
.json(&Query::question_translation_query(
problem.stat.question_title_slug.as_ref().unwrap(),
))
.send()
.unwrap()
.json()
.unwrap();
return Some(Problem {
title: problem.stat.question_title.clone().unwrap(),
title: resp_translation.data.question.translated_title,
title_slug: problem.stat.question_title_slug.clone().unwrap(),
code_definition: serde_json::from_str(&resp.data.question.code_definition).unwrap(),
content: resp.data.question.content,
content: resp_translation.data.question.translated_content,
sample_test_case: resp.data.question.sample_test_case,
difficulty: problem.difficulty.to_string(),
question_id: problem.stat.frontend_question_id,
@@ -138,6 +156,13 @@ impl Query {
query: QUESTION_QUERY_STRING.to_owned(),
}
}
fn question_translation_query(title_slug: &str) -> Query {
Query {
operation_name: QUESTION_TRANSLATIONS_QUERY_OPERATION.to_owned(),
variables: json!({ "titleSlug": title_slug }),
query: QUESTION_TRANSLATIONS_QUERY_STRING.to_owned(),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
@@ -162,6 +187,24 @@ struct Question {
meta_data: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct RawTranslationProblem {
data: TranslationData,
}

#[derive(Debug, Serialize, Deserialize)]
struct TranslationData {
question: TranslationQuestion,
}

#[derive(Debug, Serialize, Deserialize)]
struct TranslationQuestion {
#[serde(rename = "translatedTitle")]
translated_title: String,
#[serde(rename = "translatedContent")]
translated_content: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Problems {
user_name: String,
28 changes: 17 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -189,12 +189,12 @@ fn parse_extra_use(code: &str) -> String {
}

fn parse_problem_link(problem: &Problem) -> String {
format!("https://leetcode.com/problems/{}/", problem.title_slug)
format!("https://leetcode.cn/problems/{}/", problem.title_slug)
}

fn parse_discuss_link(problem: &Problem) -> String {
format!(
"https://leetcode.com/problems/{}/discuss/?currentPage=1&orderBy=most_votes&query=",
"https://leetcode.cn/problems/{}/discuss/?currentPage=1&orderBy=most_votes&query=",
problem.title_slug
)
}
@@ -277,22 +277,28 @@ fn build_desc(content: &str) -> String {
}

fn deal_solving(id: &u32) {
let problem = fetcher::get_problem(*id).unwrap();
let file_name = format!(
"p{:04}_{}",
problem.question_id,
problem.title_slug.replace("-", "_")
);
let problem_pattern = Regex::new(format!(r"^p{:04}_[^.]+", id).as_str()).unwrap();
let mut file_name = String::new();
for entry in fs::read_dir("./src/problem").unwrap() {
let entry = entry.unwrap();
let fname = entry.file_name();
match problem_pattern.find(fname.to_str().unwrap()) {
Some(m) => {
file_name = String::from(m.as_str());
break;
},
None => continue,
}
}
let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name));
// check problem/ existence
if !file_path.exists() {
panic!("problem does not exist");
}
// check solution/ no existence
let solution_name = format!(
"s{:04}_{}",
problem.question_id,
problem.title_slug.replace("-", "_")
"s{}",
&file_name[1..]
);
let solution_path = Path::new("./src/solution").join(format!("{}.rs", solution_name));
if solution_path.exists() {