LoginSignup
7
7

More than 5 years have passed since last update.

Node.jsを使ってrssを整形しサイト上に表示するメモ(ブログエンジンGhostの場合)

Last updated at Posted at 2016-02-05

概要

feedparserを使ってGhostのrssをexpressで動かしている自社サイトにparseしたのでメモ。Node.js v4.2.3 + express 4.x環境です

ちょこっと詰まったポイント

media:contentの中身ってどうとるの…!?のポイント。urlやimgが大体ここに詰められていたりします。

表示するページ側

route/index.js
var express = require('express');
var app = express();
var Feed = require('../common/feed');

router.get('/', function (req, res) {
  Feed.getBlogfeed(function (err, items) {
    res.render('index', {
      title: 'ものづくりをはじめる人が生まれやすい世界をつくる',
      items: items,
    });
  });
});

Ghostのrssフィードの例

rss.
<item>
    <title>
        <![CDATA[ IoT領域をやりますって、どういうこと? ]]>
    </title>
    <description>
        <![CDATA[
        <p><img src="http://pick.paeria.jp/content/images/2016/02/IMG_0661.jpg" alt="alt"></p> <h1 id="iot">IoTの仕事してますって、結局何?</h1> <p>IoTやってます!と言っているのはインターネットの仕事してますと言うくらいに指す領域が広く、何かの申
        ]]>
        <![CDATA[
        請をする時に事業説明をしたりすると「なんかわかんないけどネットのやつね」と括られることが多いです。</p>
        // 中略
        ]]>
    </description>
    <link>
    http://pick.paeria.jp/watasitatinokao-eruiotnituite/
</link>
<guid isPermaLink="false">4dd02062-afdc-4712-8769-e12a9c9aa8f7</guid>
<category>
    <![CDATA[ paeria ]]>
</category>
<category>
    <![CDATA[ IoT ]]>
</category>
<dc:creator>
<![CDATA[ yamaguchi takahiro ]]>
</dc:creator>
<pubDate>Wed, 03 Feb 2016 01:10:00 GMT</pubDate>
<media:content url="http://pick.paeria.jp/content/images/2016/02/IMG_0661-5.jpg" medium="image"/>
<content:encoded>
<![CDATA[
<img src="http://pick.paeria.jp/content/images/2016/02/IMG_0661-5.jpg" alt="IoT領域をやりますって、どういうこと?"><p><img src="http://pick.paeria.jp/content/images/2016/02/IMG_0661.jpg" alt="IoT領域をやりますって、どういうこと?"></p> <h1 id="iot">IoTの仕事してますって、結局何?</h1>
// 中略

]]>
</content:encoded>
</item>

rssフィードの処理側

common/feed.js
var express = require('express');
var app = express();
var Feed = {};
var FeedParser = require('feedparser');
var request = require('request');

Feed.getBlogfeed = function(cb) {
    var options = {};
    var gUrl = 'http://pick.paeria.jp/rss/';
    options = {
      url: 'http://pick.paeria.jp/rss/'
    };      
    request.get(options, function (err, body) {
      if (err) {
        console.log("rss error!")
        cb(null);
      } else {
        console.log("rss ok")
        var req_blog = request(gUrl);
        var feedparser = new FeedParser({});
        var items = [];

        req_blog.on('error', function (error) {
          // errorしたらここ
          return cb(null);
        });
        req_blog.on('response', function (res) {
     // responseを受け取った結果を接続
          this.pipe(feedparser);
        });
        feedparser.on('readable', function (item) {
     // 得られた結果を配列へ詰めていく
          while (item = this.read()) {
            items.push(item);
          }
        });
        feedparser.on('end', function () {
     // コールバックで戻す
          cb(null, items);
        });
      }
    });
};

module.exports = Feed;

テンプレート側(swigでレンダリング)

// items(rssフィード全体)を取得する
{% for i in items %}
<h1><a href="{{i.link}}">{{"title: " + i.title}}</a></h1>
<p>{{"date: " + i.pubdate|date('Y.m.d')}}</p>
 // media:contentのimgとurlをを展開する
  {% for k in i.enclosures %}
  {{"img: " + k.url}}
  <p>
  <img src="{{k.url}}">
  </p>
  {% endfor %}
{% endfor %}

おまけ

rssを吐き出している、下記のblogもよろしくお願いします!
http://pick.paeria.jp/

参照しました

Node.js の Feedparser を使って RSS フィードを JSON 形式で取得しよう
http://phiary.me/node-js-feedparser-rss-json/

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7