From 4eb510039c9cd701f414d9994fa412bbc4e41dc9 Mon Sep 17 00:00:00 2001 From: xenofem Date: Fri, 9 Dec 2022 17:43:49 -0500 Subject: [PATCH] use cached pdf if MWRA returns 404 or something --- src/fetch.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/fetch.rs b/src/fetch.rs index bd185cd..96f02d7 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -1,8 +1,11 @@ -use std::{time::{Duration, Instant, SystemTime}, path::PathBuf}; +use std::{ + path::PathBuf, + time::{Duration, Instant, SystemTime}, +}; use futures::{sink::SinkExt, TryStreamExt}; use lazy_static::lazy_static; -use log::info; +use log::{info, warn}; use reqwest::Url; use scraper::Selector; use time::PrimitiveDateTime; @@ -94,11 +97,9 @@ impl PdfFetcher { .ok_or(Error::NotFound)?; let pdf_url = CHARTS_URL.join(pdf_href)?; - let origin_modtime = self - .client - .head(pdf_url.clone()) - .send() - .await? + let head_resp = self.client.head(pdf_url.clone()).send().await?; + + let origin_modtime = head_resp .headers() .get(reqwest::header::LAST_MODIFIED) .and_then(|val| { @@ -115,7 +116,13 @@ impl PdfFetcher { .zip(origin_modtime) .map_or(true, |(cache, origin)| origin > cache); - if outdated { + if !head_resp.status().is_success() { + warn!( + "MWRA server returned unexpected response, not fetching updated PDF: {:?}", + head_resp + ); + // Just use the cached PDF, assuming we have it + } else if outdated { info!("Cached PDF is outdated, downloading latest version"); let mut pdf_stream = self