-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (99 loc) · 3.37 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {makeHttpRequest, downloadFile} from './helper.js';
// Attach an event listener to the generate CSV button
document.getElementById('generateCSV').addEventListener('click', async (e) => {
// Grab the ISBN string and remove any hyphens
let isbnString = document.getElementById('isbn').value;
isbnString = isbnString.replace("-","");
// Extract the individual ISBNs
let isbns = [];
let number = /\b\d+\b/g;
let match;
while (match = number.exec(isbnString)) {
isbns.push(match[0]);
}
try {
// Build the CSV Data
const csvData = await getCSVDataFromISBNs(isbns);
// Download the file
downloadFile(`${isbn}.csv`,csvData)
}
catch(e) {
alert("Unable to fetch data. Please double check the ISBN")
}
})
async function getCSVDataFromISBNs(isbns) {
// These are the columns we'll have in our CSV record
const csvFields = [
'isbn',
'title',
'publisher',
'publish_date',
'page_count',
'author_1',
'author_2',
'author_3',
'subject_1',
'subject_2',
'subject_3',
'subject_4',
'subject_5'
]
// Get the book info in JSON format from the Open Library API
const openLibraryData = await getOpenLibraryData(isbns);
console.log(openLibraryData);
// Build out the data rows by iterating through the ISBNs
const csvData = isbns.map(isbn => {
// Grab the isbn's metadata from the open library api data
let metadata = openLibraryData[`ISBN:${isbn}`];
// If there was metadata
if (metadata) {
// Build an exportData object containing the data we want in the CSV field
// The object's properties map to the columns in the CSV record we'll create
const exportData = buildExportData(isbn, metadata);
// build an array with the data by mapping the field names to the exportData object
return csvFields.map(fieldName => exportData[fieldName]);
}
// Otherwise just return an array with the ISBN
return [isbn];
})
// Return the CSV file
return Papa.unparse({
"fields": csvFields,
"data": csvData
});
}
function buildExportData(isbn, metadata) {
let exportData = {};
// ISBN
exportData.isbn = isbn;
// Title
exportData.title = metadata.title;
// Number of pages
exportData.page_count = metadata.pagination;
// Publish Data
exportData.publish_date = metadata.publish_date;
// Publisher
try {
exportData.publisher = metadata.publishers[0].name;
}
catch(e) {}
// Authors (maximum of 3)
for (let i = 0; i < metadata.authors.length && i < 3; i++) {
exportData[`author_${i+1}`] = metadata.authors[i].name;
}
// Subjects (maximum of 5)
for (let i = 0; i < metadata.subjects.length && i < 5; i++) {
exportData[`subject_${i+1}`] = metadata.subjects[i].name;
}
return exportData;
}
async function getOpenLibraryData(isbns) {
const isbnParams = isbns.map(isbn => `ISBN:${isbn}`).join(",")
const response = await makeHttpRequest(`https://openlibrary.org/api/books?bibkeys=${isbnParams}&jscmd=data&format=json`,'GET')
try {
return JSON.parse(response);
}
catch(e) {
throw("Unable to get data")
}
}