Skip to content

Commit

Permalink
Add a number of output options to bin/limgur.
Browse files Browse the repository at this point in the history
Specifically, when uploading an image, you can choose which pieces of
information get printed out.  For instance,
	limgur -u foo.jpg -f fi
will print the original filename, followed by the hotlinkable image url on the
next line.

In addition, the -o flag allows you to set an output medium: plaintext, titles,
bbcode or html.  Titles will present the user with labels indicating which line
is what (and thus is the default).  bbcode and html present images wrapped in
the appropriate image tags and anchors in anchor tags, while leaving other
things (like delete hashes) alone.

Lastly, -q prevents all output to STDOUT, and -Q prevents all output to either
STDOUT or STDERR.
  • Loading branch information
xiongchiamiov committed Aug 2, 2010
1 parent 2f867d9 commit eb594be
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 16 deletions.
37 changes: 36 additions & 1 deletion bin/limgur
Expand Up @@ -7,6 +7,9 @@ require 'limgur/limgur'

limgur = Limgur.new '90b4d040607755992895fdd5bb586ba2'

stdout = $stdout
stderr = $stderr

options = {}
ARGV.unshift '-h' if ARGV.empty?
opts = OptionParser.new do |opts|
Expand All @@ -23,6 +26,35 @@ opts = OptionParser.new do |opts|
opts.on('-s', '--scrot', 'Take a screenshot then upload it') do
options[:action] ||= :scrot
end

opts.on('-f FORMAT', '--format=FORMAT', 'Format of the output, with each letter being one line',
"\tf filename",
"\tI image hash",
"\tD delete hash",
"\ti image url",
"\tl large thumbnail",
"\ts small thumbnail",
"\tr imgur page",
"\td delete page",
"\tS status",
"\tn newline") do |val|
options[:format] = val
end

opts.on('-o OPTION', '--output=OPTION', ['plaintext', 'titles', 'bbcode', 'html'],
'Choose a method of presenting any URLs',
"\tOne of: plaintext, titles, bbcode, html") do |val|
options[:output] = val
end

opts.on('-q', '--quiet', "Don't write to stdout") do
$stdout = File.new('/dev/null', 'w')
end

opts.on('-Q', '--really-quiet', "Don't write to stdout or stderr") do
$stdout = File.new('/dev/null', 'w')
$stderr = File.new('/dev/null', 'w')
end

opts.on_tail('-h', '--help', 'Show this message') do
puts opts
Expand All @@ -48,7 +80,7 @@ end
case options[:action]
when :upload
ARGV.each do |arg|
puts limgur.upload arg
puts limgur.upload arg, options
end
when :delete
ARGV.each do |arg|
Expand All @@ -59,3 +91,6 @@ case options[:action]
puts limgur.scrot arg
end
end

$stdout = stdout
$stderr = stderr
72 changes: 65 additions & 7 deletions lib/limgur.rb
Expand Up @@ -7,7 +7,8 @@ def initialize key
@key = key
end

def upload image
def upload image, options={}
options = {:format => 'Sridn', :output => 'titles'}.merge options
isURL = true
begin
if isURL && URI.parse(image).scheme
Expand All @@ -24,18 +25,75 @@ def upload image
end

if response['rsp']['stat'] == 'fail'
response['rsp']['error_msg']
$stderr.puts response['rsp']['error_msg']
"fail\n" if options[:format].count('S') > 0
else
"Image was uploaded successfully!\n\n" \
"Imgur page: #{response['rsp']['image']['imgur_page']}\n" \
"Original image: #{response['rsp']['image']['original_image']}\n" \
"Delete hash: #{response['rsp']['image']['delete_hash']}\n"
output = ''

options[:format].each_char do |char|
case char
when 'f'
output << 'File: ' if options[:output] == 'titles'
output << image
when 'I'
output << 'Image hash: ' if options[:output] == 'titles'
output << response['rsp']['image']['image_hash']
when 'D'
output << 'Delete hash: ' if options[:output] == 'titles'
output << response['rsp']['image']['delete_hash']
when 'i'
output << 'Original image: ' if options[:output] == 'titles'
output << '[img]' if options[:output] == 'bbcode'
output << '<img src="' if options[:output] == 'html'
output << response['rsp']['image']['original_image']
output << '" />' if options[:output] == 'html'
output << '[/img]' if options[:output] == 'bbcode'
when 'l'
output << 'Large thumbnail: ' if options[:output] == 'titles'
output << "[url=#{response['rsp']['image']['original_image']}][img]" if options[:output] == 'bbcode'
output << '<a href="' << response['rsp']['image']['original_image'] << '"><img src="' if options[:output] == 'html'
output << response['rsp']['image']['large_thumbnail']
output << '" /></a>' if options[:output] == 'html'
output << '[/img][/url]' if options[:output] == 'bbcode'
when 's'
output << 'Small thumbnail: ' if options[:output] == 'titles'
output << "[url=#{response['rsp']['image']['original_image']}][img]" if options[:output] == 'bbcode'
output << '<a href="' << response['rsp']['image']['original_image'] << '"><img src="' if options[:output] == 'html'
output << response['rsp']['image']['small_thumbnail']
output << '" /></a>' if options[:output] == 'html'
output << '[/img][/url]' if options[:output] == 'bbcode'
when 'r'
output << 'Imgur page: ' if options[:output] == 'titles'
output << '[url=' if options[:output] == 'bbcode'
output << '<a href="' if options[:output] == 'html'
output << response['rsp']['image']['imgur_page']
output << '">Imgur</a>' if options[:output] == 'html'
output << ']Imgur[/url]' if options[:output] == 'bbcode'
when 'd'
output << 'Delete page: ' if options[:output] == 'titles'
output << '[url=' if options[:output] == 'bbcode'
output << '<a href="' if options[:output] == 'html'
output << response['rsp']['image']['delete_page']
output << '">Delete</a>' if options[:output] == 'html'
output << ']Delete[/url]' if options[:output] == 'bbcode'
when 'S'
output << 'Status: ' if options[:output] == 'titles'
output << response['rsp']['stat']
when 'n'
else
$stderr.puts "#{char} not recognized as a format character!"
end
output << "\n"
end

return output
end
rescue URI::InvalidURIError
isURL = false
retry
rescue Curl::Err::ReadError
'Please provide a valid image.'
$stderr.puts 'Please provide a valid image.'
"invalid image\n" if options[:format].count('S') > 0
end
end

Expand Down
72 changes: 64 additions & 8 deletions test/test_limgur.rb
Expand Up @@ -14,27 +14,83 @@ class LimgurTest < Test::Unit::TestCase

context 'uploading an image' do
test 'does indeed upload an image' do
upload = @limgur.upload 'tmp/test.jpg'
upload = @limgur.upload 'tmp/test.jpg', {:format => 'S', :output => 'plaintext'}

assert_equal 'Image was uploaded successfully!', upload.split("\n").first
assert_equal "ok\n", upload
end

test 'uploads an image with spaces in the name' do
upload = @limgur.upload 'tmp/test with spaces.jpg'
upload = @limgur.upload 'tmp/test with spaces.jpg', {:format => 'S', :output => 'plaintext'}

assert_equal 'Image was uploaded successfully!', upload.split("\n").first
assert_equal "ok\n", upload
end

test 'uploads an image from a url' do
upload = @limgur.upload 'http://github.com/xiongchiamiov/limgur/raw/master/test/test.jpg'
upload = @limgur.upload 'http://github.com/xiongchiamiov/limgur/raw/master/test/test.jpg', {:format => 'S', :output => 'plaintext'}

assert_equal 'Image was uploaded successfully!', upload.split("\n").first
assert_equal "ok\n", upload
end

test 'does not upload an invalid image' do
upload = @limgur.upload 'test_empty.jpg'
stderr = $stderr
$stderr = File.new('/dev/null', 'w')
upload = @limgur.upload 'test_empty.jpg', {:format => 'S', :output => 'plaintext'}
$stderr = stderr

assert_equal "invalid image\n", upload
end

test 'pays attention to format options' do
upload = @limgur.upload 'tmp/test.jpg', {:format => 'fIDilsrdSn', :output => 'plaintext'}
upload = upload.split("\n")

assert_equal 'tmp/test.jpg', upload[0]
assert_match /\w+/, upload[1]
assert_match /\w+/, upload[2]
assert_match /http:\/\/i\.imgur\.com\/\w+\.\w+/, upload[3]
assert_match /http:\/\/i\.imgur\.com\/\w+l\.\w+/, upload[4]
assert_match /http:\/\/i\.imgur\.com\/\w+s\.\w+/, upload[5]
assert_match /http:\/\/imgur\.com\/\w+/, upload[6]
assert_match /http:\/\/imgur\.com\/delete\/\w+/, upload[7]
assert_match /\w+/, upload[8]
assert_equal nil, upload[9]
end

test 'outputs bbCode when asked to' do
upload = @limgur.upload 'tmp/test.jpg', {:format => 'ilsrd', :output => 'bbcode'}
upload = upload.split("\n")

assert_match /\[img\]http:\/\/i.imgur.com\/\w+\.\w+\[\/img\]/, upload[0]
assert_match /\[url=http:\/\/i.imgur.com\/\w+\.\w+\]\[img\]http:\/\/i.imgur.com\/\w+l\.\w+\[\/img\]\[\/url\]/, upload[1]
assert_match /\[url=http:\/\/i.imgur.com\/\w+\.\w+\]\[img\]http:\/\/i.imgur.com\/\w+s\.\w+\[\/img\]\[\/url\]/, upload[2]
assert_match /\[url=http:\/\/imgur.com\/\w+\]Imgur\[\/url\]/, upload[3]
assert_match /\[url=http:\/\/imgur.com\/delete\/\w+\]Delete\[\/url\]/, upload[4]
end

test 'outputs HTML when asked to' do
upload = @limgur.upload 'tmp/test.jpg', {:format => 'ilsrd', :output => 'html'}
upload = upload.split("\n")

assert_match /<img src="http:\/\/i.imgur\.com\/\w+\.\w+" \/>/, upload[0]
assert_match /<a href="http:\/\/i\.imgur\.com\/\w+\.\w+"><img src="http:\/\/i.imgur\.com\/\w+l\.\w+" \/><\/a>/, upload[1]
assert_match /<a href="http:\/\/i\.imgur\.com\/\w+\.\w+"><img src="http:\/\/i.imgur\.com\/\w+s\.\w+" \/><\/a>/, upload[2]
assert_match /<a href="http:\/\/imgur\.com\/\w+">Imgur<\/a>/, upload[3]
assert_match /<a href="http:\/\/imgur\.com\/delete\/\w+">Delete<\/a>/, upload[4]
end

test 'outputs titles when asked to' do
upload = @limgur.upload 'tmp/test.jpg', {:format => 'fIDilsrdS', :output => 'titles'}
upload = upload.split("\n").map {|line| line.split(':')[0]}

assert_equal 'Please provide a valid image.', upload
assert_equal 'File', upload[0]
assert_equal 'Image hash', upload[1]
assert_equal 'Delete hash', upload[2]
assert_equal 'Original image', upload[3]
assert_equal 'Large thumbnail', upload[4]
assert_equal 'Small thumbnail', upload[5]
assert_equal 'Imgur page', upload[6]
assert_equal 'Delete page', upload[7]
assert_equal 'Status', upload[8]
end
end

Expand Down

0 comments on commit eb594be

Please sign in to comment.