mirror of
https://github.com/lumehq/lume.git
synced 2025-03-28 18:52:33 +01:00
feat: update fetch opg function
This commit is contained in:
parent
1be84f3139
commit
3005d27403
24
packages/ark/src/hooks/usePreview.ts
Normal file
24
packages/ark/src/hooks/usePreview.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
export function usePreview(url: string) {
|
||||
const { isLoading, isError, data } = useQuery({
|
||||
queryKey: ["url", url],
|
||||
queryFn: async () => {
|
||||
try {
|
||||
const cmd = await invoke("fetch_opg", { url });
|
||||
console.log(cmd);
|
||||
return cmd;
|
||||
} catch (e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
},
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnMount: false,
|
||||
refetchOnReconnect: false,
|
||||
staleTime: Infinity,
|
||||
retry: 2,
|
||||
});
|
||||
|
||||
return { isLoading, isError, data };
|
||||
}
|
@ -1,54 +1,16 @@
|
||||
use std::time::Duration;
|
||||
use webpage::{Webpage, WebpageOptions};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct OpenGraphResponse {
|
||||
title: String,
|
||||
description: String,
|
||||
url: String,
|
||||
image: String,
|
||||
}
|
||||
use webpage::{Opengraph, Webpage, WebpageOptions};
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fetch_opg(url: String) -> Result<OpenGraphResponse, ()> {
|
||||
pub fn fetch_opg(url: String) -> Result<Opengraph, String> {
|
||||
let mut options = WebpageOptions::default();
|
||||
options.allow_insecure = true;
|
||||
options.max_redirections = 3;
|
||||
options.timeout = Duration::from_secs(15);
|
||||
options.max_redirections = 2;
|
||||
options.timeout = Duration::from_secs(10);
|
||||
|
||||
let info = Webpage::from_url(&url, options);
|
||||
|
||||
if let Ok(data) = info {
|
||||
let html = data.html;
|
||||
let result = OpenGraphResponse {
|
||||
title: html
|
||||
.opengraph
|
||||
.properties
|
||||
.get("title")
|
||||
.cloned()
|
||||
.unwrap_or_default(),
|
||||
description: html
|
||||
.opengraph
|
||||
.properties
|
||||
.get("description")
|
||||
.cloned()
|
||||
.unwrap_or_default(),
|
||||
url: html
|
||||
.opengraph
|
||||
.properties
|
||||
.get("url")
|
||||
.cloned()
|
||||
.unwrap_or_default(),
|
||||
image: html
|
||||
.opengraph
|
||||
.images
|
||||
.get(0)
|
||||
.and_then(|i| Some(i.url.clone()))
|
||||
.unwrap_or_default(),
|
||||
};
|
||||
|
||||
Ok(result.into())
|
||||
if let Ok(data) = Webpage::from_url(&url, options) {
|
||||
Ok(data.html.opengraph.into())
|
||||
} else {
|
||||
Err(())
|
||||
Err("Get open graph failed".into())
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,8 @@ pub mod commands;
|
||||
pub mod nostr;
|
||||
pub mod tray;
|
||||
|
||||
use std::fs;
|
||||
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::fs;
|
||||
use tauri::Manager;
|
||||
use tauri_plugin_autostart::MacosLauncher;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user