Skip to content

Instantly share code, notes, and snippets.

@rajeshg
Created October 29, 2010 20:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajeshg/654320 to your computer and use it in GitHub Desktop.
Save rajeshg/654320 to your computer and use it in GitHub Desktop.
Ruby script to export Twitter favorites to a CSV file
#!/usr/bin/ruby -w
# author: rgollapudi
# description: Script to fetch all your twitter favorites and write them to a csv file
require 'open-uri'
require 'rss/1.0'
require 'rss/2.0'
# For reading rss from a url:
# refer http://benwoodall.com/2010/10/ruby-rss-parser/
#
# Open a file for exporting all favorites from the rss
$i = 1
$csv = File.open('mytwitfavs.csv', 'w')
$username = 'rajeshgollapudi'
#
# gets 20 favorites from twitter rss
#
def getfavorites
url = "http://twitter.com/favorites/#{$username}.rss"
unless $i == 1
url = url+"?page=#{$i}"
end
puts "url : " + url
content = ''
open(url) do |parse|
content = parse.read
end
rss = RSS::Parser.parse(content, false)
return rss
end
def writefavorites(rss)
# Output the feeds title, URL, and number of entries
puts "=================================="
puts "Title: #{rss.channel.title}"
puts "URL: #{rss.channel.link}"
puts "Total Entries: #{rss.items.size}"
puts "=================================="
# Setup to read feed and parse all items
# Change for this: parse top 5 posts, ask if more, (Y/N?), get/end
rss.items.each do |item|
puts "Title: #{item.title}"
puts "Published: #{item.date}"
# puts "#{item.description}"
puts "=================================="
$csv << ["#{item.title}", "#{item.date}\n"]
end
end
#
# => write the fetched rss to a file
#
rss = getfavorites() # => call once to initialize the loop
size = rss.items.size
writefavorites(rss)
while size > 0
$i += 1
rss = getfavorites()
size = rss.items.size
writefavorites(rss)
end
# close the file handle
$csv.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment