mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-03-14 01:42:37 +00:00
32 lines
1.1 KiB
Rust
32 lines
1.1 KiB
Rust
//! # Webpage utilities
|
|
//!
|
|
//! @author Pierre Hubert
|
|
|
|
use std::time::Duration;
|
|
|
|
use webpage::{Webpage, WebpageOptions};
|
|
|
|
use crate::data::error::{ExecError, Res};
|
|
use crate::data::post::PostWebLink;
|
|
|
|
/// Attempt to get information about a URL given by a user
|
|
pub fn get_post_web_link(url: &str) -> Res<PostWebLink> {
|
|
let mut options = WebpageOptions::default();
|
|
options.max_redirections = 2;
|
|
options.follow_location = true;
|
|
options.useragent = "OpenGraph Fetcher v1.0".to_string();
|
|
options.timeout = Duration::from_secs(3);
|
|
|
|
let page = Webpage::from_url(url, options)?;
|
|
|
|
/*if !page.http.content_type.to_lowercase().starts_with("text/html") {
|
|
return Err(ExecError::boxed_string(format!("The page at {} as an invalid content type: {}!", url, page.http.content_type)));
|
|
}*/
|
|
|
|
Ok(PostWebLink {
|
|
url: url.to_string(),
|
|
title: page.html.opengraph.properties.get("title").map(String::to_string),
|
|
description: page.html.opengraph.properties.get("description").map(String::to_string),
|
|
image: page.html.opengraph.images.first().map(|m| m.url.to_string()),
|
|
})
|
|
} |