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 h3manth.com and is.gd api is used to shorten the url, the other dependences are listed in the comment section of the code.

Python:

#!/usr/bin/env python
# Twitt rss feeds
# rsstwitt.py
# Author Hemanth.HM
# Site : www.h3manth.com
# 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 = "http://is.gd/api.php?longurl=%s"%(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
        else:
            long_url=str(old_feed["items"][0]["link"])
            title=str(old_feed["items"][0]["title"])
            return (title+ " " +self.shrink(long_url))

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

def main():
    """ Edit the url and tuser and tpass as per your requirments """
    feed="http://www.h3manth.com/rss.xml"
    tuser="hemanth_hm"
    tpass="**********"
    twitfeed = TweetFeeds(feed,tuser,tpass)
    twitfeed.twitt()
    
if __name__ == "__main__":
    main()

Ruby:

#!/usr/bin/env ruby
# Author : Hemanth.HM 
# Site : www.h3manth.com
# 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 http://gems.github.com 
# 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 = "http://www.h3manth.com/rss.xml"
        # fetch feed
        feed = Feedzirra::Feed.fetch_and_parse(feed_url)
        entry    = feed.entries.first
        title    = entry.title 
        link     = open('http://is.gd/api.php?longurl='\
        + 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 = Twitter::OAuth.new('consumer token', 'consumer secret')
        oauth.authorize_from_access('access token', 'access secret')
                client = Twitter::Base.new(oauth)
                client.update(status)
    end
end

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

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

Do share your ideas in the comments section below

Share this