forked from bryantchan/instapaper-node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
157 lines (130 loc) · 3.44 KB
/
index.coffee
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
request = require('request-promise')
OAuth = require('oauth-1.0a')
crypto = require('crypto')
class Instapaper
baseUrl: 'https://www.instapaper.com/api/1/'
consumer_key: ''
consumer_secret: ''
oauth: null
token: null
constructor: (@consumer_key, @consumer_secret) ->
@oauth = OAuth
consumer:
key: @consumer_key
secret: @consumer_secret
signature_method: 'HMAC-SHA1',
hash_function: (base_string, key) ->
crypto.createHmac('sha1', key).update(base_string).digest('base64')
request: (opts, callback) ->
opts.url = @baseUrl + opts.url
opts.method = 'POST'
if @token
opts.form = @oauth.authorize(opts)
opts.headers = @oauth.toHeader(@oauth.authorize(opts, @token))
else if opts.url.indexOf('access_token') > 0
opts.form = this.oauth.authorize(opts)
else
opts.form = opts.data
return new Promise (resolve, reject) ->
request(opts).then((data) ->
if opts.format is 'raw'
resolve data
else if opts.format is 'qline'
resolve qline2object(data)
else
resolve JSON.parse data
).catch (error) ->
reject error
requestToken: (user, password) ->
@request
format: 'qline'
url: 'oauth/access_token'
data:
x_auth_username: user
x_auth_password: password
x_auth_mode: 'client_auth'
setToken: (token, secret) ->
@token =
key: token
secret: secret
verifyCredentials: ->
@request
url: 'account/verify_credentials'
login: (user, password) ->
vm = @
return new Promise (resolve, reject) ->
vm.requestToken(user, password).then((authData) ->
vm.setToken authData.oauth_token, authData.oauth_token_secret
resolve()
).catch (err) ->
reject err
listBookmarks: (params = {}) ->
@request
url: 'bookmarks/list'
data: params
updateBookmarkProgress: (params = {}) ->
@request
url: 'bookmarks/update_read_progress'
data: params
addBookmark: (params = {}) ->
@request
url: 'bookmarks/add'
data: params
deleteBookmark: (bookmark_id) ->
@request
url: 'bookmarks/delete'
data:
bookmark_id: bookmark_id
starBookmark: (bookmark_id) ->
@request
url: 'bookmarks/star'
data:
bookmark_id: bookmark_id
unstarBookmark: (bookmark_id) ->
@request
url: 'bookmarks/unstar'
data:
bookmark_id: bookmark_id
archiveBookmark: (bookmark_id) ->
@request
url: 'bookmarks/archive'
data:
bookmark_id: bookmark_id
unarchiveBookmark: (bookmark_id) ->
@request
url: 'bookmarks/unarchive'
data:
bookmark_id: bookmark_id
moveBookmark: (bookmark_id, folder_id) ->
@request
url: 'bookmarks/unarchive'
data:
bookmark_id: bookmark_id
folder_id: folder_id
getText: (bookmark_id) ->
@request
format: 'raw'
url: 'bookmarks/get_text'
data:
bookmark_id: bookmark_id
listFolders: ->
@request
url: 'folders/list'
addFolder: (title) ->
@request
url: 'folders/add'
data:
title: title
deleteFolder: (folder_id) ->
@request
url: 'folders/delete'
data:
folder_id: folder_id
qline2object = (query = "") ->
result = {}
parts = query.split("&")
for item in parts
item = item.split("=")
result[item[0]] = item[1]
result
module?.exports = Instapaper