RSS feed twitt bots

The open source world has always be a great source of energy to all especially for code monkeys and terminal junkies, not all sharing code, but also happiness to the whole world, indeed with the free as in freedom making life more better.


p>I coded the below two piece of code, one in python and other in ruby to update twitter stats which would be the title and shorten link of the RSS

In the code below, the RSS feed URL considered is of the and api is used to shorten the url, the other dependences are listed in the comment section of the code.


#!/usr/bin/env python
# Twitt rss feeds
# Author Hemanth.HM
# Site :
# Licensed under GNU GPL Version 3

import feedparser
import urllib2
import twitter

class TweetFeeds:
     def __init__(self,feed,tuser,tpass):
        self.feed = feed
        self.tuser = tuser
        self.tpass = tpass

     def get_feed(self):
        """ Get feeds for the first time """
        return feedparser.parse(self.feed)

     def shrink(self,url):
        """ Shrink URL """
        surl = ""%(url)
        return urllib2.urlopen(surl).read()

     def update(self):
        """ Check if there is a new feed """
        old_feed = self.get_feed ()
        """ etag = old_feed.etag or old_feed.modified based on the feed type """
        chk_feed = feedparser.parse(self.feed, modified=old_feed.modified )
        """ chk_feed.status equals 200 implies no new posts """
        if chk_feed.status == 200:
            print "No new posts in the feed to twitt"
            return False
            return (title+ " " +self.shrink(long_url))

     def twitt(self):
        """ Twitt if any new post """
        if (self.update()):
                twit = twitter.Api(username=self.tuser, password=self.tpass)
                if(self.update() != False):
            except Exception, why:
                print str(why)

def main():
    """ Edit the url and tuser and tpass as per your requirments """
    twitfeed = TweetFeeds(feed,tuser,tpass)
if __name__ == "__main__":


#!/usr/bin/env ruby
# Author : Hemanth.HM 
# Site :
# rsstwit.rb
# License: GNU General Public License Version 3
# Requirments :
# Get twitter and feezirra gems 
# sudo apt-get install libcurl4-gnutls-dev 
# sudo apt-get install libxslt-dev libxml2-dev
# gem sources -a 
# sudo gem install pauldix-feedzirra nokogiri twitter 

require "rubygems"
require 'feedzirra'
require 'twitter'
require 'open-uri'

class Rsstwit
# def twitt
    def twitt
        # set feed_url
        feed_url = ""
        # fetch feed
        feed = Feedzirra::Feed.fetch_and_parse(feed_url)
        entry    = feed.entries.first
        title    = entry.title 
        link     = open(''\
        + entry.url , "UserAgent" => "Ruby-Wget").read
                status = title+":"+link
        # Grant access to twitter on website
        # and using request token to generate access token
        # As HTTP `raise_errors': (401): Unauthorized -  (Twitter::Unauthorized)

        oauth ='consumer token', 'consumer secret')
        oauth.authorize_from_access('access token', 'access secret')
                client =

# Create an instance 
feedtw =
# First twitt
# check for update and twit if new simple?
updated_feed = Feedzirra::Feed.update(feed)
if updated_feed.updated?

Set cron job for every one hour:
1 */1 * * * /path/scrpt

Do share your ideas in the comments section below

Share this