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