-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfiona-lib.js
executable file
·193 lines (158 loc) · 6.17 KB
/
fiona-lib.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
(function (w) {
/* exported fiona */
'use strict';
var amazon = 'https://www.amazon.com'
, fetchUri = amazon + '/gp/digital/fiona/manage/features/order-history/ajax/'
, deleteUri = amazon + '/gp/digital/fiona/du/fiona-delete.html'
, _ = utils
, LibraryItem = (function () {
var LibraryItem = function (rawData) {
if (arguments.length === 0) {
return;
}
this.title = rawData.title;
this.author = rawData.author;
this.asin = rawData.asin;
this.sid = rawData.sid;
}
, proto = LibraryItem.prototype;
proto.category = '<General Library Item>';
proto.toString = function () {
return "title:" + this.title + ", asin:" + this.asin;
};
// to avoid mass deletes of bought titles, we set this to false for anything but personal docs.
proto.isDeletable = false;
/**
* Delete library item
* @param [options.callback] {Function}
*/
proto.delete = function (callback) {
if(!this.isDeletable) {
callback();
return;
}
var HARD_CODED_SID = "192-2870048-2042810",
body = {
contentName: this.asin,
sid: HARD_CODED_SID,
orderID: this.orderID,
isAjax: 1,
category: this.category
};
doPost(deleteUri, body, callback);
};
return LibraryItem;
}())
, PersonalDoc = (function () {
var PersonalDoc, proto, uber;
PersonalDoc = function () {
LibraryItem.apply(this, arguments);
};
/**
* Fetch personal documents
* @param options.offset default 0
* @param options.count default 15
*/
PersonalDoc.findAll = createFetchFunction('queryPdocs.html', 0, 15);
PersonalDoc.prototype = new LibraryItem;
PersonalDoc.constructor = PersonalDoc;
proto = PersonalDoc.prototype;
uber = LibraryItem.prototype;
proto.category = "kindle_pdoc";
proto.isDeletable = true;
proto.toString = function () {
return "[PersonalDoc] " + uber.toString.call(this);
};
return PersonalDoc;
}())
, Ebook = (function () {
var Ebook, proto, uber;
Ebook = function (itemData) {
LibraryItem.call(this, itemData);
this.orderDate = new Date(itemData.orderDateEpoch);
this.orderID = itemData.orderID;
};
/**
* Fetch books
* @param options.offset default 0
* @param options.count default 15
*/
Ebook.findAll = createFetchFunction('queryOwnership_refactored2.html', 0, 15);
Ebook.prototype = new LibraryItem;
uber = LibraryItem.prototype;
proto = Ebook.prototype;
Ebook.constructor = Ebook;
proto.category = "fiona_ebook";
proto.toString = function () {
return "[Ebook] " + uber.toString.call(this) + ", orderDate: "+this.orderDate;
};
return Ebook;
}())
, ResultSet = (function () {
var ResultSet = function (rawData) {
this.hasMore = rawData.data.hasMore === 1;
this.offset = rawData.data.offset;
this.totalCount = rawData.data.totalCount;
var type = rawData.data.contentType;
this.items = rawData.data.items.map(function (itemData) {
if (type === "Personal Documents") return new PersonalDoc(itemData);
else if (type === "All") return new Ebook(itemData);
else return new LibraryItem(itemData);
});
}
, proto = ResultSet.prototype;
proto.toString = function () {
return "ResultSet: " + this.items.length + "/" + this.offset + " (offset " + this.offset + ")";
};
return ResultSet;
}());
function fetch(options) {
doPost(fetchUri + options.action,
{ offset: options.offset, count: options.count },
function(parsedData) { options.callback(new ResultSet(parsedData)); },
options.errorFunction || function () {
console.error(options.action + ": Fetch of titles failed");
}
);
}
function doPost(uri, params, callback, errorFunc) {
if(!params) throw new Error("Missing parameters");
var body = _.buildParameterString(params)
, r = new XMLHttpRequest();
r.open("POST", uri, true);
r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
r.onerror = errorFunc || function () {
console.error("Failed POST");
};
r.onload = function () {
var body = r.responseText, result = undefined;
if (body) {
try {
result = JSON.parse(body);
}
catch (ex) {
result = ex;
}
}
callback(result);
};
r.send(body);
}
function createFetchFunction(action, defaultOffset, defaultItemsFetched) {
return function (options) {
var opts;
if (!options) opts = {};
else opts = options;
opts.offset = opts.offset || defaultOffset;
opts.count = opts.count || defaultItemsFetched;
opts.action = action;
return fetch(opts);
};
}
w.fiona = {
_ResultSet: ResultSet,
_LibraryItem: LibraryItem,
PersonalDoc: PersonalDoc,
Ebook: Ebook
};
})(window);