-
Notifications
You must be signed in to change notification settings - Fork 3
/
flattr.js
580 lines (488 loc) · 12.6 KB
/
flattr.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
var
https = require('https'),
querystring = require('querystring');
// Flattr HTTPS URIs
//
var
options = {
host: 'api.flattr.com',
endpoint: '/rest/v2'
},
o = options;
module.exports.flattrs = new Flattrs();
module.exports.things = new Things();
module.exports.users = new Users();
// Get authentication url
//
// client_id
// scope - Separate several scopes with spaces: "flattr thing email extendedread"
// [res_type] - Response type, code (default) or token
//
exports.auth_url = function (client_id, scope, res_type) {
var res_type = typeof res_type === 'undefined' ? 'code' : res_type;
return url = 'https://flattr.com/oauth/authorize?response_type='+res_type+'&client_id='+client_id+'&scope='+encodeURIComponent(scope);
}
// Request an access token for authorized requests
//
// app - object containing application data
// client_id
// client_secret
// redirect_uri
// code - string in ?code=
// callback - callback function
//
exports.request_token = function (app, code, callback) {
var
basic_auth = new Buffer(app.client_id+':'+app.client_secret).toString('base64'),
httpsopts = {
hostname: 'flattr.com',
path: '/oauth/token',
method: 'POST',
headers: {
"Authorization": 'Basic '+basic_auth,
}
},
reqdata = {
"code": code,
"grant_type": "authorization_code",
"redirect_uri": checkurl(app.redirect_uri)
};
make_request(httpsopts, reqdata, function (data) {
callback(data.access_token);
});
};
// http://developers.flattr.net/api/resources/flattrs/
function Flattrs () {
var self = this;
// List a users flattrs
//
// user - user name
// params (optional) - param object:
// count - Number of records to receive
// page - Page of results to retreive
// callback - callback function
//
self.list = function (user, params, callback) {
var count = '', page = '';
if (typeof params === 'object') {
count = params.count;
page = params.page;
}
else if (typeof params === 'function')
callback = params;
var httpsopts = {
hostname: o.host,
path: o.endpoint +'/users/'+user+'/flattrs?count='+count+'&page='+page,
method: 'GET'
};
make_request(httpsopts, function (data) {
callback(data);
});
};
// List the authenticated users flattrs
//
// token - access token
// params (optional) - param object:
// count - Number of records to receive
// page - Page of results to retreive
// callback - callback function
//
self.list_auth = function (token, params, callback) {
var count = '', page = '';
if (typeof params === 'object') {
count = params.count;
page = params.page;
}
else if (typeof params === 'function')
callback = params;
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/user/flattrs?count='+count+'&page='+page,
method: 'GET',
headers: {
"Authorization": "Bearer "+token
}
};
make_request(httpsopts, function (data) {
callback(data);
});
};
// Flattr a thing
//
// token - access token
// id - id of thing to flattr
// callback (optional) - callback function
//
self.thing = function (token, id, callback) {
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/things/'+id+'/flattr',
method: 'POST',
headers: {
"Authorization": "Bearer "+token
}
};
make_request(httpsopts, {}, function (data) {
callback(data);
});
};
//
// Flattr an URL
// Params is also available if you auto-submit an url.
//
// token - access token
// url - URL of the thing
// [user] (optional) - user owning the thing
// [params] (optional) -
// title - Title of your your thing.
// description - Description for your thing.
// language - Language of your page, use one of the available
// languages.
// tags - Tags you want your post to be tagged with. Multiple
// tags are separated with ,
// hidden - If you want to hide the things from public listings
// on flattr.com set hidden to 1.
// category - Category from the list of categories found here.
//
// callback - callback function
//
self.url = function (token, url) {
var query_str = '';
var
user = '',
params = {},
callback = arguments[arguments.length-1];
if (typeof arguments[2] === 'string') {
user = arguments[2];
if (typeof arguments[3] === 'object')
params = arguments[3];
}
if (typeof arguments[2] === 'object')
params = arguments[2];
for (key in params)
query_str += '&'+key+'='+encodeURIComponent(params[key]);
var
httpsopts = {
hostname: o.host,
path: o.endpoint+'/flattr',
method: 'POST',
headers: {
"Authorization": "Bearer "+token
}
},
data = {
"url": 'http://flattr.com/submit/auto?user_id='+user+'&url='+
encodeURIComponent(checkurl(url))+query_str
};
make_request(httpsopts, data, function (resp) {
callback(resp);
});
}
}
function Things () {
var self = this;
// List a users things
//
// Parameters
// user - user name
// params (optional) - param object:
// count - Number of records to retrieve
// page - Page of results to retrieve
// callback - callback function
//
self.list = function (user, params, callback) {
var count = '', page = '';
if (typeof params === 'object') {
count = params.count;
page = params.page;
}
else if (typeof params === 'function')
callback = params;
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/users/'+user+'/things?count='+count+'&page='+page,
method: 'GET'
};
make_request(httpsopts, function (data) {
callback(data)
});
};
// List the authenticated users things
//
// token - access token
// params (optional) - param object:
// count - Number of records to retrieve
// page - Page of results to retrieve
// callback - callback function
//
self.list_auth = function (token, params, callback) {
var count = '', page = '';
if (typeof params === 'object') {
count = params.count;
page = params.page;
}
else if (typeof params === 'function')
callback = params;
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/user/things?count='+count+'&page='+page,
method: 'GET',
headers: {
"Authorization": "Bearer "+token
}
};
make_request(httpsopts, function (data) {
callback(data)
});
};
// Get a thing
//
// Arguments
// id - id of thing or array of ids
// token - optional access token
// callback - callback function
//
self.get = function (id, token, callback) {
var ids = '';
if (typeof id === 'number')
ids = id;
// Array:
else {
ids = id[0];
for (i=1; i<id.length; i++)
ids += ','+id[i];
}
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/things/'+ids,
method: 'GET'
};
if (typeof token === 'string')
httpsopts['headers'] = { "Authorization": "Bearer "+token }
else if (typeof token === 'function')
callback = token;
make_request(httpsopts, function (data) {
callback(data)
});
};
// Check if a thing exists
//
// url - The url to lookup
// autosubmit - set to true to check an auto submit url
// callback - callback function
//
self.exists = function (url, autosubmit, callback) {
var query = '';
if (autosubmit === true) {
query = 'http://flattr.com/submit/auto?url='+
encodeURIComponent(checkurl(url));
}
else if (typeof autosubmit === 'function') {
query = checkurl(url);
callback = autosubmit;
}
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/things/lookup?url='+query,
method: 'GET'
};
make_request(httpsopts, function (data) {
callback(data)
});
};
// Create a thing
//
// token - access_token
// url - String url to submit
// params (optional) -
// title - Title of your your thing.
// description - Description for your thing.
// language - Language of your page, use one of the available
// languages.
// tags - Tags you want your post to be tagged with. Multiple
// tags are separated with ,
// hidden - If you want to hide the things from public listings
// on flattr.com set hidden to 1.
// category - Category from the list of categories found here.
//
// callback - callback function
//
self.create = function (token, url, params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}
params["url"] = checkurl(url);
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/things',
method: 'POST',
headers: {
"Authorization": "Bearer "+token
}
};
make_request(httpsopts, params, function (data) {
callback(data)
});
};
// Update a thing
//
// token - access_token
// id - id of string
// params (optional) -
// title - Title of your your thing.
// description - Description for your thing.
// language - Language of your page, use one of the available
// languages.
// tags - Tags you want your post to be tagged with. Multiple
// tags are separated with ,
// hidden - If you want to hide the things from public listings
// on flattr.com set hidden to 1.
// category - Category from the list of categories found here.
//
// callback - callback function
//
self.update = function (token, id, params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}
// Flattr workaround for the PATCH request. See docs.
params['_method'] = 'patch';
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/things/'+id,
method: 'PATCH',
headers: {
"Authorization": "Bearer "+token
}
};
make_request(httpsopts, params, function (data) {
callback(data)
});
};
// Deletes a thing
//
// token - access token
// id - id of thing to delete
// callback - callback function
//
self.del = function (token, id, callback) {
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/things/'+id,
method: 'DELETE',
headers: {
"Authorization": "Bearer "+token
}
};
make_request(httpsopts, {}, function (data) {
console.log('DATA', data);
callback(data.error_description ? data : { message: 'ok' });
});
};
// Search things
//
// params - object containing search params
// query (Optional) - string Free text search string
// tags (Optional) - string Filter by tags, separate with ,
// language (Optional) - string Filter by language
// category (Optional) - string Filter by category
// user (Optional) - string Filter by username
// page (Optional) - integer The result page to show
// count (Optional) - integer Number of items per page
//
// callback - callback function
//
self.search = function (params, callback) {
var query_str = '';
for (key in params)
query_str += key+'='+encodeURIComponent(params[key])+'&';
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/things/search?'+query_str,
method: 'GET',
};
make_request(httpsopts, function (data, headers) {
callback(data, headers)
});
};
}
function Users () {
var self = this;
// Get a user
//
// user - user name
// callback - callback function
//
self.get = function (user, callback) {
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/users/'+user,
method: 'GET'
};
make_request(httpsopts, function (data) {
callback(data)
});
};
// Get the authenticated user
//
// token - access token
// callback - callback function
//
self.get_auth = function (token, callback) {
var httpsopts = {
hostname: o.host,
path: o.endpoint+'/user',
method: 'GET',
headers: {
"Authorization": "Bearer "+token
}
};
make_request(httpsopts, function (data) {
callback(data)
});
};
}
// Https request helper function
//
// Arguments
// httpsopts - options for the https request
// reqdata (optional) - data to be sent to server
// callback - callback function
//
function make_request (httpsopts) {
var
reqdata = (typeof arguments[1] === 'object') ? JSON.stringify(arguments[1]) : '',
callback = arguments[arguments.length-1];
if (reqdata) {
if (!httpsopts.headers)
httpsopts["headers"] = {};
httpsopts.headers["Content-Length"] = reqdata.length;
httpsopts.headers["Content-Type"] = 'application/json';
}
var req = https.request(httpsopts, function (res) {
var data = '';
res.setEncoding('utf-8');
res.on('data', function (buffer) {
data += buffer;
});
res.on('end', function () {
//console.log(data);
try {
data = JSON.parse(data);
callback(data, res.headers);
}
catch (e) {
callback({}, res.headers);
}
});
});
req.on('error', function (e) {
console.log(e);
});
req.end(reqdata);
}
function checkurl (url) {
return /^(http|https):\/\//.test(url) ? url : 'http://'+url;
}