forked from kastner/ruby-junk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathretweet-code.rb
More file actions
executable file
·55 lines (43 loc) · 1.21 KB
/
retweet-code.rb
File metadata and controls
executable file
·55 lines (43 loc) · 1.21 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
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'json'
require 'net/http'
TWITTER_USER = ""
TWITTER_PASS = ""
RETWEET_CODE = "" # This code is given out to trusted people allowed to retweet
LAST_TWEET_FILE = ".last_tweet_code"
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
def valid?(tweet)
!tweet.match(/\b#{RETWEET_CODE}\b/).nil?
end
def strip(tweet)
tweet.gsub(/\b#{RETWEET_CODE}\b\s*/, '')
end
direct_messages.reverse.each do |dm|
next if dm["id"] == last_tweet
tweet = "from @#{dm["sender_screen_name"]}: #{strip(dm["text"])}" if valid?(dm["text"])
send_update(tweet)
end
update_last_tweet(direct_messages.first["id"]) if direct_messages.first