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

filter useless snippets when add a problem and rustfmt code #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 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 @@ -10,6 +10,7 @@ serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
rand = "0.6.5"
regex = "1"

[lib]
doctest = false
Expand Down
62 changes: 40 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
extern crate serde_derive;
#[macro_use]
extern crate serde_json;

extern crate regex;
mod problem;

use regex::Regex;
use std::env;
use std::fs;
use std::path::{Path};
use std::io::Write;
use std::io;
use std::io::Write;
use std::path::Path;

/// main() helps to generate the submission template .rs
fn main() {
Expand All @@ -18,9 +19,10 @@ fn main() {
loop {
println!("Please enter a problem id, or enter \"random\" to generate a random problem.");
let mut is_random = false;
let mut id :u32 = 0;
let mut id: u32 = 0;
let mut id_arg = String::new();
io::stdin().read_line(&mut id_arg)
io::stdin()
.read_line(&mut id_arg)
.expect("Failed to read line");
let id_arg = id_arg.trim();
match id_arg {
Expand All @@ -29,23 +31,30 @@ fn main() {
id = generate_random_id(&solved_ids);
is_random = true;
println!("Generate random problem: {}", &id);
},
}
_ => {
id = id_arg.parse::<u32>().expect(&format!("not a number: {}", id_arg));
id = id_arg
.parse::<u32>()
.expect(&format!("not a number: {}", id_arg));
if solved_ids.contains(&id) {
println!("The problem you chose is invalid (the problem may have been solved \
or may have no rust version).");
println!(
"The problem you chose is invalid (the problem may have been solved \
or may have no rust version)."
);
continue;
}
}
}

let problem = problem::get_problem(id)
.expect(&format!("Error: failed to get problem #{} \
(The problem may be paid-only or may not be exist).",
id));
let code = problem.code_definition.iter()
.filter(|&d| { d.value == "rust" })
let problem = problem::get_problem(id).expect(&format!(
"Error: failed to get problem #{} \
(The problem may be paid-only or may not be exist).",
id
));
let code = problem
.code_definition
.iter()
.filter(|&d| d.value == "rust")
.next();
if code.is_none() {
println!("Problem {} has no rust version.", &id);
Expand Down Expand Up @@ -88,17 +97,20 @@ fn main() {
}
}

fn generate_random_id(except_ids : &Vec<u32>) -> u32 {
use std::fs;
fn generate_random_id(except_ids: &Vec<u32>) -> u32 {
use rand::Rng;
use std::fs;
let mut rng = rand::thread_rng();
loop {
let res :u32 = rng.gen_range(1, 1106);
let res: u32 = rng.gen_range(1, 1106);
if !except_ids.contains(&res) {
return res;
}
println!("Generate a random num ({}), but it is invalid (the problem may have been solved \
or may have no rust version). Regenerate..", res);
println!(
"Generate a random num ({}), but it is invalid (the problem may have been solved \
or may have no rust version). Regenerate..",
res
);
}
}

Expand Down Expand Up @@ -136,7 +148,7 @@ fn parse_extra_use(code: &str) -> String {

fn build_desc(content: &str) -> String {
// TODO: fix this shit
content
let content = content
.replace("<strong>", "")
.replace("</strong>", "")
.replace("<em>", "")
Expand All @@ -147,6 +159,7 @@ fn build_desc(content: &str) -> String {
.replace("</b>", "")
.replace("<pre>", "")
.replace("</pre>", "")
.replace("</font>", "")
.replace("<ul>", "")
.replace("</ul>", "")
.replace("<li>", "")
Expand All @@ -166,5 +179,10 @@ fn build_desc(content: &str) -> String {
.replace("&minus;", "-")
.replace("&#39;", "'")
.replace("\n\n", "\n")
.replace("\n", "\n * ")
.replace("\n", "\n * ");

Regex::new("<font color=\"[a-z]*\">")
.unwrap()
.replace_all(&content, "")
.to_string()
}
27 changes: 15 additions & 12 deletions src/problem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate serde_json;
extern crate reqwest;
extern crate serde_json;

use std::fmt::{Display, Formatter, Error};
use std::fmt::{Display, Error, Formatter};

const PROBLEMS_URL: &str = "https://leetcode.com/api/problems/algorithms/";
const GRAPHQL_URL: &str = "https://leetcode.com/graphql";
Expand All @@ -21,24 +21,28 @@ pub fn get_problem(id: u32) -> Option<Problem> {
let problems = get_problems().unwrap();
for problem in problems.stat_status_pairs.iter() {
if problem.stat.question_id == id {

if problem.paid_only {
return None
return None;
}

let client = reqwest::Client::new();
let resp: RawProblem = client.post(GRAPHQL_URL)
.json(&Query::question_query(problem.stat.question_title_slug.as_ref().unwrap()))
.send().unwrap()
.json().unwrap();
let resp: RawProblem = client
.post(GRAPHQL_URL)
.json(&Query::question_query(
problem.stat.question_title_slug.as_ref().unwrap(),
))
.send()
.unwrap()
.json()
.unwrap();
return Some(Problem {
title: problem.stat.question_title.clone().unwrap(),
title_slug: problem.stat.question_title_slug.clone().unwrap(),
code_definition: serde_json::from_str( & resp.data.question.code_definition).unwrap(),
code_definition: serde_json::from_str(&resp.data.question.code_definition).unwrap(),
content: resp.data.question.content,
sample_test_case: resp.data.question.sample_test_case,
difficulty: problem.difficulty.to_string(),
})
});
}
}
None
Expand Down Expand Up @@ -68,7 +72,6 @@ pub struct CodeDefinition {
pub default_code: String,
}


#[derive(Debug, Serialize, Deserialize)]
struct Query {
#[serde(rename = "operationName")]
Expand All @@ -81,7 +84,7 @@ impl Query {
fn question_query(title_slug: &str) -> Query {
Query {
operation_name: QUESTION_QUERY_OPERATION.to_owned(),
variables: json!({"titleSlug": title_slug}),
variables: json!({ "titleSlug": title_slug }),
query: QUESTION_QUERY_STRING.to_owned(),
}
}
Expand Down