-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
143 lines (111 loc) · 4.19 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
132
133
134
135
136
137
138
139
140
141
142
143
const request = require('request-promise');
const crypto = require('crypto');
const OAuth = require('oauth-1.0a');
module.exports = class Instapaper {
baseURL = 'https://www.instapaper.com/api/';
user = null;
password = null;
token = null;
authorizing = null;
constructor(consumer_key, consumer_secret) {
this.consumer_key = consumer_key;
this.consumer_secret = consumer_secret;
this.oauth = this.createOAuth();
}
createOAuth = () => {
return OAuth({
consumer: { key: this.consumer_key, secret: this.consumer_secret },
signature_method: 'HMAC-SHA1',
hash_function: (base_string, key) =>
crypto.createHmac('sha1', key).update(base_string).digest('base64'),
});
};
setCredentials(user, password) {
this.user = user;
this.password = password;
}
authorize = () => {
return new Promise((resolve, reject) => {
if (this.token) {
return resolve(this);
}
if (this.authorizing) {
return resolve(this.authorizing);
}
if (!this.user || !this.password) {
return reject('please input valid username and password');
}
const options = this.buildAuthOption('1/oauth/access_token', {
format: 'qline',
data: {
x_auth_username: this.user,
x_auth_password: this.password,
x_auth_mode: 'client_auth',
},
});
this.authorizing = request(options)
.then((data) => {
const token = data.split('&').reduce((acc, current) => {
const [key, val] = current.split('=');
acc[key] = val;
return acc;
}, {});
this.token = {
key: token.oauth_token,
secret: token.oauth_token_secret,
};
this.authorizing = null;
resolve(this);
})
.catch((error) => {
this.authorizing = null;
reject(error);
});
});
};
buildAuthOption = (url, params = {}) => {
const options = {
...params,
method: 'POST',
url: this.baseURL + url,
json: true
};
options.form = this.oauth.authorize(options);
if (this.token) {
options.headers = this.oauth.toHeader(
this.oauth.authorize(options, this.token)
);
}
return options;
};
request = (url, params = {}, version = '1') => {
return this.authorize()
.then(() => this.buildAuthOption(version + url, { data: params }))
.then((options) => request(options));
};
verifyCredentials = () => this.request('/account/verify_credentials');
list = (params = {}) => this.request('/bookmarks/list', params);
updateReadProgress = (params = {}) => this.request('/bookmarks/update_read_progress', params);
add = (params = {}) => this.request('/bookmarks/add', params);
delete = (bookmarkId) => this.request('/bookmarks/delete', {bookmark_id: bookmarkId});
star = (bookmarkId) => this.request('/bookmarks/star', {bookmark_id: bookmarkId});
unstar = (bookmarkId) => this.request('/bookmarks/unstar', {bookmark_id: bookmarkId});
archive = (bookmarkId) => this.request('/bookmarks/archive', {bookmark_id: bookmarkId});
unArchive = (bookmarkId) => this.request('/bookmarks/unArchive', {bookmark_id: bookmarkId});
getText = (bookmarkId) => this.request('/bookmarks/get_text', {bookmark_id: bookmarkId});
move = (bookmarkId, folderId) => this.request('/bookmarks/move', {bookmark_id: bookmarkId, folder_id: folderId});
listFolders = (params = {}) => this.request('/folders/list');
addFolder = (title) => this.request('/folders/add', {title});
deleteFolder = (folderId) => this.request('/folders/delete', {folder_id: folderId});
listHighlights = (bookmarkId) => this.request(`/bookmarks/${bookmarkId}/highlights`, {}, '1.1')
/**
* Create a new highlight for <bookmark-id>
* @param {*} bookmarkId
* @param {*} params
* text: The text for the highlight
* position: Optional. The 0-indexed position of text in the content. Defaults to 0.
* @returns
*/
addHighlight = (bookmarkId, params = {}) => this.request(`/bookmarks/${bookmarkId}/highlight`, params, '1.1')
deleteHighlight = (highlightId) => this.request(`/highlights/${highlightId}/delete`, {}, '1.1')
};