-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathretweet.rb
executable file
·46 lines (36 loc) · 992 Bytes
/
retweet.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
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'json'
require 'net/http'
TWITTER_USER = ""
TWITTER_PASS = ""
LAST_TWEET_FILE = ".last_tweet"
def last_tweet
@last_tweet ||= open(LAST_TWEET_FILE).read rescue 0
end
def url
"http://twitter.com/direct_messages.json?since_id=#{last_tweet}"
end
def dm_from_twitter
open(url, :http_basic_authentication => [TWITTER_USER, TWITTER_PASS]).read
end
def direct_messages
@direct_messages ||= JSON.parse(dm_from_twitter)
end
def update_last_tweet(tweet_id)
File.open(LAST_TWEET_FILE, "w") do |f|
f.puts tweet_id
end
end
def send_update(tweet)
Net::HTTP.post_form(URI.parse("http://#{TWITTER_USER}:#{TWITTER_PASS}@twitter.com/statuses/update.json"),
{'status' => tweet}
)
end
direct_messages.reverse.each do |dm|
next if dm["id"] == last_tweet
tweet = "from @#{dm["sender_screen_name"]}: #{dm["text"]}"
send_update(tweet)
end
update_last_tweet(direct_messages.first["id"]) if direct_messages.first