Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Forked from anonymous/redis tweets.rb
Created March 18, 2009 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshnesbitt/81438 to your computer and use it in GitHub Desktop.
Save joshnesbitt/81438 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'redis'
require 'rfeedparser'
$redis = Redis.new
class Tweet < Struct.new(:tweet_id, :text, :author, :link)
def self.parse(item)
self.new("tweet/#{item['id']}", item.title, item.author.split.first, item.link)
end
def self.find(id)
$redis[id] || raise(ArgumentError, "Not found")
end
def self.author(name, page = 1)
list_of("authors/#{name}", page)
end
def self.timeline(page = 1)
list_of('timeline', page)
end
def save
$redis[tweet_id] = self
$redis.push_tail('timeline', tweet_id)
$redis.push_tail("authors/#{author}", tweet_id)
end
def to_s
"#{text} - by #{author}"
end
private
def self.list_of(key, page, page_size = 25)
offset_start = (page.abs) * -page_size
offset_end = offset_start + page_size-1
$redis.list_range(key, offset_start, offset_end).collect do |id|
find(id)
end
end
end
feed = FeedParser.parse('http://search.twitter.com/search.atom?q=Shopify')
feed.entries.each do |e|
Tweet.parse(e).save
end
# Output timeline
puts Tweet.timeline.join("\n")
# Output tweets by bbbox
puts Tweet.author('bbbox').join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment