-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
188 lines (158 loc) · 5.08 KB
/
app.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
// Generated by CoffeeScript 1.10.0
(function() {
var $, Timeline, _, app, express, jsdom, prettyDate, request, rss2timeline, toEpoch, window, xml2js;
express = require("express");
xml2js = require('xml2js');
_ = require("underscore");
request = require("request");
prettyDate = require("./pretty");
jsdom = require('jsdom');
window = jsdom.jsdom().parentWindow;
$ = require("jquery/dist/jquery")(window);
Timeline = (function() {
function Timeline(feed) {
this.title = feed.channel.title;
this.link = feed.channel.link;
this.description = feed.channel.description;
this.bookmarks = _.map(feed.item, function(item) {
return new Timeline.Bookmark(item);
});
}
return Timeline;
})();
Timeline.Bookmark = (function() {
function Bookmark(item) {
var element, ref;
this.title = item.title;
this.link = item.link;
this.favicon_url = "http://favicon.st-hatena.com/?url=" + this.link;
this.comment = (ref = item.description) != null ? ref : "";
this.count = item['hatena:bookmarkcount'];
this.datetime = item['dc:date'];
this.created_at = prettyDate(item['dc:date']);
this.user = new Timeline.User(item['dc:creator']);
this.permalink = item['@']['rdf:about'];
this.category = item['dc:subject'];
if (item['content:encoded']) {
element = $(item['content:encoded']);
this.description = element.find('p').text();
if (/class="entry-image"/.test(item['content:encoded'])) {
this.thumbnail_url = element.find('.entry-image').attr('src');
}
}
}
return Bookmark;
})();
Timeline.User = (function() {
function User(name) {
this.name = name;
if (this.name != null) {
this.profile_image_url = "http://www.st-hatena.com/users/" + this.name.substr(0, 2) + ("/" + this.name + "/profile.gif");
}
}
return User;
})();
app = module.exports = express.createServer();
app.configure(function() {
app.use(express.bodyParser());
app.use(express.methodOverride());
return app.use(app.router);
});
app.configure("development", function() {
return app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.configure("production", function() {
return app.use(express.errorHandler());
});
rss2timeline = function(url, headers, cb) {
var parser;
parser = new xml2js.Parser(xml2js.defaults["0.1"]);
parser.addListener('end', function(result) {
var timeline;
timeline = new Timeline(result);
return cb(timeline);
});
return request({
method: 'GET',
uri: url,
headers: _.extend(headers, {
'User-Agent': 'HBFav/0.0.1'
}),
timeout: 20 * 1000
}, function(error, response, body) {
var e, error1;
if (error) {
console.log("[error] " + error + ": " + url);
}
if (!error && response.statusCode === 200) {
try {
return parser.parseString(body);
} catch (error1) {
e = error1;
return console.log(e);
}
}
});
};
toEpoch = function(date) {
return parseInt(date.getTime() / 1000.0);
};
app.get("/hotentry", function(req, res) {
var url;
if (req.param('category')) {
url = "http://b.hatena.ne.jp/hotentry/" + (req.param('category')) + ".rss";
} else {
url = "http://b.hatena.ne.jp/hotentry.rss";
}
return rss2timeline(url, {}, function(timeline) {
"callback: " + url;
_(timeline.bookmarks).each(function(bookmark) {
return bookmark.user = new Timeline.User("hatenabookmark");
});
return res.send(timeline);
});
});
app.get("/entrylist", function(req, res) {
var url;
if (req.param('category')) {
url = "http://b.hatena.ne.jp/entrylist/" + (req.param('category')) + ".rss";
} else {
url = "http://b.hatena.ne.jp/entrylist.rss";
}
return rss2timeline(url, {}, function(timeline) {
_(timeline.bookmarks).each(function(bookmark) {
return bookmark.user = new Timeline.User("hatenabookmark");
});
return res.send(timeline);
});
});
app.get("/:id", function(req, res) {
var offset, url;
url = "http://b.hatena.ne.jp/" + req.params.id + "/favorite.rss?with_me=1";
if (req.param('until')) {
url += "&until=" + (req.param('until'));
} else if (offset = req.param('of')) {
url += "&of=" + (req.param('of'));
}
return rss2timeline(url, {
"Cache-Control": "no-cache"
}, function(timeline) {
return res.send(timeline);
});
});
app.get("/:id/bookmark", function(req, res) {
var offset, ref, url;
offset = (ref = req.param('of')) != null ? ref : 0;
url = "http://b.hatena.ne.jp/" + req.params.id + "/rss?of=" + offset;
return rss2timeline(url, {
"Cache-Control": "no-cache"
}, function(timeline) {
return res.send(timeline);
});
});
app.listen(process.env.PORT || 3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
}).call(this);