Archived
1
0
Fork 0
forked from Anri/cal8tor

use custom user-agent

This commit is contained in:
Mylloon 2022-08-29 11:44:35 +02:00
parent 578657d350
commit 6bc4f69cd5
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 17 additions and 13 deletions

View file

@ -3,8 +3,10 @@ use regex::{Captures, Regex};
use scraper::{Html, Selector};
use std::collections::HashMap;
pub async fn info() -> HashMap<usize, Vec<(DateTime<Utc>, i64)>> {
let document = get_webpage().await.expect("Can't reach info website.");
pub async fn info(user_agent: &str) -> HashMap<usize, Vec<(DateTime<Utc>, 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<usize, Vec<(DateTime<Utc>, i64)>> {
}
/// Get info webpage
async fn get_webpage() -> Result<Html, Box<dyn std::error::Error>> {
async fn get_webpage(user_agent: &str) -> Result<Html, Box<dyn std::error::Error>> {
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

View file

@ -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

View file

@ -15,10 +15,11 @@ pub async fn timetable(
year: i8,
semester_opt: Option<i8>,
letter: Option<char>,
user_agent: &str,
) -> (Vec<String>, (usize, Vec<models::Day>)) {
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<char>,
user_agent: &str,
) -> Result<Html, Box<dyn std::error::Error>> {
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);