1
0
mirror of https://gitlab.com/Anson-Projects/zine.git synced 2025-06-16 13:36:40 +00:00

Make sure that featured post always has an image

This commit is contained in:
Anson Biggs 2024-04-26 03:41:15 +00:00
parent 5b018ad267
commit 5f5c0af02d
2 changed files with 41 additions and 4 deletions

View File

@ -33,12 +33,25 @@ fn main() -> Result<(), Box<dyn Error>> {
entries.retain(|entry| entry.score.is_positive()); entries.retain(|entry| entry.score.is_positive());
entries.par_iter_mut().for_each(|entry| { entries.par_iter_mut().for_each(|entry| {
if entry.image_url.is_some() { if let Some(image_url) = &entry.image_url {
entry.score += 1440; match web_fetchers::is_valid_image_url(image_url) {
Ok(true) => {
entry.score += 1440;
}
_ => {
entry.image_url = None;
entry.score += -1440;
}
}
} else { } else {
match web_fetchers::fetch_social_image(entry.link.clone()) { match web_fetchers::fetch_social_image(entry.link.clone()) {
Ok(social_image_url) => { Ok(social_image_url) => {
entry.image_url = Some(social_image_url); if web_fetchers::is_valid_image_url(&social_image_url).unwrap_or(false) {
entry.image_url = Some(social_image_url);
entry.score += 1440;
} else {
entry.score += -1440;
}
} }
Err(error) => { Err(error) => {
log::info!("{}: {}", error, entry.link.clone()); log::info!("{}: {}", error, entry.link.clone());
@ -47,7 +60,6 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
} }
}); });
entries.retain(|entry| entry.score.is_positive()); entries.retain(|entry| entry.score.is_positive());
// Count occurences of main urls // Count occurences of main urls
@ -66,6 +78,18 @@ fn main() -> Result<(), Box<dyn Error>> {
// Remove bottom 10% from list // Remove bottom 10% from list
entries.truncate(entries.len() - (entries.len() as f64 * 0.1).ceil() as usize); entries.truncate(entries.len() - (entries.len() as f64 * 0.1).ceil() as usize);
// Make sure first entry has an image since it is used as the featured post
let mut max_iter = 0;
while entries.first().unwrap().image_url.is_none() {
entries[0].score += -100;
entries.sort();
max_iter += 1;
if max_iter > 10000 {
break;
}
}
let index = site_generator::generate_index(entries.clone()); let index = site_generator::generate_index(entries.clone());
let index_path = Path::new("output/index.html"); let index_path = Path::new("output/index.html");
DirBuilder::new() DirBuilder::new()

View File

@ -33,3 +33,16 @@ pub fn fetch_social_image(url: String) -> Result<String, Box<dyn std::error::Err
Err("No social image found".into()) Err("No social image found".into())
} }
} }
pub fn is_valid_image_url(url: &str) -> Result<bool, Box<dyn std::error::Error>> {
let client = reqwest::blocking::Client::new();
let response = client.head(url).send()?;
let status = response.status();
let content_type = response.headers().get(reqwest::header::CONTENT_TYPE);
Ok(status.is_success()
&& content_type.map_or(false, |ct| {
ct.to_str().map_or(false, |s| s.starts_with("image/"))
}))
}