rss.js
2.51 KB
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
/**
* 逛rss订阅
* @author: chenfeng<feng.chen@yoho.cn>
* @date: 2016/09/13
*/
'use strict';
const mRoot = '../models';
const RssModel = require(`${mRoot}/rss`);
const Feed = require('feed');
const _ = require('lodash');
const moment = require('moment');
/**
* [订阅逛最新的资讯]
*/
const index = (req, res, next) => {
let gender = req.query.gender || '1,2,3';
return req.ctx(RssModel).getRssArticle(gender).then((result) => {
if (!result) {
return next();
}
res.setHeader('Content-Type', 'text/xml; charset=utf-8');
let feed = new Feed({
id: 'http://yohobuy.com',
title: '有货逛',
description: 'Yoho!Buy有货 | 年轻人潮流购物中心',
link: 'http://yohobuy.com',
copyright: '2015 yoho.inc',
generator: 'http://m.yohobuy.com',
updated: new Date(),
});
_.forEach(result, item => {
item.url = item.url.indexOf('http') >= 0 ? item.url : 'http://' + item.url;
feed.addItem({
title: item.title,
link: `${item.url}&ref=rss`,
description: item.intro,
author: [{
name: (item.author && item.author.name) || ' '
}],
date: new Date(item.publishTimeLong &&
parseFloat(item.publishTimeLong) || moment(item.publishTime, 'MM月DD日 HH:mm'))
});
});
if (req.params[0] && req.params[0] === '/atom') {
return res.send(feed.atom1());
}
return res.send(feed.rss2());
}).catch(next);
};
const rss = (req, res, next, gmt) => {
let gender = req.query.gender || '1,2,3';
res.setHeader('Content-Type', 'text/xml; charset=utf-8');
return req.ctx(RssModel).getRssArticle(gender).then((result) => {
if (!result) {
return next();
}
_.forEach(result, item => {
item.url = item.url.indexOf('http') >= 0 ? item.url : 'http://' + item.url;
let time = new Date(item.publishTimeLong &&
parseFloat(item.publishTimeLong) || moment(item.publishTime, 'MM月DD日 HH:mm'));
let format = gmt ? 'ddd, MM MMM YYYY hh:mm:ss +0800' : 'YYYY-MM-DD hh:mm:ss +0800';
item.publishTime = moment(time).format(format);
});
return res.render('rss/index', {
layout: false,
items: result
});
}).catch(next);
};
module.exports = {
index,
rss
};