-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgetBooks.js
More file actions
56 lines (46 loc) · 1.51 KB
/
getBooks.js
File metadata and controls
56 lines (46 loc) · 1.51 KB
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
const fetch = require('node-fetch');
const fs = require('fs');
async function download(url, name) {
const response = await fetch(url);
const buffer = await response.buffer();
fs.writeFile(`./public/covers/${name}.png`, buffer, () =>
console.log('finished downloading!'));
}
async function createBook(bookIsbn) {
const response = await fetch(`https://api.itbook.store/1.0/books/${bookIsbn}`);
const book = await response.json();
const {title, subtitle, isbn13: isbn, desc: abstract, authors: author, publisher, price, pages} = book;
await download(book.image, book.isbn13);
return {
id: isbn,
title,
subtitle,
isbn,
abstract,
author,
publisher,
price,
numPages: +pages,
cover: `http://localhost:4730/covers/${isbn}.png`,
};
}
async function getBooks(page) {
const response = await fetch(`https://api.itbook.store/1.0/search/web&page=${page}`);
const json = await response.json();
return await Promise.all(json.books.map(async (book) => {
return await createBook(book.isbn13);
}));
}
(async function() {
const books = await Promise.all(Array.from({length: 25}, (_, i) => i + 1).map(getBooks));
fs.writeFile('./db.json', JSON.stringify({
books: books.flat(),
}), () => {
console.log('db.json file created!')
// File destination.txt will be created or overwritten by default.
fs.copyFile('./db.json', './db-original.json', (err) => {
if (err) throw err;
console.log('db.json was copied to db-original.json');
});
})
}());