-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon.js
69 lines (62 loc) · 1.64 KB
/
amazon.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
/**
* @file amazon.js
* @author [email protected] <Brett Lempereur>
*
* Get the contents of Amazon Wishlists and find out which of them are
* actualy books.
*/
var async = require('async');
var AWL = require('amazon-wish-list');
var isbn = require('node-isbn');
// Create an Amazon Wishlist client.
var client = new AWL();
/**
* Attempt to resolve an ISBN identifier.
*/
function resolve(identifier, callback) {
isbn.resolve(identifier, function(err, book) {
if (err) {
if (err.message.startsWith("no books found with isbn:")) {
callback(null, null);
} else {
callback(err, null);
}
} else {
callback(null, book);
}
});
}
/**
* Get the list of books contained within an Amazon Wishlist.
*/
function getBooks(identifier, callback) {
client.getById(identifier).then(function(wishlist) {
var ids = wishlist.items.map(function(i) { return i.id; });
async.map(ids, resolve, function (err, results) {
var books = results.filter(function (i) { return i != null; });
callback(err, books);
});
})
.catch(function(reason) {
callback(new Error("Could not get wishlist: " + reason), null);
});
}
/**
* Get the best ISBN number available for a book.
*/
function getIsbn(book) {
var ids = book.industryIdentifiers;
var isbn13 = ids.find(function (id) { return id.type == 'ISBN_13'; });
if (isbn13 !== undefined) {
return isbn13.identifier;
}
var isbn10 = ids.find(function (id) { return id.type == 'ISBN_10'; });
if (isbn10 !== undefined) {
return isbn10.identifier;
}
return undefined;
}
module.exports = {
getBooks: getBooks,
getIsbn: getIsbn
}