Skip to content

Instantly share code, notes, and snippets.

@tadaken3
Last active October 30, 2018 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadaken3/0d5e3bff58c53c798d89e634efeeaf7c to your computer and use it in GitHub Desktop.
Save tadaken3/0d5e3bff58c53c798d89e634efeeaf7c to your computer and use it in GitHub Desktop.
いくつかのブログのFeedから、最新の情報を取得したい
/*
やりたいこと:
いくつかのブログのFeedから、最新の情報を取得し、JSON形式で返したい
node -v9.5.0
*/
/*求めている結果
[
[{
blogTitle: 'タダケンのEnjoy Tech',
articleTitle: 'PythonでSlackにメッセージを送る方法',
pubdate: 2018-10-26T21:00:00.000Z,
link: 'https://tadaken3.hatenablog.jp/entry/python-slack'
}],
[{
 blogTitle: 'コバヤシ リョウタ',
 articleTitle: 'ロゴを募集したら素敵なロゴをたくさんご提案いただいた話',
 pubdate: 2018-10-25T12:35:15.000Z,
 link: 'https://note.mu/rk_tech/n/na346e6bc5bda'
 }],
 [{
blogTitle: 'ぺんすけブログ',
articleTitle: 'プログラミング学習で分からない時の解決方法',
 pubdate: 2018-10-26T00:55:15.000Z,
 link: 'https://pensuke.work/solve-a-programming-problem/'
 }],
]
*/
/*今、出力される結果
[ { blogTitle: 'タダケンのEnjoy Tech',
articleTitle: 'PythonでSlackにメッセージを送る方法',
pubdate: 2018-10-26T21:00:00.000Z,
link: 'https://tadaken3.hatenablog.jp/entry/python-slack' } ]
[ { blogTitle: 'コバヤシ リョウタ',
articleTitle: 'ロゴを募集したら素敵なロゴをたくさんご提案いただいた話',
pubdate: 2018-10-25T12:35:15.000Z,
link: 'https://note.mu/rk_tech/n/na346e6bc5bda' } ]
[ { blogTitle: 'ぺんすけブログ',
articleTitle: 'プログラミング学習で分からない時の解決方法',
pubdate: 2018-10-26T00:55:15.000Z,
link: 'https://pensuke.work/solve-a-programming-problem/' } ]
*/
*/
cconst FeedParser = require('feedparser');
const request = require('request');
const fetchRSS = (feed) => {
let req = request(feed);
let feedparser = new FeedParser({});
let result;
let feedMeta;
let items = [];
return new Promise(function (resolve, reject) {
req.on('error', function (error) {
return reject(this.error)
});
req.on('response', function (res) {
this.pipe(feedparser);
});
feedparser.on('meta', function(meta) {
feedMeta = meta;
});
feedparser.on('readable', function() {
while(item = this.read()) {
items.push(item);
}
});
feedparser.on('end', function() {
latestArticle = items[0]
result = {
'blogTitle' : feedMeta.title,
'articleTitle' : latestArticle.title,
'pubdate' : latestArticle.pubdate,
'link' : latestArticle.link
}
return resolve([result])
});
})
}
let feeds = [
'https://pensuke.work/rss',
'https://tadaken3.hatenablog.jp/feed',
'https://note.mu/rk_tech/rss'
];
async function asyncMap(array, operation) {
return Promise.all(array.map(async item => await operation(item)))
}
asyncMap(feeds, async feed => {
const response = await fetchRSS(feed)
await console.log(response)
})
@tadaken3
Copy link
Author

array.map()を実行しているのに、104行目のconsole.logの結果に","がないのが、理解に苦しんでいる状況です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment