feat: cache marka api responses
This commit is contained in:
@@ -6,13 +6,15 @@ import * as openai from "@lib/openai.ts";
|
||||
import { getYoutubeVideoDetails } from "@lib/youtube.ts";
|
||||
import {
|
||||
extractYoutubeId,
|
||||
formatDate,
|
||||
isYoutubeLink,
|
||||
toUrlSafeString,
|
||||
safeFileName,
|
||||
} from "@lib/string.ts";
|
||||
import { createLogger } from "@lib/log/index.ts";
|
||||
import { createResource } from "@lib/marka/index.ts";
|
||||
import { webScrape } from "@lib/webScraper.ts";
|
||||
import { ArticleResource } from "@lib/marka/schema.ts";
|
||||
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
|
||||
|
||||
const log = createLogger("api/article");
|
||||
|
||||
@@ -44,16 +46,34 @@ async function processCreateArticle(
|
||||
streamResponse.enqueue("postprocessing article");
|
||||
|
||||
const title = result?.title || aiMeta?.headline || "";
|
||||
const id = toUrlSafeString(title);
|
||||
|
||||
let finalPath = result.image;
|
||||
if (result?.image) {
|
||||
const extension = fileExtension(result?.image);
|
||||
const imagePath = `resources/articles/images/${
|
||||
safeFileName(title)
|
||||
}_cover.${extension}`;
|
||||
try {
|
||||
streamResponse.enqueue("downloading image");
|
||||
const res = await fetch(result.image);
|
||||
streamResponse.enqueue("saving image");
|
||||
const buffer = await res.arrayBuffer();
|
||||
await createResource(imagePath, buffer);
|
||||
finalPath = imagePath;
|
||||
} catch (err) {
|
||||
console.log("Failed to save image", err);
|
||||
}
|
||||
}
|
||||
|
||||
const newArticle: ArticleResource["content"] = {
|
||||
_type: "Article",
|
||||
headline: title,
|
||||
articleBody: result.content,
|
||||
url: fetchUrl,
|
||||
datePublished: result?.published || aiMeta?.datePublished ||
|
||||
new Date().toISOString(),
|
||||
image: result?.image,
|
||||
datePublished: formatDate(
|
||||
result?.published || aiMeta?.datePublished || undefined,
|
||||
),
|
||||
image: finalPath,
|
||||
author: {
|
||||
_type: "Person",
|
||||
name: (result.schemaOrgData?.author?.name || aiMeta?.author || "")
|
||||
@@ -66,9 +86,9 @@ async function processCreateArticle(
|
||||
|
||||
streamResponse.enqueue("writing to disk");
|
||||
|
||||
await createResource(`articles/${id}.md`, newArticle);
|
||||
await createResource(`articles/${title}.md`, newArticle);
|
||||
|
||||
streamResponse.enqueue("id: " + id);
|
||||
streamResponse.enqueue("id: " + title);
|
||||
}
|
||||
|
||||
async function processCreateYoutubeVideo(
|
||||
|
||||
Reference in New Issue
Block a user