mirror of
https://gitlab.com/Anson-Projects/anson-stuff/zinetest.git
synced 2025-06-15 21:46:40 +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)
|
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 {
|
fn create_html_card(entry: &Entry) -> Markup {
|
||||||
let title = entry
|
let title = entry
|
||||||
.title
|
.title
|
||||||
@ -30,15 +43,27 @@ fn create_html_card(entry: &Entry) -> Markup {
|
|||||||
.map_or_else(|| "".to_string(), |t| t.content.clone());
|
.map_or_else(|| "".to_string(), |t| t.content.clone());
|
||||||
|
|
||||||
let link = entry.links.first().unwrap();
|
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
|
if lang != "en" {
|
||||||
let image_src = entry
|
println!("Non english! {} {}", lang, link.href);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut image_url = entry
|
||||||
.media
|
.media
|
||||||
.iter()
|
.first()
|
||||||
.flat_map(|media| &media.thumbnails)
|
.and_then(|m| m.content.first())
|
||||||
.map(|thumbnail| &thumbnail.image.uri)
|
.and_then(|c| c.url.as_ref().map(|u| u.to_string()))
|
||||||
.next()
|
.unwrap_or_default();
|
||||||
.map_or_else(|| "".to_string(), |uri| uri.to_string());
|
|
||||||
|
// 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(
|
let description = entry.content.as_ref().map_or_else(
|
||||||
|| {
|
|| {
|
||||||
@ -69,8 +94,9 @@ fn create_html_card(entry: &Entry) -> Markup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
body {
|
body {
|
||||||
@if !image_src.is_empty() {
|
@if !image_url.is_empty() {
|
||||||
img src=(image_src) alt="Entry image";
|
img src=(image_url) alt="Entry image";
|
||||||
|
p;
|
||||||
}
|
}
|
||||||
p { (truncated_description) }
|
p { (truncated_description) }
|
||||||
}
|
}
|
||||||
@ -276,6 +302,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
entries
|
entries
|
||||||
.sort_by_key(|entry| Reverse(entry.published.unwrap_or(entry.updated.unwrap_or_default())));
|
.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 html_string = generate_index(entries).into_string();
|
||||||
|
|
||||||
let output_path = Path::new("output/index.html");
|
let output_path = Path::new("output/index.html");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user