Skip to content

Commit

Permalink
debug reqwest
Browse files Browse the repository at this point in the history
  • Loading branch information
Aif4thah committed Sep 17, 2024
1 parent dffb414 commit 83d482e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion quality/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ edition = "2021"
[dependencies]
walkdir = "2.3.2"
regex = "1.5.4"
reqwest = { version = "0.12", features = ["blocking", "json"] }
reqwest = { version = "0.12", features = ["blocking", "json", "default-tls"] }

43 changes: 20 additions & 23 deletions quality/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT, ACCEPT, ACCEPT_LANGUAG

fn main() {

// 0. Lancement de répertoire Dojo-101
// 0. Lancement de répertoire Dojo-101

let current_dir = std::env::current_dir().unwrap();
let parent_dir = current_dir.parent().unwrap();
println!("\n[*] Dojo 101 path: {:?}", parent_dir);

// 1. Affiche le nombre de fichiers, les fichiers qui ne sont pas au format markdown, et les 5 plus vieux fichiers
// 1. Affiche le nombre de fichiers, les fichiers qui ne sont pas au format markdown, et les 5 plus vieux fichiers

let mut files = vec![];
let mut non_markdown_files = vec![];

for entry in WalkDir::new(parent_dir).min_depth(2).max_depth(3) {
let entry = entry.unwrap();
let path = entry.path();

// Vérifie si le fichier est dans un dossier à ignorer
let mut dojo101_file = true;

let mut dojo101_file = true; // Vérifie si le fichier est dans un dossier à ignorer
for ancestor in path.ancestors() {
let dir_name = ancestor.file_name().unwrap_or_default().to_str().unwrap();
if dir_name == "quality" || dir_name.starts_with('.') {
Expand All @@ -60,8 +59,7 @@ fn main() {

println!("\n[*] 5 oldest files: {:?}", &files[..5.min(files.len())]);

// 2. Vérifie qu'il n'y a pas de dossier dans les sous dossiers

// 2. Vérifie qu'il n'y a pas de dossier dans les sous dossiers

for entry in WalkDir::new(parent_dir).min_depth(1).max_depth(1) {
let entry = entry.unwrap();
Expand All @@ -78,31 +76,30 @@ fn main() {
}
}

// 3. Extrait la liste des URLs contenues dans les liens markdown (sous forme "[ref](url)")
// 3. Extrait la liste des URLs contenues dans les liens markdown (sous forme "[ref](url)")

let url_regex = Regex::new(r"\[([^\]]+)\]\((https?://[^\s\)]+)\)").unwrap();
let mut urls = HashSet::new();
let url_regex = Regex::new(r"\[([^\]]+)\]\((https?://[^\s\)]+)\)").unwrap();
let mut urls = HashSet::new();

for path in files.iter().filter(|p| p.extension().unwrap_or_default() == "md") {
let content = fs::read_to_string(path).unwrap();
for cap in url_regex.captures_iter(&content) {
urls.insert(cap[2].to_string());
for path in files.iter().filter(|p| p.extension().unwrap_or_default() == "md") {
let content = fs::read_to_string(path).unwrap();
for cap in url_regex.captures_iter(&content) {
urls.insert(cap[2].to_string());
}
}
}

println!("\n[*] Unique URLs in markdown files: {:?}", urls);
//println!("\n[*] Unique URLs in markdown files: {:?}", urls);

// 4. Vérifie la validité des URLs
// 4. Vérifie la validité des URLs

let client = Client::new();
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_static("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0"));
headers.insert(ACCEPT, HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.5"));
headers.insert(ACCEPT_ENCODING, HeaderValue::from_static("gzip, deflate, br"));
headers.insert(USER_AGENT, HeaderValue::from_static("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0"));
headers.insert(ACCEPT, HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/png,image/svg+xml,*/*;q=0.8"));
headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"));
headers.insert(ACCEPT_ENCODING, HeaderValue::from_static("gzip, deflate, br, zstd"));

for url in &urls {
match client.head(url).headers(headers.clone()).send() {
match client.get(url).headers(headers.clone()).send() {
Ok(response) => {
if response.status().is_success() {
//println!("\n[*] URL is valid: {}", url);
Expand Down

0 comments on commit 83d482e

Please sign in to comment.