-
-
Save kb10uy/126716c0e9584442c7d8 to your computer and use it in GitHub Desktop.
Twitter Yo-ping tool.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "optparse" | |
require "twitter" | |
Version = "1.1" | |
# Twitter API Tokens | |
token = { | |
consumer_key: "", | |
consumer_secret: "", | |
access_token: "", | |
access_token_secret: "" | |
} | |
@target = "" | |
@aimed = false | |
@exclude = false | |
@count = 0 | |
@aimed_count = 0 | |
# init | |
opt = OptionParser.new | |
opt.on("-t SN", "--target SN", "Set a particular Yo target.") { |sn| | |
if sn !~ /@\w+/ then | |
puts "\e[31mError: target screen name must starts with '@'\e[0m" | |
exit | |
end | |
@target = sn | |
@aimed = true | |
} | |
opt.on("-e", "--exclude-global", "Excludes global(non-reply) Yo.") { |v| | |
@exclude = v | |
} | |
token.each { |key, val| | |
if val == "" then | |
puts "\e[31mError: #{key} is empty.\e[0m" | |
exit | |
end | |
} | |
@rest = Twitter::REST::Client.new(token) | |
@stream = Twitter::Streaming::Client.new(token) | |
# listening thread | |
streaming = Thread.new do | |
@stream.user do |status| | |
next unless status.is_a?(Twitter::Tweet) | |
next unless status.text =~ /^(@\w+\s+)*Yo\s*$/i | |
@et = status.created_at - @my_yo.created_at | |
if status.text.include?("@#{@my_sn}") then | |
puts "\e[32mAimed Yo reply from @#{status.user.screen_name} in #{@et.round(3)}s\e[0m" | |
@aimed_count += 1 | |
elsif !@exclude then | |
puts "Global Yo response from @#{status.user.screen_name} in #{@et.round(3)}s" | |
end | |
@count += 1 | |
end | |
end | |
sleep 3 | |
if (streaming.status == false) | |
puts "\e[31mUserstream listen error!\e[0m" | |
exit 1 | |
end | |
puts "Twitter Yo Tool Version #{Version}" | |
puts "Original: paralleltree, Forked: kb10uy" | |
puts "" | |
# check environment | |
begin | |
opt.parse(ARGV) | |
rescue => e | |
puts "\e[31mError: #{e.message} (#{e.class})\e[0m" | |
exit 1 | |
end | |
# start | |
print "Sending Yo " | |
print "to #{@target} " if @aimed | |
begin | |
@my_yo = @rest.update((@aimed ? "#{@target} " : "") + "Yo") | |
@my_sn = @my_yo.user.screen_name | |
puts "=> #{@my_yo.id}" | |
rescue => e | |
puts | |
puts "\e[31mError: #{e.message} (#{e.class})\e[0m" | |
exit 1 | |
end | |
begin | |
streaming.join | |
rescue Interrupt | |
end | |
puts | |
puts "--- Yo statistics ---" | |
puts "Yo transmitted on #{@my_yo.created_at.iso8601}" | |
puts "Yo Response received #{@count} times" | |
puts "+-Global Yo: #{@count - @aimed_count} times" | |
puts "+-Aimed Yo: #{@aimed_count} times" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment