phi

I'm a Game Programmer and Frontend Engineer passionate about programming education. Math / C / C++ / C# / JavaScript / HTML5 / CSS3 / Python

phiaryjust a creator

Node.js の Feedparser を使って RSS フィードを JSON 形式で取得しよう

9 years ago

このブログのエントリーをランダムでピックアップしてツイートする ボット( phiary_jp )を作ってまして, そこでちょっと使ったのでまとめてみました.

Feedparser とは?

Feedparser とは, RSS や Atom といったフィードを json 形式にパースすることができる Node.js のモジュールです.

Feedparser をインストールしよう

npm 経由でインストールします.

$npm install feedparser

ついでにサンプルで使う request もインストールしておきましょう.

$npm install request

Feedparser の使い方

ちょっとしたサンプルを作ってみました. 下記のような形で使います.

このブログの rss を取得してタイトル一覧を表示しています.

feed.js

/*
 * feed.js
 */

var FeedParser = require('feedparser');  
var request = require('request');  
var feed = 'http://phiary.me/rss';

var req = request(feed);  
var feedparser = new FeedParser({});

var items = [];

req.on('response', function (res) {  
  this.pipe(feedparser);
});

feedparser.on('meta', function(meta) {  
  console.log('==== %s ====', meta.title);
});

feedparser.on('readable', function() {  
  while(item = this.read()) {
    // console.log(item);
    items.push(item);
  }
});

feedparser.on('end', function() {  
  // show titles
  items.forEach(function(item) {
    console.log('- [' + item.title + ']' + '(' + item.link + ')');
  });
});

Feedparser のサンプルを実行してみよう

下記のコマンドで実行できます.

$ node feed.js

すると下記のように本ブログのタイトル一覧が表示されるのがわかるかと思います.

$ node feed.js 
==== phiary ====
- [Ghost で検索機能(Search)を実装しよう! #荒業](http://phiary.me/ghost-search/)
- [CSS3 だけで角丸バルーンを作る方法](http://phiary.me/css3-rounded-corners-balloon/)
- [簡単にハッカー気分が味わえる『GeekTyper』](http://phiary.me/geektyper/)
- [CSS で textarea に罫線を入れてノートっぽくしてみよう](http://phiary.me/css-textarea-note/)
- [OnsenUI と Google AJAX Feed API で Blog Reader 作ってみた](http://phiary.me/onsen-ui-google-ajax-feed-api-blog-reader/)
- [Onsen UI のススメ](http://phiary.me/onsenui-nosusume/)
- [Google AJAX Feed API を使ってみた](http://phiary.me/google-ajax-feed-api-usage/)
- [base64 だけでWebページを作る方法](http://phiary.me/base64-only-web-page/)
- [JavaScript で iframe の中身を動的に書き換える方法](http://phiary.me/javascript-iframe-dynamic-update/)
- [YAML 入門](http://phiary.me/yaml-introduction/)
- [『js-yaml』 を使って Browser 上で YAML を JSON に変換する方法](http://phiary.me/js-yaml-browser-json/)
- [Git で origin の url を調べる方法](http://phiary.me/git-origin-url/)
- [Ghost Theme でページの判定をする方法](http://phiary.me/ghost-theme-judged-context/)
- [Ghost を Heroku にデプロイする手順](http://phiary.me/ghost-heroku-deploy/)
- [JavaScript で小数点以下の桁数を揃える方法](http://phiary.me/javascript-decimal/)

取得できるプロパティ一覧

meta

  • title
  • description
  • link (website link)
  • xmlurl (the canonical link to the feed, as specified by the feed)
  • date (most recent update)
  • pubdate (original published date)
  • author
  • language
  • image (an Object containing url and title properties)
  • favicon (a link to the favicon -- only provided by Atom feeds)
  • copyright
  • generator
  • categories (an Array of Strings)

article

  • title
  • description (frequently, the full article content)
  • summary (frequently, an excerpt of the article content)
  • link
  • origlink (when FeedBurner or Pheedo puts a special tracking url in the link property, origlink contains the original link)
  • permalink (when an RSS feed has a guid field and the isPermalink attribute is not set to false, permalink contains the value of guid)
  • date (most recent update)
  • pubdate (original published date)
  • author
  • guid (a unique identifier for the article)
  • comments (a link to the article's comments section)
  • image (an Object containing url and title properties)
  • categories (an Array of Strings)
  • source (an Object containing url and title properties pointing to the original source for an article; see the RSS Spec for an explanation of this element)
  • enclosures (an Array of Objects, each representing a podcast or other enclosure and having a url property and possibly type and length properties)
  • meta (an Object containing all the feed meta properties; especially handy when using the EventEmitter interface to listen to article emissions)

Gist でも作ってみた

一応 Gist にもファイルを用意したので, よかったらこちらからダウンロードして動かしてみてください♪

https://gist.github.com/phi-jp/950a4a3ed9afc1b5fccc

Reference