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
#!/usr/bin/env ruby

# */15 * * * * cd /path/to/script_dir/ && ruby twitter-retwitter.rb > /dev/null 2>&1

require 'net/http'
require 'rss'
require 'open-uri'

TWITTER_USER = "" # your twitter username
TWITTER_PASS = "" # twitter password
DONE_FILE = "#{ENV["HOME"]}/.retweet_list"
SEPERATOR = "\n--==--==--*--==--==--\n"

seen_replies = []
begin
  seen_replies = open(DONE_FILE).read.split(SEPERATOR)
rescue Errno::ENOENT
end

get_more = true
page = 1

while get_more do
  rss = RSS::Parser.parse(open("http://twitter.com/statuses/replies.rss?page=#{page}", :http_basic_authentication => [TWITTER_USER, TWITTER_PASS]).read)

  # if this page is empty, we're done
  get_more = false if rss.items.empty?

  # loop over each reply
  rss.items.each do |item|
    reply = item.link + "|" + item.description.chomp

    # if we haven't seen it before
    unless seen_replies.include?(reply) or seen_replies.include?(reply + "\n")
      
      # put it in teh seen list
      seen_replies << reply
      
      # fix the tweet to give credit to the author
      tweet = item.description.gsub(/^([^:].+): @#{TWITTER_USER}/, 'from @\1:')
      
      # post to twitter
      puts "Posting #{tweet}"
      Net::HTTP.post_form(URI.parse("http://#{TWITTER_USER}:#{TWITTER_PASS}@twitter.com/statuses/update.json"),
        {'status' => tweet}
      )
    else
      get_more = false
    end
  end
  
  page += 1
end

# write out the file
File.open(DONE_FILE, "w") do |f|
  f.puts seen_replies.join(SEPERATOR)
end