#!/usr/bin/env ruby ## Tumblr is a command line interface for publishing to Tumblr.com ## ## Usage: tumblr [ OPTIONS ] [ FILE or URL ] ## ## Publish to Tumblr. If a URL is given, it will create a link post. ## If URL is a YouTube or Vimeo URL, it will create a video post. ## If a FILE is given, it will publish its contents. ## See tumblr(5) for formatting instructions for FILE. ## If a URL or FILE is not given, tumblr will read from the Standard Input. ## If tumblr reads from the Standard Input, you must authenticate with -a. ## ## require 'optparse' def usage File.readlines(__FILE__). grep(/^##.*/). map { |line| line.chomp[3..-1] }. join("\n") end begin require 'tumblr' rescue LoadError raise if $!.to_s !~ /tumblr/ libdir = File.expand_path("../../lib", __FILE__).sub(/^#{Dir.pwd}/, '.') if !$:.include?(libdir) $:.unshift libdir require File.expand_path('../../.bundle/environment', __FILE__) retry end raise end publisher = {} publish_state = nil group = nil ARGV.options do |option| option.banner = usage option.separator "Options" auth_text = 'Email Address and Password, separated by a colon' option.on('-a EMAIL:PASSWORD', '--auth EMAIL:PASSWORD', auth_text) do |auth| publisher[:email],publisher[:password] = auth.split(':') end address_text = 'Email Address (will prompt for password)' option.on('-e EMAIL', '--email EMAIL', address_text) do |email| publisher[:email] = email end option.on('--credentials FILE', 'A YAML file with "email" and "password" keys for authentication') do |file| credentials = YAML.load(File.read(file)) publisher[:email] = credentials['email'] publisher[:password] = credentials['password'] end option.on('-p','--publish', 'Publish the post immediately (ignores "state" parameter)') { publish_state = :published } option.on('-q','--queue', 'Add the post to the queue') { publish_state = :queue } option.on('-d','--draft', 'Save the post as a draft') { publish_state = :draft } option.on('--group=GROUP','Publish to a group blog') {|value| group = value } option.separator "" option.on('-h','--help', 'Show this help message') { puts option ; exit } option.on('-v','--version', 'Show version number') do puts Tumblr::VERSION exit end option.separator "" option.parse! end if ARGV.empty? && STDIN.tty? puts usage puts "See 'tumblr --help' for more information." exit end input = if !ARGV.empty? path = ARGV.first (path =~ /^https?:\/\/\S+$/i) ? path : File.read(path) else STDIN.read.chomp! end if !STDIN.tty? && (!publisher[:email] || !publisher[:password]) bad_auth = %q(Error: An email address and password are required. Use 'tumblr -a' to authenticate. See 'tumblr --help' for details.) abort bad_auth end if !publisher[:email] || !publisher[:password] begin require 'highline/import' rescue LoadError require 'rubygems' retry end def get_email(pub) pub[:email] = ask("Email Address: ") get_email(pub) if pub[:email].empty? end def get_password(pub) pub[:password] = ask("Password: ") { |q| q.echo = false } get_password(pub) if pub[:password].empty? end if !publisher[:email] get_email(publisher) end if !publisher[:password] get_password(publisher) end end post = Tumblr.parse(input) post.state = publish_state if publish_state post.group = group if group response = Tumblr.execute(publisher, post) if response.success? puts "Published to Tumblr. The ID for this post is: #{response.body}" exit else abort %Q(Oh no! Something went wrong. Tumblr says, "#{response.body}") end