Created
April 18, 2014 01:03
-
-
Save blakejakopovic/11019730 to your computer and use it in GitHub Desktop.
read_twitter_favourites
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
#!/usr/bin/env ruby | |
require 'dotenv' | |
Dotenv.load '~/bin/.env' | |
require 'twitter' # v5.0 | |
require 'terminal-notifier' | |
HOST_IGNORE_LIST = [ | |
'www.techmeme.com' #ignored because direct article link is desired | |
] | |
def ignored_url(uri) | |
uri.host && HOST_IGNORE_LIST.include?(uri.host) | |
end | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = ENV.fetch('TWITTER_CONSUMER_KEY') | |
config.consumer_secret = ENV.fetch('TWITTER_CONSUMER_SECRET') | |
config.access_token = ENV.fetch('TWITTER_USER_OAUTH_TOKEN') | |
config.access_token_secret = ENV.fetch('TWITTER_USER_OAUTH_TOKEN_SECRET') | |
end | |
twitter_username = ENV.fetch('TWITTER_USERNAME') | |
favourite_limit = ENV.fetch('TWITTER_FAVOURITE_LIMIT') { 20 } | |
options = {count: favourite_limit , include_entities: 1} | |
favourite_tweets = client.favorites(twitter_username, options) | |
collection_object = Hash.new {|h,k| h[k] = [] } | |
collection = favourite_tweets.each_with_object(collection_object) do |tweet, h| | |
tweet.urls.each do |entity| | |
uri = URI(entity.expanded_url) rescue nil | |
h[:links] << entity.expanded_url unless ignored_url(uri) | |
end | |
h[:ids] << tweet.id | |
end | |
unless collection[:links].empty? | |
links = collection[:links].reverse | |
# Open first link in a new window | |
system "/usr/local/bin/chrome-cli open #{links.shift} -n" | |
links.map do |link| | |
system "/usr/local/bin/chrome-cli open #{link}" | |
end | |
# system "open #{collection[:links].reverse.join(' ')}" | |
collection[:ids].map {|id| client.unfavorite(id); sleep 1 } | |
sleep 10 | |
new_favourites_count = client.favorites(twitter_username, {count: 200}).count | |
puts "#{new_favourites_count} favourite tweets remain" | |
TerminalNotifier.notify("#{new_favourites_count} favourite tweets remain", title: "Twitter Favourites") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment