Last active
March 18, 2016 15:24
-
-
Save wschenk/86e1e87772e7d589e963 to your computer and use it in GitHub Desktop.
Basic twitter CLI to see what's going on
This file contains 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
require 'thor' | |
require 'twitter' | |
require 'httparty' | |
TWITTER_APP_KEY="rzlJX...." | |
TWITTER_APP_SECRET="euF5bI...." | |
TWITTER_ACCESS_TOKEN="17827...." | |
TWITTER_ACCESS_TOKEN_SECRET="7NSXm..." | |
def print_user_info( u ) | |
t = "%-20s: %s\n" | |
printf t, "Screenname", u.user_name | |
printf t, "Full Name", u.name | |
printf t, "Bio", u.description | |
printf t, "Website", lookup_url( u.website.to_s ) | |
printf t, "Joined", u.created_at.strftime( "%Y-%m-%d" ) | |
printf t, "Location", u.location | |
printf t, "Verified", u.verified? | |
printf t, "Tweets", u.tweets_count | |
printf t, "Followers", u.followers_count | |
printf t, "Following", u.friends_count | |
printf t, "Favorites count", u.favorites_count | |
end | |
def lookup_url( url ) | |
return nil if url.nil? || url == "" | |
r = HTTParty.head url, { follow_redirects: false } | |
r['location'] || url | |
end | |
class CLI < Thor | |
desc "user SCREENAME", "Look up info for a specific user." | |
def user( username ) | |
print_user_info client.user( "wschenk" ) | |
end | |
desc "lookup URL", "Resolve a link" | |
def lookup( url ) | |
puts lookup_url( url ) | |
end | |
desc "limits", "Print out the current rate limits" | |
def limits | |
resp = client.get( "/1.1/application/rate_limit_status.json" ) | |
current_time = Time.now.to_i | |
template = " %-40s %5d remaining, resets in %3d seconds\n" | |
resp.body[:resources].each do |category,resources| | |
puts category.to_s | |
resources.each do |resource,info| | |
printf template, resource.to_s, info[:remaining], (info[:reset] - current_time) | |
end | |
end | |
end | |
desc "user_timeline", "Show the authenticated user's tweets" | |
def user_timeline | |
client.user_timeline.each do |tweet| | |
puts "@#{tweet.user.user_name}:#{tweet.text}" | |
end | |
end | |
desc "home_timeline", "Show the authenticated user's timeline" | |
def home_timeline | |
client.home_timeline.each do |tweet| | |
puts "@#{tweet.user.user_name}:#{tweet.text}" | |
end | |
end | |
desc "retweets", "Show the authenticated user's retweets" | |
def retweets | |
client.retweets_of_me.each do |tweet| | |
puts "@#{tweet.user.user_name}:#{tweet.text}" | |
end | |
end | |
desc "mentions", "Show the authenticated user's mentions" | |
def mentions | |
client.mentions.each do |tweet| | |
puts "@#{tweet.user.user_name}:#{tweet.text}" | |
end | |
end | |
desc "followers SCREENNAME", "Prints out all of the users followers" | |
def followers( screenname ) | |
client.followers( screenname ).each do |u| | |
printf( "@%-15s %-20s %s\n", u.user_name, u.name, u.description ) | |
end | |
end | |
desc "search STRING", "Shows all the tweets that match the string" | |
options [:exact, :user_info] | |
def search( string ) | |
string = "\"#{string}\"" if options[:exact] | |
reach = 0 | |
client.search( string, count: 100 ).each do |t| | |
puts "#{t.id}:#{t.created_at}:@#{t.user.user_name}:#{t.user.followers_count}:#{t.retweet_count}:#{t.text}" | |
reach += t.user.followers_count | |
if options[:user_info] | |
print_user_info t.user if options[:user_info] | |
puts | |
end | |
end | |
puts "#{string} reached #{reach} people." | |
end | |
desc "filter TERMS", "Print out tweets that match the terms" | |
def filter( terms ) | |
streaming_client.filter(track: terms) do |object| | |
puts "@#{object.user.user_name}:#{object.text}" if object.is_a?(Twitter::Tweet) | |
end | |
end | |
desc "listen", "Prints our the authenticated user's stream as it happens" | |
def listen | |
streaming_client.user do |object| | |
case object | |
when Twitter::Tweet | |
puts "Tweet:@#{object.user.user_name}:#{object.text}" | |
when Twitter::DirectMessage | |
puts "DM:@#{object.sender.user_name}:#{object.text}" | |
when Twitter::Streaming::StallWarning | |
warn "Falling behind!" | |
end | |
end | |
end | |
private | |
def client | |
@client ||= Twitter::REST::Client.new do |config| | |
config.consumer_key = TWITTER_APP_KEY | |
config.consumer_secret = TWITTER_APP_SECRET | |
config.access_token = TWITTER_ACCESS_TOKEN | |
config.access_token_secret = TWITTER_ACCESS_TOKEN_SECRET | |
end | |
end | |
def streaming_client | |
@streaming_client ||= Twitter::Streaming::Client.new do |config| | |
config.consumer_key = TWITTER_APP_KEY | |
config.consumer_secret = TWITTER_APP_SECRET | |
config.access_token = TWITTER_ACCESS_TOKEN | |
config.access_token_secret = TWITTER_ACCESS_TOKEN_SECRET | |
end | |
end | |
end | |
# Only run if the script is called directly | |
if __FILE__ == $0 | |
CLI.start( ARGV ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment