public
Description: twitterslurp - an example application using the Drop.io API (http://api.drop.io)
Homepage: http://api.drop.io
Clone URL: git://github.com/whoisjake/twitterslurp.git
twitterslurp / slurp
100755 101 lines (79 sloc) 2.852 kb
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby
 
require 'rubygems'
require 'optparse'
require 'ostruct'
require 'json'
require 'net/http'
require 'cgi'
 
lib_dir = File.dirname(__FILE__) + '/lib'
$:.unshift lib_dir unless $:.include?(lib_dir)
require 'dropio'
 
class SlurpConfig
  def self.parse(args)
     options = OpenStruct.new
     options.token = ''
 
     opts = OptionParser.new do |opts|
       opts.banner = "Usage: ./slurp [options]"
 
       opts.separator ""
       opts.separator "Specific options:"
 
       opts.on("-d", "--drop DROPNAME") do |drop|
         options.drop = drop
       end
       
       opts.on("-s", "--search QUERY") do |query|
          options.query = query
        end
       
       opts.on("-t", "--token [TOKEN]") do |token|
          options.token = token
       end
 
       opts.separator ""
       opts.separator "Common options:"
 
       opts.on_tail("-h", "--help", "Show this message") do
         puts opts
         exit
       end
       
       opts.on_tail("--version", "Show version") do
         puts OptionParser::Version.join('.')
         exit
       end
     end
 
     opts.parse!(args)
     
     if options.drop.nil? && options.query.nil?
       puts opts
       exit 0
     end
     
     options
  end
end
 
options = SlurpConfig.parse(ARGV)
 
include Dropio
# PASTE YOUR API KEY HERE
Dropio.api_key = "your_api_key_here"
 
GOOD_WORDS = %w[love like awesome rad cool sweet neat clever rocks great fantastic interesting fun exciting nice]
BAD_WORDS = %w[hate stupid dumb slow sucks sucky suck blows idiot]
 
begin
  drop = Drop.find(options.drop, options.token)
  if drop.guests_can_add || drop.admin_token
    puts "Starting to slurp tweets that contain \"#{options.query}\" into #{drop.name}"
    
    loop do
      last_id = 0
      res = Net::HTTP.get URI.parse("http://search.twitter.com/search.json?rpp=50&since_id=#{last_id}&q=#{CGI::escape(options.query)}")
      tweets = JSON.parse(res)['results']
 
      puts "Slurping #{tweets.size} tweets"
      tweets.each do |tweet|
        color = "000000"
        if (tweet["text"].split(" ") & GOOD_WORDS).any?
          color = "008000"
        elsif (tweet["text"].split(" ") & BAD_WORDS).any?
          color = "ff0000"
        end
        a = drop.create_note("Tweet from #{tweet["from_user"]}", "<span style=\"font-size: medium;color: \##{color};\">" + tweet["text"] + "</span><br/><a href=\"http://twitter.com/#{tweet["from_user"]}/status/#{tweet["id"]}\">http://twitter.com/#{tweet["from_user"]}/status/#{tweet["id"]}</a>")
        last_id = tweet["id"] if (tweet["id"] > last_id)
      end
      
      sleep(60*5)
    end
    
  else
    puts "Slurping can't add notes to the Drop #{drop.name}. Please sign in as the admin and set 'Guests can add' or use the admin token/password."
  end
rescue Exception => e
  puts "There was a problem slurping: #{e.message}"
end