update
This commit is contained in:
parent
406d664697
commit
de5a55298f
3 changed files with 441 additions and 307 deletions
File diff suppressed because one or more lines are too long
|
@ -26,6 +26,11 @@ impl TimeSince {
|
||||||
pub async fn get(state: State<AppState>) -> HomeTemplate {
|
pub async fn get(state: State<AppState>) -> HomeTemplate {
|
||||||
let mut t = vec![];
|
let mut t = vec![];
|
||||||
|
|
||||||
|
let client = reqwest::Client::builder()
|
||||||
|
.user_agent("warthunder_leak_counter.lelgenio.com")
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
for source in sources::sources() {
|
for source in sources::sources() {
|
||||||
let url = source.url();
|
let url = source.url();
|
||||||
|
|
||||||
|
@ -54,12 +59,21 @@ pub async fn get(state: State<AppState>) -> HomeTemplate {
|
||||||
|
|
||||||
if needs_update {
|
if needs_update {
|
||||||
tracing::info!("Need update cache");
|
tracing::info!("Need update cache");
|
||||||
let Ok(res) = (reqwest::get(url.clone())).await else {
|
let Ok(res) = (client.get(url.clone())).send().await else {
|
||||||
tracing::error!("fetch error");
|
tracing::error!("fetch error");
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let Ok(text) = res.text().await else {
|
let status = res.status();
|
||||||
|
let text = res.text().await;
|
||||||
|
|
||||||
|
if !status.is_success() {
|
||||||
|
tracing::error!("fetch returned status {status}");
|
||||||
|
tracing::error!("response body {text:?}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(text) = text else {
|
||||||
tracing::error!("fetch decode text error");
|
tracing::error!("fetch decode text error");
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
@ -73,9 +87,12 @@ pub async fn get(state: State<AppState>) -> HomeTemplate {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let Ok(last) = source.latest_leak(text) else {
|
let last = match source.latest_leak(text) {
|
||||||
tracing::error!("source decode error");
|
Ok(last) => last,
|
||||||
continue;
|
Err(err) => {
|
||||||
|
tracing::error!("source decode error: {err:#?}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
t.push(last);
|
t.push(last);
|
||||||
|
|
|
@ -12,10 +12,11 @@ impl Source for Wikipedia {
|
||||||
"https://en.wikipedia.org/wiki/War_Thunder".to_string()
|
"https://en.wikipedia.org/wiki/War_Thunder".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip_all, err)]
|
||||||
fn latest_leak(&self, html: &str) -> Result<time::Date> {
|
fn latest_leak(&self, html: &str) -> Result<time::Date> {
|
||||||
let soup = soup::Soup::new(html);
|
let soup = soup::Soup::new(html);
|
||||||
|
|
||||||
let tables = soup.tag("table").find_all();
|
let tables: Vec<_> = soup.tag("table").find_all().collect();
|
||||||
|
|
||||||
let tables_with_classified = tables
|
let tables_with_classified = tables
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -23,8 +24,9 @@ impl Source for Wikipedia {
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let table = match &tables_with_classified[..] {
|
let table = match &tables_with_classified[..] {
|
||||||
|
[] => bail!("No tables found"),
|
||||||
[table] => table,
|
[table] => table,
|
||||||
_ => bail!("Cannot reliably find leaks table"),
|
_ => bail!("Found more than one table with the string Classified"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let lines: Vec<String> = table
|
let lines: Vec<String> = table
|
||||||
|
@ -83,7 +85,7 @@ fn test_wikipedia_html_parse() {
|
||||||
let html = std::fs::read_to_string("./data/wikipedia.html").unwrap();
|
let html = std::fs::read_to_string("./data/wikipedia.html").unwrap();
|
||||||
|
|
||||||
let real = Wikipedia.latest_leak(&html).unwrap();
|
let real = Wikipedia.latest_leak(&html).unwrap();
|
||||||
let expected = time::Date::from_calendar_date(2023, time::Month::December, 12).unwrap();
|
let expected = time::Date::from_calendar_date(2025, time::Month::June, 23).unwrap();
|
||||||
|
|
||||||
assert_eq!(expected, real);
|
assert_eq!(expected, real);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue