Linux - Send STDOUT of command to an RSS Feed

被刻印的时光 ゝ 提交于 2019-12-01 14:53:19

I have a proper solution for you, in command line. That use Perl Template::Toolkit module in background (no need to learn Perl just now) :

first install the package perl-template-toolkit, then create the template file rss.tpl :

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>[% title %]</title>
    <description>[% desc %]</description>
  </channel>
  <!-- rest of the RSS -->
</rss>

And run the command :

tpage --define title=foobar --define desc=description --interpolate rss.tpl

Output is :

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>foobar</title>
    <description>description</description>
  </channel>
  <!-- rest of the RSS -->
</rss>

You will find a complete template to modify here

There is another solution using xmlstarlet:

Create an initial rss feed file feed.xml:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>My RSS Feed</title>
    <description>This is my RSS Feed</description>
  </channel>
</rss>

Create a shell script that uses xmlstarlet to add items:

#!/bin/sh

TITLE="My RSS entry"
LINK="http://example.com/entry4711"
DATE="`date`"
DESC="Good news"
GUID="http://example.com/entry4711" 

xmlstarlet ed -L   -a "//channel" -t elem -n item -v ""  \
     -s "//item[1]" -t elem -n title -v "$TITLE" \
     -s "//item[1]" -t elem -n link -v "$LINK" \
     -s "//item[1]" -t elem -n pubDate -v "$DATE" \
     -s "//item[1]" -t elem -n description -v "$DESC" \
     -s "//item[1]" -t elem -n guid -v "$GUID" \
     -d "//item[position()>10]"  feed.xml ; 

To have a generic solution one would prefer passing the parameters from the commandline of course.

The -d command ensures that the feed does not grow inifinet but has at most 10 items.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!