πŸ“œ ⬆️ ⬇️

The simplest RSS reader

Preamble


For tracking RSS it is convenient to use the so-called aggregators. Also at present, online aggregators such as Google Reader have gained popularity. But there are cases when their functionality is redundant, for example, if you need to follow a certain tape during a day or several days and there is no desire to clog your aggregators with temporary links. So I needed such a tool.

The tool had to solve the following tasks:
- notify about updates
- the ability to quickly follow the link
- do not store data, sessions, etc.

Here is what happened:
')

And about how - read below.



Console


The first thing I managed to find was rsstail.
rsstail is a console program that allows you to monitor tape updates in real time. This program performs its functions one hundred percent, but as it turned out, switching to the console and looking for the last records is uncomfortable.

Graphic application


This was followed by an attempt to approach the other side - to find the simplest graphic reader.
The result of these searches showed that most of even the simplest readers are full-fledged aggregators and their functionality is too excessive for such a simple task as tracking a single feed.

Combining graphics and console


Let's try to tie some graphic notification module (tray) to rsstail.
It has been tried:
- notify-send (no good, because mouse events are not processed)
- gtrayicon (works as a trigger: on / off, as well as after starting you can not change the tooltip message)
- zenity (there was an attempt to use the --notification key, but this mode is still not fully implemented)

This search is over, the task to get a simple feed-reader was not so simple.

Script!


Well, here we have almost no limits!
I tried to comment out all the code, so it will be easy to understand and redo it for myself.

Put the message icon in the tray

#!/usr/bin/env ruby #scriptname: libs/rsstray.rb require 'gtk2' browser = 'libs/browser.sh' # signal is using for pushing commands to the main thread signal = Queue.new # Create tray icon si = Gtk::StatusIcon.new # Choose icon image si.icon_name = 'application-rss+xml' # Hide tray icon si.visible = false # Bind action mouse button si.signal_connect('activate') { signal.push('open') } # Bind menu mouse button si.signal_connect('popup-menu') { if signal.num_waiting != 0 then signal.push('') end } # Main thread that reads data from pipe Thread.start do # Get line line by line from pipe while line = STDIN.gets # A record isn't ready yet ready=0 # Parse line case line when /^Title: (.*)$/ then title = $1 when /^Link: (.*)$/ then link = $1 when /^Pub.date: (.*)$/ then pubdate = $1; ready=1 end # Read next line if record ins't ready if not (ready == 1) then next end # Send url to STDOUT puts link # Check if all properties are exist if defined? title and defined? link and defined? pubdate message = title + "\n\n" + link + "\n\n" + pubdate else message = "Something wrong has happened..." end # Set tooltip message for tray icon si.set_tooltip(message) # Make tray icon visible si.set_visible(true) # Wait for signal and open browser if signal is 'open' # and go futher if signal isn't 'open' while signal.pop == 'open' pid = fork { exec(browser, link ) } Process.detach(pid) end # Hide tray icon si.set_visible(false) end # Exit if EOF Gtk.main_quit end # Start main GTK loop Gtk.main 


We submit data to the input "tray" script

 #!/bin/bash #scriptname: rssreader.sh # Go to script directory cd $(dirname $0) || exit 1 # Check arguments [ -z "$1" ] && { echo "usage: $0 http://address_of_rss_feed [number_of_initially_show_items]"; exit 0; } [ -z "$2" ] && num=1 || num=$2 # Lunch rsstail and tray icon rsstail -zpl -n $num -u "$1" | libs/rsstray.rb 


We start the browser on an event from a tray script

 #!/bin/bash #scriptname: libs/browser.sh firefox "$1" 2> /dev/null & disown 


How to use



Launch
 rssreader.sh http://habrahabr.ru/rss 5 

Where habrahabr.ru/rss is the address of the tape, and 5 is the number of recent entries that will fall into the reader immediately after launch. If the number is not a number, then the default is 1.

System tray
- To bring to the icon - a brief information about the current record in the tooltip window appears
- β€œaction” button (usually left) - open the current entry in the browser
- Button "context menu" (usually right) - go to the next entry
- If there are no more entries, the icon disappears until new ones appear.

Few comments


- The work of scripts tested in Ubuntu 10.10 + ruby ​​1.8.7
- For the correct operation of the scripts you need to install: ruby, libgtk2-ruby, rsstail
- rsstail by default checks for updates every 15 minutes
- I could not find a decent implementation of fork + detach for ruby ​​1.8 (without installing additional libraries), so I had to use a dirty trick and unlink the browser process from the reading process using bash and & disown.
In ruby ​​1.9, this would not have happened, but it is not yet mainstream.

Afterword



Scripts of a simple RSS reader can be useful not only to a user who does not want to add a feed for some reason to his main reader, but also to a web developer testing an RSS reader on a test site, and as a demonstration of the capabilities of script engines. You can also play around on the other side and submit not RSS, but, for example, messages from Nagios with the help of nagtail at the input.

Source: https://habr.com/ru/post/115536/


All Articles