mirror of
https://gitlab.com/Anson-Projects/anson-stuff/zinetest.git
synced 2025-06-15 13:36:39 +00:00
images for erryone
This commit is contained in:
parent
884ba6fd92
commit
684bad8a67
46
src/main.rs
46
src/main.rs
@ -23,6 +23,19 @@ fn fetch_feed(url: &str) -> Result<Vec<Entry>, Box<dyn Error>> {
|
||||
Ok(feed.entries)
|
||||
}
|
||||
|
||||
fn fetch_social_image(url: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let html = reqwest::blocking::get(url)?.text()?;
|
||||
let document = Html::parse_document(&html);
|
||||
let selector = Selector::parse("meta[property=\"og:image\"]").unwrap();
|
||||
|
||||
let image_url = document
|
||||
.select(&selector)
|
||||
.next()
|
||||
.and_then(|element| element.value().attr("content"))
|
||||
.unwrap_or("");
|
||||
|
||||
Ok(image_url.to_string())
|
||||
}
|
||||
fn create_html_card(entry: &Entry) -> Markup {
|
||||
let title = entry
|
||||
.title
|
||||
@ -30,15 +43,27 @@ fn create_html_card(entry: &Entry) -> Markup {
|
||||
.map_or_else(|| "".to_string(), |t| t.content.clone());
|
||||
|
||||
let link = entry.links.first().unwrap();
|
||||
let lang = link.clone().href_lang.unwrap_or("en".to_string());
|
||||
|
||||
// Attempt to find the first thumbnail URL in the media content
|
||||
let image_src = entry
|
||||
if lang != "en" {
|
||||
println!("Non english! {} {}", lang, link.href);
|
||||
}
|
||||
|
||||
let mut image_url = entry
|
||||
.media
|
||||
.iter()
|
||||
.flat_map(|media| &media.thumbnails)
|
||||
.map(|thumbnail| &thumbnail.image.uri)
|
||||
.next()
|
||||
.map_or_else(|| "".to_string(), |uri| uri.to_string());
|
||||
.first()
|
||||
.and_then(|m| m.content.first())
|
||||
.and_then(|c| c.url.as_ref().map(|u| u.to_string()))
|
||||
.unwrap_or_default();
|
||||
|
||||
// Fallback to fetching social image if direct extraction didn't work
|
||||
if image_url.is_empty() {
|
||||
println!(
|
||||
"Falling back to searching for a social image for {}",
|
||||
link.href
|
||||
);
|
||||
image_url = fetch_social_image(link.href.as_str()).unwrap_or_default();
|
||||
}
|
||||
|
||||
let description = entry.content.as_ref().map_or_else(
|
||||
|| {
|
||||
@ -69,8 +94,9 @@ fn create_html_card(entry: &Entry) -> Markup {
|
||||
}
|
||||
}
|
||||
body {
|
||||
@if !image_src.is_empty() {
|
||||
img src=(image_src) alt="Entry image";
|
||||
@if !image_url.is_empty() {
|
||||
img src=(image_url) alt="Entry image";
|
||||
p;
|
||||
}
|
||||
p { (truncated_description) }
|
||||
}
|
||||
@ -276,6 +302,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
entries
|
||||
.sort_by_key(|entry| Reverse(entry.published.unwrap_or(entry.updated.unwrap_or_default())));
|
||||
|
||||
entries.truncate(30);
|
||||
|
||||
let html_string = generate_index(entries).into_string();
|
||||
|
||||
let output_path = Path::new("output/index.html");
|
||||
|
Loading…
x
Reference in New Issue
Block a user