Skip to content

Commit 25fc463

Browse files
committed
Add book cover image retrieval and update functionality
1 parent 21e6976 commit 25fc463

File tree

2 files changed

+161
-5
lines changed

2 files changed

+161
-5
lines changed

book-covers-db.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
1. get all books in book DB
3+
if cover_image_url_large is null, get book cover image from http://localhost:2000/bookcover?book_title=book+title&author_name=book+author which will return JSON in form
4+
{
5+
"url": "..."
6+
}
7+
2. insert book cover image into book DB
8+
3. if not null, skip
9+
*/
10+
11+
import { createClient } from "@supabase/supabase-js";
12+
import dotenv from "dotenv";
13+
import fs from "fs";
14+
15+
dotenv.config();
16+
17+
const supabaseUrl = process.env.SUPABASE_URL;
18+
const supabaseKey = process.env.SUPABASE_KEY;
19+
const supabase = createClient(supabaseUrl, supabaseKey, {
20+
auth: {
21+
persistSession: false,
22+
},
23+
});
24+
25+
const getBooks = async () => {
26+
const { data, error } = await supabase
27+
.from("books")
28+
.select("*");
29+
30+
if (error) {
31+
console.error(error);
32+
return [];
33+
}
34+
35+
return data;
36+
};
37+
38+
const getBookCover = async (bookTitle, authorName) => {
39+
// convert to url format
40+
bookTitle = bookTitle.replace(" ", "+");
41+
// convert any : or & etc
42+
bookTitle = encodeURIComponent(bookTitle);
43+
authorName = authorName.replace(" ", "+");
44+
authorName = encodeURIComponent(authorName);
45+
46+
try {
47+
const url = `http://localhost:2000/bookcover?book_title=${bookTitle}&author_name=${authorName}`;
48+
const response = await fetch(url);
49+
const data = await response.json();
50+
if (data.error) {
51+
console.log(url);
52+
return { error: data.error };
53+
}
54+
return data.url;
55+
} catch (err) {
56+
console.error(err);
57+
return { error: err };
58+
}
59+
}
60+
61+
const updateBookCover = async (bookId, bookCoverUrl) => {
62+
const { data, error } = await supabase
63+
.from("books")
64+
.update({ cover_image_url_large: bookCoverUrl })
65+
.eq("book_id", bookId);
66+
67+
if (error) {
68+
console.error(error);
69+
return false;
70+
}
71+
72+
return true;
73+
}
74+
75+
const main = async () => {
76+
const books = await getBooks();
77+
for (const book of books) {
78+
if (book.cover_image_url_large === null) {
79+
console.log(`Getting book cover image for ${book.title}.`);
80+
const bookCoverUrl = await getBookCover(book.title, book.author);
81+
console.log(bookCoverUrl);
82+
if (bookCoverUrl === null || bookCoverUrl.error) {
83+
console.error(`Failed to get book cover image for ${book.title}.`);
84+
if (bookCoverUrl.error) {
85+
console.error(bookCoverUrl.error);
86+
}
87+
continue;
88+
}
89+
await updateBookCover(book.book_id, bookCoverUrl);
90+
console.log(`Book cover image for ${book.title} updated.`);
91+
} else {
92+
console.log(`Book cover image for ${book.title} already exists.`);
93+
}
94+
}
95+
}
96+
97+
if (process.env.DEV) {
98+
main();
99+
}

compile-quotes.js

+62-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const openai = new OpenAIApi(configuration);
2222

2323
dotenv.config();
2424

25-
const BOOK_ID = "38531384";
25+
const BOOK_ID = "38318378";
2626

2727
const supabaseUrl = process.env.SUPABASE_URL;
2828
const supabaseKey = process.env.SUPABASE_KEY;
@@ -68,6 +68,20 @@ const compileQuotesFomID = async (bookID) => {
6868
return quotes;
6969
};
7070

71+
const getQuotes = async (bookId) => {
72+
const { data, error } = await supabase
73+
.from("highlights")
74+
.select("*")
75+
.eq("book_id", bookId);
76+
77+
if (error) {
78+
console.error(error);
79+
return [];
80+
}
81+
82+
return data;
83+
};
84+
7185
const assignTopicToCluster = async (cluster) => {
7286
try {
7387
const prompt = `Given the following quotes, what is a good topic for them? Return only the topic as a Markdown heading with no leading #. No bold (**) or italics (*) are needed.`;
@@ -104,6 +118,46 @@ const assignTopicToCluster = async (cluster) => {
104118
}
105119
};
106120

121+
const highlightQuote = async (quote, topic) => {
122+
console.log(`Highlighting quote: ${quote} for topic: ${topic}`);
123+
try {
124+
const prompt = `Given the following quote, highlight the part related to the topic using ** (MD bold). Return the quote with the relevant part highlighted. Say nothing else. Do not include the topic in the response.`;
125+
126+
const completion = await openai.createChatCompletion({
127+
messages: [
128+
{
129+
role: "system",
130+
content: prompt,
131+
},
132+
{
133+
role: "user",
134+
content: `Quote: <start>${quote}<end>\n\nTopic: ${topic}\n\nHighlighted Quote (Verbatim):`,
135+
},
136+
],
137+
model: "gpt-3.5-turbo",
138+
});
139+
140+
const content = completion.data.choices[0].message.content;
141+
142+
console.log(content);
143+
144+
return content.trim();
145+
} catch (err) {
146+
console.log("START ERROR");
147+
console.error(err);
148+
console.error(err.response);
149+
console.error(err.response.data);
150+
console.error(err.response.data.error);
151+
console.error(err.response.data.error.message);
152+
console.error(err.response.data.error.code);
153+
console.error(err.response.data.error.status);
154+
console.error(err.response.data.error.request);
155+
console.log("END ERROR");
156+
throw err;
157+
}
158+
}
159+
160+
107161
const summarizeCluster = async (cluster) => {
108162
try {
109163
const prompt = `Given the following quotes, summarize them into a two-three sentence summary. Return only the summary`;
@@ -213,14 +267,17 @@ const main = async () => {
213267
markdown += `## ${topic}\n\n`;
214268
markdown += `### Summary\n\n${summary}\n\n`;
215269
markdown += `### Quotes\n\n`;
216-
cluster.forEach((quote) => {
217-
markdown += `- ${quote.text}\n`;
218-
});
270+
for (const quote of cluster) {
271+
markdown += `- ${await highlightQuote(quote.text, topic)}\n`;
272+
}
219273
markdown += `\n\n`;
220274
// markdown += `### Follow Up Questions\n\n${followUp}\n\n`;
221275
}
222276

223277
fs.writeFileSync("output.md", markdown);
224278
};
225279

226-
// main();
280+
// if env var DEV exists, run main
281+
if (process.env.DEV) {
282+
main();
283+
}

0 commit comments

Comments
 (0)