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 {
|
||||
let mut t = vec![];
|
||||
|
||||
let client = reqwest::Client::builder()
|
||||
.user_agent("warthunder_leak_counter.lelgenio.com")
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
for source in sources::sources() {
|
||||
let url = source.url();
|
||||
|
||||
|
@ -54,12 +59,21 @@ pub async fn get(state: State<AppState>) -> HomeTemplate {
|
|||
|
||||
if needs_update {
|
||||
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");
|
||||
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");
|
||||
continue;
|
||||
};
|
||||
|
@ -73,9 +87,12 @@ pub async fn get(state: State<AppState>) -> HomeTemplate {
|
|||
continue;
|
||||
};
|
||||
|
||||
let Ok(last) = source.latest_leak(text) else {
|
||||
tracing::error!("source decode error");
|
||||
let last = match source.latest_leak(text) {
|
||||
Ok(last) => last,
|
||||
Err(err) => {
|
||||
tracing::error!("source decode error: {err:#?}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
t.push(last);
|
||||
|
|
|
@ -12,10 +12,11 @@ impl Source for Wikipedia {
|
|||
"https://en.wikipedia.org/wiki/War_Thunder".to_string()
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all, err)]
|
||||
fn latest_leak(&self, html: &str) -> Result<time::Date> {
|
||||
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
|
||||
.into_iter()
|
||||
|
@ -23,8 +24,9 @@ impl Source for Wikipedia {
|
|||
.collect::<Vec<_>>();
|
||||
|
||||
let table = match &tables_with_classified[..] {
|
||||
[] => bail!("No tables found"),
|
||||
[table] => table,
|
||||
_ => bail!("Cannot reliably find leaks table"),
|
||||
_ => bail!("Found more than one table with the string Classified"),
|
||||
};
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue