-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-database.js
67 lines (57 loc) · 2.16 KB
/
update-database.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
import fetch from 'node-fetch';
import { parse } from 'arraybuffer-xml-parser';
import { getLines, createFile, readFile, getURL, getYear, shelves } from './utils.js'
const production = true;
const user_profile_id = "104159625";
const rss = `https://www.goodreads.com/review/list_rss/${user_profile_id}?key=&shelf=`;
const encoder = new TextEncoder();
Object.keys(shelves).forEach((shelve, shelveIndex) => {
let url = production ? rss + shelve : `http://0.0.0.0:8000/mocks/${shelve}.xml`;
fetch(url)
.then(response => response.text())
.then(data => {
const xmlData = encoder.encode(data);
const object = parse(xmlData);
let items = object.rss.channel.item;
let newBooks = '';
const fileContents = readFile(shelve);
const storedBooks = getLines(fileContents).map((element) => {
const parts = element.split('|');
return {
book_id: parts[0],
year: parts[1],
title: parts[2],
URL: parts[3],
score: parts[4]
}
});
if (!Array.isArray(items)) {
items = [items];
}
items.forEach((item) => {
const found = storedBooks.findIndex(book => +book.book_id === +item.book_id);
// Bypass if we're on the currently-reading
// We don't consider found, just add them all
if (found === -1 || shelve === 'currently-reading' || shelve === 'to-read') {
const title = item.title;
const URL = getURL(item.description);
const year = shelve === ('currently-reading' || 'to-read') ? '' : getYear(item.pubDate);
const score = Math.floor(item.user_rating);
const book = `${item.book_id}|${year}|${title}|${URL}|${score}`;
newBooks += `\n${book}`;
}
});
if (newBooks !== '') {
let content = fileContents + newBooks;
if (shelve === 'currently-reading' || shelve === 'to-read') {
// Since we delete books from this shelve, we can't simply
// add the new ones. We need to start clean
content = newBooks
}
createFile(
`./public/${shelve}.txt`,
`${content}`
);
}
});
});