Sha256: 1d535feb9ad0ad5ab8a59680ff07d93dac56852e791cebfc849e220b4fc7c0c0

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

#!/usr/bin/env ruby

require 'twitter'
require 'httparty'
require File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib", "instafavs")

$verbose = false

def verbose(msg, level=1)
  if $verbose
    puts "* #{msg}" if level == 1
    puts "  -> #{msg}" if level == 2
  end
end

def send_links_from_fav_tweets_to_instapaper
  Instafavs::Config.load!

  seen_fav_ids = Set.new
  favourites = Twitter.favorites(Instafavs::Config.twitter_username)
  verbose("found #{favourites.size} latest favourites for user #{Instafavs::Config.twitter_username}")
  favourites.each_with_index do |f, i|
    seen_fav_ids << f.id_str
    if Instafavs::Config.last_seen_fav_ids.include?(f.id_str)
      verbose("skipping ##{i + 1} '#{f.text}', already seen")
      next
    end
    verbose("scanning #{f.text} for urls to extract")
    f.text.scan(%r{http(?:s?)://[^ ]+}).each do |url|
      verbose("found #{url}, sending to instapaper", 2)
      HTTParty.post("https://www.instapaper.com/api/add",
                    :query => { :url => url, :username => Instafavs::Config.instapaper_username,
                                :password => Instafavs::Config.instapaper_password })
    end
  end
  verbose("updating config with #{seen_fav_ids.size} latest seen ids")
  Instafavs::Config.update_last_seen_fav_ids!(seen_fav_ids)
end

def collect_config_info
  config = {}
  %w(twitter_username instapaper_username instapaper_password).each do |needed_value|
    while config[needed_value].nil? || config[needed_value] == ""
      print "#{needed_value.sub(/_/, " ").capitalize}: "
      config[needed_value] = gets.strip
    end
  end
  
  config
end

options = Instafavs::parse_cmd_line_options!

$verbose = options[:verbose]

if options[:configure]
  Instafavs::Config.save!(collect_config_info)
  puts "Configuration saved successfully."
else
  send_links_from_fav_tweets_to_instapaper
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
instafavs-0.0.5 bin/instafavs