-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (106 loc) · 2.89 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
"use strict";
var http = require('http');
/**
* Fetch and parse Hatena bookmark data for the specified hatena user.
*
* @param {String} hatenaId hatena ID
* @param {Function} cb callback
*/
function fetch(hatenaId, cb) {
if (!hatenaId) {
throw new Error('hatena-bookmark-parser: hatenaId parameter is missing.');
}
var Url = 'http://b.hatena.ne.jp/' + hatenaId + '/search.data';
http.get(Url, function(res) {
var body = "";
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var error = null, result;
if (res.statusCode === 302) {
error = new Error('moved temporarily');
} else if (res.statusCode === 403) {
error = new Error('permission denied');
} else if (res.statusCode === 404) {
error = new Error('not found');
} else {
result = parse(body);
}
cb(error, result);
});
}).on('error', function(err) {
cb(err);
});
}
/**
* Parse search.data string into object
*
* @param {String} searchData the string fetched from search.data
* @return {Object} parsed result. the format follows
*
* ```
* [
* {
* "title": "This is page title", // the title of this page
* "comment": "your comment.", // your bookmark comment. this maybe be null if you didn't comment
* "url": "http://www.yahoo.co.jp", // the page url
* "count": 10, // the count of bookmarks of this page
* "date": Tue May 14 2605 23:49:42 GMT+0900 (JST) // the date you bookmarked this page
* }
* ]
* ```
*/
function parse(searchData) {
var result = [];
var bs = searchData.split('\n');
if (bs[bs.length-1] === '') {
bs = bs.slice(0,-1);
}
while (result.length < bs.length) {
var b = bs.slice(0, 3);
result.push({
title: b[0],
comment: b[1],
url: b[2],
tags: extractTags(b[1])
});
bs = bs.slice(3);
}
for (var i = 0; i < bs.length; ++i) {
var elems = bs[i].split('\t');
result[i].count = parseInt(elems[0]);
var d = ((/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/).exec(elems[1])||[]).slice(1);
result[i].date = new Date(d[0], d[1]-1, d[2], d[3], d[4], d[5]);
}
return result;
}
/**
* extract tags from a bookmark comment
*
* @param {String} comment A bookmark comment
* @return {Array} tags
*/
function extractTags(comment) {
var tags = [], tag = '';
var inBrace = false;
for (var i=0; comment[i] !== undefined; ++i) {
var c = comment[i];
if (c !== '[' && !inBrace) continue;
if (c === '[') { inBrace = true; continue; }
if (c === ']' && inBrace) {
inBrace = false;
tags.push(tag);
tag = '';
continue;
}
if (inBrace) { tag += c; }
}
return tags;
}
module.exports = {
fetch: fetch,
parse: parse,
extractTags: extractTags
};