Skip to content

Commit fe4693c

Browse files
committed
Move parseLink and getFacebookStats to /util, remove request dependency
1 parent 6b3715d commit fe4693c

File tree

11 files changed

+131
-83
lines changed

11 files changed

+131
-83
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ LiveJournal.jsonrpc.request('comment.get_thread', {
9696

9797
You can access method list using `LiveJournal.jsonrpc.methods`.
9898

99+
## API
100+
101+
LiveJournal.API is wrapper around some resources provided at http://www.livejournal.com/bots/
102+
99103
## Other docs and resources
100104

101105
* http://www.livejournal.com/developer/
102106
* http://lj-dev.livejournal.com/
107+
* http://www.livejournal.com/bots/
103108

104109
## Tests
105110

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
'use strict';
2+
13
module.exports = {
2-
utils: require('./lib/utils'),
3-
4+
utils: {
5+
parseLink: require('./lib/utils/parseLink'),
6+
getFacebookStats: require('./lib/utils/getFacebookStats')
7+
},
8+
49
API: require('./lib/api'),
510

611
xmlrpc: require('./lib/xmlrpc'),
7-
12+
813
jsonrpc: require('./lib/jsonrpc')
914
};

lib/api.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
var request = require('request');
1+
'use strict';
2+
3+
var request = require('superagent');
24

35
var BASE = 'http://www.livejournal.com/';
46

57
function getFriends(params, callback) {
6-
request({
7-
url: BASE + '/misc/fdata.bml?user=' + params.journal
8-
}, function(err, res, body) {
8+
request.get(BASE + '/misc/fdata.bml').query({
9+
user: params.journal
10+
}).end(function(err, res) {
911
if (err) {
1012
return callback(err, null);
1113
}
1214

15+
var body = res.text;
16+
1317
if (body.charAt(0) === '!') {
1418
return callback({ message: body.trim() }, null);
1519
}

lib/utils.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

lib/utils/getFacebookStats.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
var request = require('superagent');
4+
5+
function _groupBy(arr, amount) {
6+
var result = [], offset = 0;
7+
8+
do {
9+
result.push(arr.slice(offset, offset + amount));
10+
offset += amount;
11+
} while (offset < arr.length);
12+
13+
return result;
14+
}
15+
16+
function getFacebookStats(urls, callback) {
17+
var groups = _groupBy(urls, 10),
18+
result = {},
19+
done = 0;
20+
21+
groups.forEach(function(group) {
22+
request('http://graph.facebook.com/').query({
23+
ids: group.join(',')
24+
}).end(function(err, res) {
25+
if (!err && res.statusCode == 200) {
26+
var body = JSON.parse(res.text);
27+
28+
for (var key in body) {
29+
result[key] = body[key].shares || 0;
30+
}
31+
32+
done++;
33+
if (done >= groups.length) {
34+
callback(null, result);
35+
}
36+
} else {
37+
callback(err);
38+
}
39+
});
40+
});
41+
}
42+
43+
module.exports = getFacebookStats;

lib/utils/parseLink.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
var rxLink = [
4+
/([0-9a-zA-Z-_]+)\.livejournal\.com\/([0-9]+)\.html/,
5+
/m\.livejournal\.com\/read\/[a-z]+\/([0-9a-zA-Z_-]+)\/(\d+)/,
6+
/users\.livejournal\.com\/([0-9a-zA-Z-_]+)\/([0-9]+).html/
7+
];
8+
9+
function parseLink(url) {
10+
if (!url) {
11+
return null;
12+
}
13+
14+
for (var i = 0, match; i < rxLink.length; i++) {
15+
match = url.match(rxLink[i]);
16+
17+
if (match && match.length === 3) {
18+
return { journal: match[1], postId: match[2] };
19+
}
20+
}
21+
22+
return null;
23+
}
24+
25+
module.exports = parseLink;

lib/xmlrpc.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ var LJ = xmlrpc.createClient({
1515
port: 80
1616
});
1717

18-
LJ.Deserializer.prototype.endBase64 = function(data) {
19-
var buffer = new Buffer(data, 'base64');
20-
this.push(String(buffer));
21-
this.value = false;
22-
};
23-
2418
var methods = [
2519
'checkfriends',
2620
'consolecommand',

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
"url": "git://github.com/agentcooper/node-livejournal.git"
1919
},
2020
"dependencies": {
21-
"request": ">=2.25",
2221
"superagent": "^1.2.0",
23-
"xmlrpc": "git://github.com/agentcooper/node-xmlrpc.git"
22+
"xmlrpc": "^1.3.1"
2423
},
2524
"main": "index",
2625
"devDependencies": {

spec/apiSpec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var LiveJournal = require('../');
2+
3+
describe('LiveJournal.API', function() {
4+
describe('getFriends', function() {
5+
it('should provide friends and friendOf', function(done) {
6+
LiveJournal.API.getFriends({
7+
journal: 'ljreader-app'
8+
}, function(err, res) {
9+
expect(res.friends).toBeDefined();
10+
expect(res.friendOf).toBeDefined();
11+
12+
expect(res.friends.length > 0).toBe(true);
13+
expect(res.friendOf.length > 0).toBe(true);
14+
done();
15+
});
16+
}, 10000);
17+
});
18+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var LiveJournal = require('../../');
2+
3+
describe('LiveJournal.utils', function() {
4+
describe('getFacebookStats', function() {
5+
it('should get share stats', function(done) {
6+
var urls = [
7+
'http://brad.livejournal.com/2409049.html',
8+
'http://brad.livejournal.com/2410009.html',
9+
'http://brad.livejournal.com/29215.html',
10+
'http://brad.livejournal.com/badlink'
11+
];
12+
13+
LiveJournal.utils.getFacebookStats(urls, function(err, res) {
14+
urls.forEach(function(url) {
15+
expect(res[url]).toBeDefined();
16+
expect(typeof res[url] === 'number').toBe(true);
17+
});
18+
done();
19+
});
20+
}, 10000);
21+
});
22+
});

0 commit comments

Comments
 (0)