Created
February 19, 2012 17:49
-
-
Save vinc/1864831 to your computer and use it in GitHub Desktop.
CLI for sending URLs to Instapaper
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
require 'uri' | |
require 'net/http' | |
require 'yaml' | |
require 'highline/import' | |
abort 'Usage: readlater <url>' if ARGV.length == 0 | |
# Build Request(s) | |
api = 'https://www.instapaper.com/api' | |
uri = URI("#{api}/add") | |
until ARGV.length == 0 do | |
params = { :url => ARGV.shift } | |
uri.query = URI.encode_www_form(params) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
# Add authentication | |
config = ENV['XDG_CONFIG_HOME'] | |
config = "#{ENV['HOME']}/.config" if config.nil? | |
config += '/readlater/config.yml' | |
yml = File.exists?(config) ? YAML::load(File.open(config)) : nil | |
username = yml.nil? ? ask('Username: ') : yml['username'] | |
password = yml.nil? ? ask('Password: ') {|q| q.echo = false} : yml['password'] | |
req = Net::HTTP::Get.new(uri.request_uri) | |
req.basic_auth(username, password) | |
# Send request | |
res = http.request(req) | |
case res.code.to_i | |
when 201 | |
next | |
when 403 | |
abort 'Error: Invalid username or password' | |
else | |
abort "Error: API returned status code: #{res.code}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment