feat: integrate Hardcover API for books
- Add lib/hardcover.ts with GraphQL client for Hardcover API - Add routes/api/books/[name].ts for creating books via Hardcover ID - Add routes/api/books/enhance/[name].ts for enhancing books - Add routes/api/hardcover/query.ts for searching books - Add routes/books/[name].tsx and index.tsx for book pages
This commit is contained in:
242
routes/api/books/create/index.ts
Normal file
242
routes/api/books/create/index.ts
Normal file
@@ -0,0 +1,242 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { Defuddle } from "defuddle/node";
|
||||
import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
|
||||
import { createStreamResponse, isValidUrl } from "@lib/helpers.ts";
|
||||
import * as openai from "@lib/openai.ts";
|
||||
import * as unsplash from "@lib/unsplash.ts";
|
||||
import { getYoutubeVideoDetails } from "@lib/youtube.ts";
|
||||
import {
|
||||
extractYoutubeId,
|
||||
formatDate,
|
||||
isYoutubeLink,
|
||||
safeFileName,
|
||||
toUrlSafeString,
|
||||
} 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 { BookResource } from "@lib/marka/schema.ts";
|
||||
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
|
||||
|
||||
const log = createLogger("api/book");
|
||||
|
||||
async function getUnsplashCoverImage(
|
||||
content: string,
|
||||
streamResponse: ReturnType<typeof createStreamResponse>,
|
||||
): Promise<string | undefined> {
|
||||
try {
|
||||
streamResponse.info("creating unsplash search term");
|
||||
const searchTerm = await openai.createUnsplashSearchTerm(content);
|
||||
if (!searchTerm) return;
|
||||
streamResponse.info(`searching for ${searchTerm}`);
|
||||
const unsplashUrl = await unsplash.getImageBySearchTerm(searchTerm);
|
||||
return unsplashUrl;
|
||||
} catch (e) {
|
||||
log.error("Failed to get unsplash cover image", e);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function ext(str: string) {
|
||||
try {
|
||||
const u = new URL(str);
|
||||
if (u.searchParams.has("fm")) {
|
||||
return u.searchParams.get("fm")!;
|
||||
}
|
||||
return fileExtension(u.pathname);
|
||||
} catch (_e) {
|
||||
return fileExtension(str);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchAndStoreCover(
|
||||
imageUrl: string | undefined,
|
||||
title: string,
|
||||
streamResponse?: ReturnType<typeof createStreamResponse>,
|
||||
): Promise<string | undefined> {
|
||||
if (!imageUrl) return;
|
||||
const imagePath = `books/images/${safeFileName(title)}_cover.${
|
||||
ext(imageUrl)
|
||||
}`;
|
||||
try {
|
||||
streamResponse?.info("downloading image");
|
||||
const res = await fetch(imageUrl);
|
||||
streamResponse?.info("saving image");
|
||||
if (!res.ok) {
|
||||
console.log(`Failed to download remote image: ${imageUrl}`, res.status);
|
||||
return;
|
||||
}
|
||||
const buffer = await res.arrayBuffer();
|
||||
await createResource(imagePath, buffer);
|
||||
return `resources/${imagePath}`;
|
||||
} catch (err) {
|
||||
console.log(`Failed to save image: ${imageUrl}`, err);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async function processCreateBook(
|
||||
{ fetchUrl, streamResponse }: {
|
||||
fetchUrl: string;
|
||||
streamResponse: ReturnType<typeof createStreamResponse>;
|
||||
},
|
||||
) {
|
||||
log.info("create book from url", { url: fetchUrl });
|
||||
|
||||
streamResponse.info("downloading book");
|
||||
|
||||
const result = await webScrape(fetchUrl, streamResponse);
|
||||
|
||||
log.debug("downloaded and parse parsed", result);
|
||||
|
||||
streamResponse.info("parsed book, creating tags with openai");
|
||||
|
||||
const aiMeta = await openai.extractArticleMetadata(result.markdown);
|
||||
|
||||
streamResponse.info("postprocessing book");
|
||||
|
||||
const title = result?.title || aiMeta?.headline || "";
|
||||
|
||||
let coverImagePath: string | undefined = undefined;
|
||||
if (result?.image?.length) {
|
||||
log.debug("using local image for cover image", { image: result.image });
|
||||
coverImagePath = await fetchAndStoreCover(
|
||||
result.image,
|
||||
title,
|
||||
streamResponse,
|
||||
);
|
||||
} else {
|
||||
const urlPath = await getUnsplashCoverImage(
|
||||
result.markdown,
|
||||
streamResponse,
|
||||
);
|
||||
coverImagePath = await fetchAndStoreCover(urlPath, title, streamResponse);
|
||||
log.debug("using unsplash for cover image", { image: coverImagePath });
|
||||
}
|
||||
|
||||
const url = toUrlSafeString(title);
|
||||
|
||||
const newBook: BookResource["content"] = {
|
||||
_type: "Book",
|
||||
headline: title,
|
||||
bookBody: result.markdown,
|
||||
url: fetchUrl,
|
||||
datePublished: formatDate(
|
||||
result?.published || aiMeta?.datePublished || undefined,
|
||||
),
|
||||
image: coverImagePath,
|
||||
author: {
|
||||
_type: "Person",
|
||||
name: (result.schemaOrgData?.author?.name || aiMeta?.author || "")
|
||||
.replace(
|
||||
"@",
|
||||
"twitter:",
|
||||
),
|
||||
},
|
||||
} as const;
|
||||
|
||||
streamResponse.info("writing to disk");
|
||||
|
||||
log.debug("writing to disk", {
|
||||
...newBook,
|
||||
bookBody: newBook.bookBody?.slice(0, 200),
|
||||
});
|
||||
|
||||
await createResource(`books/${url}.md`, newBook);
|
||||
|
||||
streamResponse.send({ type: "finished", url });
|
||||
}
|
||||
|
||||
async function processCreateYoutubeVideo(
|
||||
{ fetchUrl, streamResponse }: {
|
||||
fetchUrl: string;
|
||||
streamResponse: ReturnType<typeof createStreamResponse>;
|
||||
},
|
||||
) {
|
||||
log.info("create youtube book from url", {
|
||||
url: fetchUrl,
|
||||
});
|
||||
|
||||
streamResponse.info("getting video infos from youtube api");
|
||||
|
||||
const youtubeId = extractYoutubeId(fetchUrl);
|
||||
|
||||
const video = await getYoutubeVideoDetails(youtubeId);
|
||||
|
||||
streamResponse.info("shortening title with openai");
|
||||
const videoTitle = await openai.shortenTitle(video.snippet.title) ||
|
||||
video.snippet.title;
|
||||
|
||||
const thumbnail = video?.snippet?.thumbnails?.maxres;
|
||||
const coverImagePath = await fetchAndStoreCover(
|
||||
thumbnail.url,
|
||||
videoTitle || video.snippet.title,
|
||||
streamResponse,
|
||||
);
|
||||
|
||||
const newBook: BookResource["content"] = {
|
||||
_type: "Book",
|
||||
headline: video.snippet.title,
|
||||
bookBody: video.snippet.description,
|
||||
image: coverImagePath,
|
||||
url: fetchUrl,
|
||||
datePublished: formatDate(video.snippet.publishedAt),
|
||||
author: {
|
||||
_type: "Person",
|
||||
name: video.snippet.channelTitle,
|
||||
},
|
||||
};
|
||||
|
||||
streamResponse.info("creating book");
|
||||
|
||||
const filename = toUrlSafeString(videoTitle);
|
||||
|
||||
await createResource(
|
||||
`books/${filename}.md`,
|
||||
newBook,
|
||||
);
|
||||
|
||||
streamResponse.info("finished");
|
||||
|
||||
streamResponse.send({ type: "finished", url: filename });
|
||||
}
|
||||
|
||||
export const handler: Handlers = {
|
||||
GET(req, ctx) {
|
||||
const session = ctx.state.session;
|
||||
if (!session) {
|
||||
throw new AccessDeniedError();
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const fetchUrl = url.searchParams.get("url");
|
||||
|
||||
if (!fetchUrl || !isValidUrl(fetchUrl)) {
|
||||
throw new BadRequestError();
|
||||
}
|
||||
|
||||
const streamResponse = createStreamResponse();
|
||||
|
||||
if (isYoutubeLink(fetchUrl)) {
|
||||
processCreateYoutubeVideo({ fetchUrl, streamResponse }).then(
|
||||
(book) => {
|
||||
log.debug("created book from youtube", { book });
|
||||
},
|
||||
).catch((err) => {
|
||||
log.error(err);
|
||||
}).finally(() => {
|
||||
streamResponse.cancel();
|
||||
});
|
||||
} else {
|
||||
processCreateBook({ fetchUrl, streamResponse }).then((book) => {
|
||||
log.debug("created book from link", { book });
|
||||
}).catch((err) => {
|
||||
log.error(err);
|
||||
}).finally(() => {
|
||||
streamResponse.cancel();
|
||||
});
|
||||
}
|
||||
|
||||
return streamResponse.response;
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user