forked from tobiaswright/pocket-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (90 loc) · 2.75 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
var request = require('request');
var config = {
pocketUrl: {
request: 'https://getpocket.com/v3/oauth/request',
authorize: 'https://getpocket.com/v3/oauth/authorize',
get: 'https://getpocket.com/v3/get',
add: 'https://getpocket.com/v3/add',
modify: 'https://getpocket.com/v3/send'
},
headers: {
'content-type': 'application/x-www-form-urlencoded',
'X-Accept': 'application/json'
}
};
var getRequestToken = function (key, callback) {
var options = {
headers: config.headers,
body: 'consumer_key=' + key + '&redirect_uri=pocketapp1234:authorizationFinished',
url: config.pocketUrl.request
};
request.post(options, function (error, response, body) {
callback(JSON.parse(body));
});
};
var getAccessToken = function (key, requestToken, callback) {
var options = {
headers: config.headers,
url: config.pocketUrl.authorize,
body: 'consumer_key=' + key + '&code=' + requestToken + '&redirect_uri=pocketapp1234:authorizationFinished'
};
request.post(options, function (error, response, body) {
callback(JSON.parse(body));
});
};
var getArticles = function (key, accessToken, params, callback) {
if (!callback) {
callback = params;
params = {};
}
var options = {
headers: config.headers,
url: config.pocketUrl.get,
body: 'consumer_key=' + key + '&access_token=' + accessToken
};
for (var prop in params) {
// skip loop if the property is from prototype
if (!params.hasOwnProperty(prop)) continue;
options.body += '&' + prop + '=' + params[prop];
}
request.post(options, function (error, response, body) {
completePost(error, response, body, callback);
});
};
var addArticles = function (addurl, key, accessToken, callback) {
var options = {
headers: config.headers,
url: config.pocketUrl.add,
body: 'url=' + addurl + '&consumer_key=' + key + '&access_token=' + accessToken
};
request.post(options, function (error, response, body) {
completePost(error, response, body, callback);
});
};
var modifyArticles = function (actions, key, accessToken, callback) {
actions = JSON.stringify(actions);
var options = {
headers: config.headers,
url: config.pocketUrl.modify,
body: 'actions=' + encodeURIComponent(actions) + '&consumer_key=' + key + '&access_token=' + accessToken
};
request.post(options, function (error, response, body) {
completePost(error, response, body, callback);
});
};
function completePost(error, response, body, callback) {
if (error) {
callback(error, null);
} else if (response.headers.hasOwnProperty('x-error')) {
callback(response.headers['x-error'], null);
} else {
callback(null, JSON.parse(body));
}
}
module.exports = {
getRequestToken: getRequestToken,
getAccessToken: getAccessToken,
getArticles: getArticles,
addArticles: addArticles,
modifyArticles: modifyArticles
};