From 6bc4f69cd524b8891fee5bd9ace2c8377b79197c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 29 Aug 2022 11:44:35 +0200 Subject: [PATCH] use custom user-agent --- src/info.rs | 15 +++++++-------- src/main.rs | 6 ++++-- src/timetable.rs | 9 ++++++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/info.rs b/src/info.rs index a944d87..1bf6c6d 100644 --- a/src/info.rs +++ b/src/info.rs @@ -3,8 +3,10 @@ use regex::{Captures, Regex}; use scraper::{Html, Selector}; use std::collections::HashMap; -pub async fn info() -> HashMap, i64)>> { - let document = get_webpage().await.expect("Can't reach info website."); +pub async fn info(user_agent: &str) -> HashMap, i64)>> { + let document = get_webpage(user_agent) + .await + .expect("Can't reach info website."); // Selectors let sel_ul = Selector::parse("ul").unwrap(); @@ -58,14 +60,11 @@ pub async fn info() -> HashMap, i64)>> { } /// Get info webpage -async fn get_webpage() -> Result> { +async fn get_webpage(user_agent: &str) -> Result> { let url = "https://informatique.up8.edu/licence-iv/edt"; - // We don't use reqwest::get() but a client with a custom user-agent - // in order to avoid getting rate limit - let client = reqwest::Client::builder() - .user_agent("bypass-rate_limit") - .build()?; + // Use custom User-Agent + let client = reqwest::Client::builder().user_agent(user_agent).build()?; let html = client.get(url).send().await?.text().await?; // Panic on error diff --git a/src/main.rs b/src/main.rs index e8ff4e5..3601041 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,16 +51,18 @@ async fn main() { None => "", }; + let user_agent = format!("cal8tor/{}", env!("CARGO_PKG_VERSION")); + println!( "Récupération de l'emploi du temps des L{}{}{}...", year, seperator, letter.unwrap_or_default().to_uppercase() ); - let timetable = timetable::timetable(year, args.semester, letter).await; + let timetable = timetable::timetable(year, args.semester, letter, &user_agent).await; println!("Récupération des informations par rapport à l'année..."); - let info = info::info().await; + let info = info::info(&user_agent).await; if args.export.is_some() { // Export the calendar diff --git a/src/timetable.rs b/src/timetable.rs index 7b0e74c..4cbaa5b 100644 --- a/src/timetable.rs +++ b/src/timetable.rs @@ -15,10 +15,11 @@ pub async fn timetable( year: i8, semester_opt: Option, letter: Option, + user_agent: &str, ) -> (Vec, (usize, Vec)) { let semester = get_semester(semester_opt, letter); - let document = get_webpage(year, semester, letter) + let document = get_webpage(year, semester, letter, user_agent) .await .expect("Can't reach timetable website."); @@ -126,6 +127,7 @@ async fn get_webpage( year: i8, semester: i8, letter: Option, + user_agent: &str, ) -> Result> { let url = { let panic_semester_message = "Unknown semester."; @@ -168,8 +170,9 @@ async fn get_webpage( } }; - // Get raw html - let html = reqwest::get(&url).await?.text().await?; + // Use custom User-Agent + let client = reqwest::Client::builder().user_agent(user_agent).build()?; + let html = client.get(&url).send().await?.text().await?; // Panic on error crate::utils::check_errors(&html, &url);