joshnesbitt (owner)

Fork Of

gist: 81206 by anonymous

Revisions

  • 7eb809 Wed Mar 18 08:47:53 -0700 2009
gist: 81438 Download_button fork
public
Public Clone URL: git://gist.github.com/81438.git
Embed All Files: show embed
redis tweets.rb #
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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")