平时爱看知乎日报,写了个最简单的 知乎日报 web版,放在了JAE
2014.05.13更新:更新界面,完善功能,新版本放在了 http://izhihu.jd-app.com (仅在chrome下测试过样式),原版还在 这里 2014.04.13更新:增加了 前一天 功能,移动设备界面适配,采用巧妙的方式规避了时间格式化问题(取消注释即可下载所有历史文件)
代码如下:
var http = require('http');
var fs = require('fs');
var date = '';
function fetchBefore(beforeDate) {
http.get("http://news.at.zhihu.com/api/1.2/news/before/" + beforeDate, function (res) {
if (res.statusCode != 200) return;
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
var year = beforeDate.substr(0, 4);
if (!fs.existsSync(year)) fs.mkdirSync(year);
fs.writeFile(year + '/' + beforeDate + '.json', data);
//if (beforeDate != '20130520') fetchBefore(JSON.parse(data).date); //fetch all the history stories
});
});
}
(function fetchLatest() {
http.get("http://news.at.zhihu.com/api/1.2/news/latest", function (res) {
if (res.statusCode != 200) return;
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
var json = JSON.parse(data);
if (date != json.date) {
date = json.date;
fetchBefore(date);
}
fs.writeFile('latest.json', data);
});
});
setTimeout(fetchLatest, 10 * 60 * 1000);
})();
http.createServer(function (request, response) {
var url = request.url;
if (url == '/favicon.ico') return;
var file = 'latest.json';
if (url.length == 16) {
var path = url.substr(8, 4) + '/' + url.substr(8, 8) + '.json';
if (fs.existsSync(path)) file = path;
}
var data = fs.readFileSync(file);
var json = JSON.parse(data);
response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
response.write('<!doctype html><html><head><title>知乎日报</title>');
response.write('<meta name="viewport" content="width=device-width,user-scalable=no" />');
response.write('<style>a{color:#333;text-decoration:none;}body{max-width:600px;margin:10px auto;padding:10px;}</style></head><body>');
response.write('<h1><a href="/">知乎日报</a> - ' + json.display_date + '</h1>');
for (var i = 0; i < json.news.length; i++) {
response.write('<h3><a href="' + json.news[i].share_url + '" target="_blank">' + json.news[i].title + '</a></h3>');
}
response.write('<h3><a href="/before/' + json.date + '"><<< 前一天</a></h3>');
response.write('</body></html>');
response.end();
}).listen(process.env.PORT || 1337, null);
最初版本
var http = require('http');
var fs = require('fs');
var fetchAt=null;
function writeData(data, response) {
var json = JSON.parse(data);
response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
var title = '知乎日报[' + json.date + ']';
response.write('<style>a{color:#333;text-decoration:none;}body{width:600px;margin:30px auto 30px;}</style>');
response.write('<title>' + title + '</title>');
response.write('<body>');
response.write('<h1>' + title + '</h1>');
for (var i = 0; i < json.news.length; i++) {
response.write('<h3><a href="' + json.news[i].share_url + '" target="_blank">' + json.news[i].title + '</a></h3>');
}
response.write('</body>');
response.end();
}
function fetchData(response) {
http.get("http://news.at.zhihu.com/api/1.2/news/latest", function (res) {
if (res.statusCode != 200) return;
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
fs.writeFile('latest.json', data);
fetchAt = new Date();
writeData(data, response);
});
}).on('error', function (e) {
response.write("Error: " + e.message);
});
}
http.createServer(function (request, response) {
if(request.url == '/favicon.ico') return;
if(fetchAt==null||(new Date()-fetchAt)/1000/60>10){
fetchData(response);
return;
}
var data=fs.readFileSync('latest.json');
writeData(data, response);
}).listen(process.env.PORT || 1337, null);
30 回复