1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-20 00:15:17 +00:00

Start to fetch page content

This commit is contained in:
2021-04-08 16:45:29 +02:00
parent d53f0aae7b
commit 74adef5221
5 changed files with 495 additions and 90 deletions

View File

@ -10,4 +10,5 @@ pub mod string_utils;
pub mod pdf_utils;
pub mod mp3_utils;
pub mod mp4_utils;
pub mod zip_utils;
pub mod zip_utils;
pub mod webpage_utils;

View File

@ -0,0 +1,32 @@
//! # 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(5);
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()),
})
}