XML feed to Twitter
Requirements for create the script:
- A computer with Ruby, or, if you have good hosting, you can SSH in and run Ruby there
- The ability to install a Ruby gem, or maybe it is already installed
- A text editor (Vi is what I used)
- A Twitter account
First things first, let’s go into your shell and see if you have the plugins (gems) you need already installed. Type this in to see if the gems we need are installed:
1 | gem list |
If you do not see “feed-normalizer” and “twitter4r” in the list, do this to install them:
1 | gem install feed-normalizer twitter4r |
It probably will take a while to “Bulk update” the Gem source, that is normal. Make sure to say “Y” (yes) to install of the dependencies when prompted. It will install Gems like simple-rss, hoe, rubyforge, rake, json, etc.
-
Side Note: You can make sure the Gems are installed by using IRB. So, type in “irb” (Windows user will probably have to go to c:\ruby\bin if Ruby is not in your path) and type in:
1 | require 'twitter4r' |
and the result should show “=> true”. If you get a “=> false”, that means it was not installed. I don’t know what could have gone wrong. I haven’t had any problems in OSX, Linux or Windows installing Gems.
Now that we have the Gems installed we need to find a feed. Almost any “normal” feed will work. I used the feed for Torah Portions (http://www.torahportions.org/portions_rss.php).
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/usr/bin/ruby require 'rubygems' require 'feed-normalizer' gem('twitter4r', '>=0.2.0') require('twitter') #grabbing the url, and setting up the Normilzer feed_url = 'http://www.torahportions.org/portions_rss.php' rss = FeedNormalizer::FeedNormalizer.parse open(feed_url) #quit if for some reason there isn't a feed exit unless rss.entries.length > 0 #creating the array of the portion @ary = Array.new #read entries, push to the array called @ary rss.entries.each do |entry| #replacing " Reading" with just a ":" because tweats are limited to 140 char title = entry.title.gsub(' Reading', ':') body = entry.description + "\r" #actually adding to the array @ary.push(title,body) end @ary.delete_at(0) #removing the part that says "This Weeks Portion" @ary.push("...http://short.ie/torah") #adding the link to torahportions.org/this-week.html puts @ary login = 'torahportions' # change this password = 'NOTMYPASSWORDHERE' # change this client = Twitter::Client.new(:login => login, :password => password) #status = client.status(:post, @ary) |