limgur 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-2010 Danny Tatom
2
+ Copyright (c) 2010 James Pearson
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ # limgur
2
+
3
+ CLI to Imgur, letting you upload and delete images.
4
+
5
+ If you've got scrot installed, you can also take a screenshot and upload it in one command.
6
+
7
+ ## Usage
8
+
9
+ Usage: limgur [options]
10
+ -u, --upload Upload via image or URL
11
+ -d, --delete Delete an image via hash
12
+ -s, --scrot Take a screenshot then upload it
13
+ -h, --help Show this message
14
+ -v, --version Show version
15
+
16
+ ## TODO
17
+
18
+ * Allow scrot'n to a directory without specifying a filename (eg. limgur -s ~/images/)
19
+ * Accept scrot options while scrot'n (eg. limgur -s -c -d 3 ~/images/wat.png)
20
+
21
+ ## Copyright
22
+
23
+ Copyright (c) 2009-2010 Danny Tatom, 2010 James Pearson. See LICENSE for details.
@@ -0,0 +1,11 @@
1
+ require "mg"
2
+ MG.new("limgur.gemspec")
3
+
4
+ require 'fileutils'
5
+ require 'rake/testtask'
6
+
7
+ task :default => :test
8
+
9
+ Rake::TestTask.new("test") do |t|
10
+ t.pattern = 'test/test_*.rb'
11
+ end
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
4
+
5
+ require 'optparse'
6
+ require 'limgur'
7
+
8
+ limgur = Limgur.new '90b4d040607755992895fdd5bb586ba2'
9
+
10
+ stdout = $stdout
11
+ stderr = $stderr
12
+
13
+ options = {}
14
+ ARGV.unshift '-h' if ARGV.empty?
15
+ opts = OptionParser.new do |opts|
16
+ opts.banner = 'Usage: limgur [options]'
17
+
18
+ opts.on('-u', '--upload', 'Upload via image or URL') do
19
+ options[:action] ||= :upload
20
+ end
21
+
22
+ opts.on('-d', '--delete', 'Delete an image via hash') do
23
+ options[:action] ||= :delete
24
+ end
25
+
26
+ opts.on('-s', '--scrot', 'Take a screenshot then upload it') do
27
+ options[:action] ||= :scrot
28
+ end
29
+
30
+ opts.on('-f FORMAT', '--format=FORMAT', 'Format of the output, with each letter being one line',
31
+ "\tf filename",
32
+ "\tI image hash",
33
+ "\tD delete hash",
34
+ "\ti image url",
35
+ "\tl large thumbnail",
36
+ "\ts small thumbnail",
37
+ "\tr imgur page",
38
+ "\td delete page",
39
+ "\tS status",
40
+ "\tn newline") do |val|
41
+ options[:format] = val
42
+ end
43
+
44
+ opts.on('-o OPTION', '--output=OPTION', ['plaintext', 'titles', 'bbcode', 'html'],
45
+ 'Choose a method of presenting any URLs',
46
+ "\tOne of: plaintext, titles, bbcode, html") do |val|
47
+ options[:output] = val
48
+ end
49
+
50
+ opts.on('-q', '--quiet', "Don't write to stdout") do
51
+ $stdout = File.new('/dev/null', 'w')
52
+ end
53
+
54
+ opts.on('-Q', '--really-quiet', "Don't write to stdout or stderr") do
55
+ $stdout = File.new('/dev/null', 'w')
56
+ $stderr = File.new('/dev/null', 'w')
57
+ end
58
+
59
+ opts.on_tail('-h', '--help', 'Show this message') do
60
+ puts opts
61
+ exit 0
62
+ end
63
+
64
+ opts.on_tail('-v', '--version', 'Show version') do
65
+ puts "limgur v#{Limgur.version}"
66
+ puts "Copyright (c) 2009-2010 Danny Tatom <dannytatom@gmail.com>"
67
+ puts "Copyright (c) 2010 James Pearson <xiong.chiamiov@gmail.com>"
68
+ puts "Licensed under the MIT"
69
+ exit 0
70
+ end
71
+ end.parse!
72
+
73
+ if options[:action] == :upload && ARGV.empty?
74
+ warn 'Missing image to upload'
75
+ end
76
+
77
+ if options[:action] == :delete && ARGV.empty?
78
+ warn 'Missing delete hash'
79
+ end
80
+
81
+ case options[:action]
82
+ when :upload
83
+ ARGV.each do |arg|
84
+ puts limgur.upload arg, options
85
+ end
86
+ when :delete
87
+ ARGV.each do |arg|
88
+ puts limgur.delete arg
89
+ end
90
+ when :scrot
91
+ ARGV.each do |arg|
92
+ puts limgur.scrot arg
93
+ end
94
+ end
95
+
96
+ $stdout = stdout
97
+ $stderr = stderr
@@ -0,0 +1,124 @@
1
+ require 'open-uri'
2
+ require 'curb'
3
+ require 'crack/json'
4
+
5
+ class Limgur
6
+ def initialize key
7
+ @key = key
8
+ end
9
+
10
+ def upload image, options={}
11
+ options = {:format => 'Sridn', :output => 'titles'}.merge options
12
+ isURL = true
13
+ begin
14
+ if isURL && URI.parse(image).scheme
15
+ c = Curl::Easy.new 'http://imgur.com/api/upload.json'
16
+ c.http_post Curl::PostField.content('key', @key),
17
+ Curl::PostField.content('image', image)
18
+ response = Crack::JSON.parse c.body_str
19
+ else
20
+ c = Curl::Easy.new 'http://imgur.com/api/upload.json'
21
+ c.multipart_form_post = true
22
+ c.http_post Curl::PostField.content('key', @key),
23
+ Curl::PostField.file('image', image)
24
+ response = Crack::JSON.parse c.body_str
25
+ end
26
+
27
+ if response['rsp']['stat'] == 'fail'
28
+ $stderr.puts response['rsp']['error_msg']
29
+ "fail\n" if options[:format].count('S') > 0
30
+ else
31
+ output = ''
32
+
33
+ options[:format].each_char do |char|
34
+ case char
35
+ when 'f'
36
+ output << 'File: ' if options[:output] == 'titles'
37
+ output << image
38
+ when 'I'
39
+ output << 'Image hash: ' if options[:output] == 'titles'
40
+ output << response['rsp']['image']['image_hash']
41
+ when 'D'
42
+ output << 'Delete hash: ' if options[:output] == 'titles'
43
+ output << response['rsp']['image']['delete_hash']
44
+ when 'i'
45
+ output << 'Original image: ' if options[:output] == 'titles'
46
+ output << '[img]' if options[:output] == 'bbcode'
47
+ output << '<img src="' if options[:output] == 'html'
48
+ output << response['rsp']['image']['original_image']
49
+ output << '" />' if options[:output] == 'html'
50
+ output << '[/img]' if options[:output] == 'bbcode'
51
+ when 'l'
52
+ output << 'Large thumbnail: ' if options[:output] == 'titles'
53
+ output << "[url=#{response['rsp']['image']['original_image']}][img]" if options[:output] == 'bbcode'
54
+ output << '<a href="' << response['rsp']['image']['original_image'] << '"><img src="' if options[:output] == 'html'
55
+ output << response['rsp']['image']['large_thumbnail']
56
+ output << '" /></a>' if options[:output] == 'html'
57
+ output << '[/img][/url]' if options[:output] == 'bbcode'
58
+ when 's'
59
+ output << 'Small thumbnail: ' if options[:output] == 'titles'
60
+ output << "[url=#{response['rsp']['image']['original_image']}][img]" if options[:output] == 'bbcode'
61
+ output << '<a href="' << response['rsp']['image']['original_image'] << '"><img src="' if options[:output] == 'html'
62
+ output << response['rsp']['image']['small_thumbnail']
63
+ output << '" /></a>' if options[:output] == 'html'
64
+ output << '[/img][/url]' if options[:output] == 'bbcode'
65
+ when 'r'
66
+ output << 'Imgur page: ' if options[:output] == 'titles'
67
+ output << '[url=' if options[:output] == 'bbcode'
68
+ output << '<a href="' if options[:output] == 'html'
69
+ output << response['rsp']['image']['imgur_page']
70
+ output << '">Imgur</a>' if options[:output] == 'html'
71
+ output << ']Imgur[/url]' if options[:output] == 'bbcode'
72
+ when 'd'
73
+ output << 'Delete page: ' if options[:output] == 'titles'
74
+ output << '[url=' if options[:output] == 'bbcode'
75
+ output << '<a href="' if options[:output] == 'html'
76
+ output << response['rsp']['image']['delete_page']
77
+ output << '">Delete</a>' if options[:output] == 'html'
78
+ output << ']Delete[/url]' if options[:output] == 'bbcode'
79
+ when 'S'
80
+ output << 'Status: ' if options[:output] == 'titles'
81
+ output << response['rsp']['stat']
82
+ when 'n'
83
+ else
84
+ $stderr.puts "#{char} not recognized as a format character!"
85
+ end
86
+ output << "\n"
87
+ end
88
+
89
+ return output
90
+ end
91
+ rescue URI::InvalidURIError
92
+ isURL = false
93
+ retry
94
+ rescue Curl::Err::ReadError
95
+ $stderr.puts 'Please provide a valid image.'
96
+ "invalid image\n" if options[:format].count('S') > 0
97
+ end
98
+ end
99
+
100
+ def delete hash
101
+ c = Curl::Easy.new "http://imgur.com/api/delete/#{hash}.json"
102
+ c.http_get
103
+ response = Crack::JSON.parse c.body_str
104
+
105
+ if response['error']
106
+ response['error']['error_msg']
107
+ else
108
+ 'Image was successfully deleted!'
109
+ end
110
+ end
111
+
112
+ def scrot filename=nil
113
+ filename = Time.now.to_s.gsub(' ', '').gsub(':', '') + '.png' if filename.nil?
114
+
115
+ if !(system 'scrot ' + filename)
116
+ raise "scrot not installed"
117
+ end
118
+ upload File.expand_path filename
119
+ end
120
+
121
+ def self.version
122
+ '1.0.2'
123
+ end
124
+ end
@@ -0,0 +1,5 @@
1
+ require 'contest'
2
+ require 'test/unit'
3
+ require 'fileutils'
4
+
5
+ require 'lib/limgur'
Binary file
Binary file
File without changes
@@ -0,0 +1,138 @@
1
+ require 'test/helper'
2
+
3
+ class LimgurTest < Test::Unit::TestCase
4
+ setup do
5
+ FileUtils.mkdir 'tmp'
6
+ FileUtils.cp Dir.glob('test/*.jpg'), 'tmp/'
7
+
8
+ @limgur = Limgur.new('90b4d040607755992895fdd5bb586ba2')
9
+ end
10
+
11
+ teardown do
12
+ FileUtils.rm_rf 'tmp'
13
+ end
14
+
15
+ context 'uploading an image' do
16
+ test 'does indeed upload an image' do
17
+ upload = @limgur.upload 'tmp/test.jpg', {:format => 'S', :output => 'plaintext'}
18
+
19
+ assert_equal "ok\n", upload
20
+ end
21
+
22
+ test 'uploads an image with spaces in the name' do
23
+ upload = @limgur.upload 'tmp/test with spaces.jpg', {:format => 'S', :output => 'plaintext'}
24
+
25
+ assert_equal "ok\n", upload
26
+ end
27
+
28
+ test 'uploads an image from a url' do
29
+ upload = @limgur.upload 'http://github.com/xiongchiamiov/limgur/raw/master/test/test.jpg', {:format => 'S', :output => 'plaintext'}
30
+
31
+ assert_equal "ok\n", upload
32
+ end
33
+
34
+ test 'does not upload an invalid image' do
35
+ stderr = $stderr
36
+ $stderr = File.new('/dev/null', 'w')
37
+ upload = @limgur.upload 'test_empty.jpg', {:format => 'S', :output => 'plaintext'}
38
+ $stderr = stderr
39
+
40
+ assert_equal "invalid image\n", upload
41
+ end
42
+
43
+ test 'pays attention to format options' do
44
+ upload = @limgur.upload 'tmp/test.jpg', {:format => 'fIDilsrdSn', :output => 'plaintext'}
45
+ upload = upload.split("\n")
46
+
47
+ assert_equal 'tmp/test.jpg', upload[0]
48
+ assert_match /\w+/, upload[1]
49
+ assert_match /\w+/, upload[2]
50
+ assert_match /http:\/\/i\.imgur\.com\/\w+\.\w+/, upload[3]
51
+ assert_match /http:\/\/i\.imgur\.com\/\w+l\.\w+/, upload[4]
52
+ assert_match /http:\/\/i\.imgur\.com\/\w+s\.\w+/, upload[5]
53
+ assert_match /http:\/\/imgur\.com\/\w+/, upload[6]
54
+ assert_match /http:\/\/imgur\.com\/delete\/\w+/, upload[7]
55
+ assert_match /\w+/, upload[8]
56
+ assert_equal nil, upload[9]
57
+ end
58
+
59
+ test 'outputs bbCode when asked to' do
60
+ upload = @limgur.upload 'tmp/test.jpg', {:format => 'ilsrd', :output => 'bbcode'}
61
+ upload = upload.split("\n")
62
+
63
+ assert_match /\[img\]http:\/\/i.imgur.com\/\w+\.\w+\[\/img\]/, upload[0]
64
+ assert_match /\[url=http:\/\/i.imgur.com\/\w+\.\w+\]\[img\]http:\/\/i.imgur.com\/\w+l\.\w+\[\/img\]\[\/url\]/, upload[1]
65
+ assert_match /\[url=http:\/\/i.imgur.com\/\w+\.\w+\]\[img\]http:\/\/i.imgur.com\/\w+s\.\w+\[\/img\]\[\/url\]/, upload[2]
66
+ assert_match /\[url=http:\/\/imgur.com\/\w+\]Imgur\[\/url\]/, upload[3]
67
+ assert_match /\[url=http:\/\/imgur.com\/delete\/\w+\]Delete\[\/url\]/, upload[4]
68
+ end
69
+
70
+ test 'outputs HTML when asked to' do
71
+ upload = @limgur.upload 'tmp/test.jpg', {:format => 'ilsrd', :output => 'html'}
72
+ upload = upload.split("\n")
73
+
74
+ assert_match /<img src="http:\/\/i.imgur\.com\/\w+\.\w+" \/>/, upload[0]
75
+ assert_match /<a href="http:\/\/i\.imgur\.com\/\w+\.\w+"><img src="http:\/\/i.imgur\.com\/\w+l\.\w+" \/><\/a>/, upload[1]
76
+ assert_match /<a href="http:\/\/i\.imgur\.com\/\w+\.\w+"><img src="http:\/\/i.imgur\.com\/\w+s\.\w+" \/><\/a>/, upload[2]
77
+ assert_match /<a href="http:\/\/imgur\.com\/\w+">Imgur<\/a>/, upload[3]
78
+ assert_match /<a href="http:\/\/imgur\.com\/delete\/\w+">Delete<\/a>/, upload[4]
79
+ end
80
+
81
+ test 'outputs titles when asked to' do
82
+ upload = @limgur.upload 'tmp/test.jpg', {:format => 'fIDilsrdS', :output => 'titles'}
83
+ upload = upload.split("\n").map {|line| line.split(':')[0]}
84
+
85
+ assert_equal 'File', upload[0]
86
+ assert_equal 'Image hash', upload[1]
87
+ assert_equal 'Delete hash', upload[2]
88
+ assert_equal 'Original image', upload[3]
89
+ assert_equal 'Large thumbnail', upload[4]
90
+ assert_equal 'Small thumbnail', upload[5]
91
+ assert_equal 'Imgur page', upload[6]
92
+ assert_equal 'Delete page', upload[7]
93
+ assert_equal 'Status', upload[8]
94
+ end
95
+ end
96
+
97
+ context 'deleting an image' do
98
+ test 'effectively deletes it' do
99
+ upload = @limgur.upload 'tmp/test.jpg'
100
+ hash = upload.split("\n")[3].gsub('Delete hash: ', '')
101
+ delete = @limgur.delete hash
102
+
103
+ assert_equal 'Image was successfully deleted!', delete
104
+ end
105
+ end
106
+
107
+ context 'using scrot' do
108
+ test 'saves a screenshot with default name if not given' do
109
+ filename = Time.now.to_s.gsub(' ', '').gsub(':', '') + '.png'
110
+
111
+ begin
112
+ screenshot = @limgur.scrot
113
+ assert_equal true, File.exists?(filename)
114
+
115
+ FileUtils.rm filename
116
+ rescue => e
117
+ if e.message == "scrot not installed"
118
+ omit "Scrot not installed"
119
+ else
120
+ raise e
121
+ end
122
+ end
123
+ end
124
+
125
+ test 'saves a screenshot with filename given' do
126
+ begin
127
+ screenshot = @limgur.scrot 'tmp/screenshot.png'
128
+ assert_equal true, File.exists?('tmp/screenshot.png')
129
+ rescue => e
130
+ if e.message == "scrot not installed"
131
+ omit "Scrot not installed"
132
+ else
133
+ raise e
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: limgur
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 2
8
+ - 0
9
+ - 0
10
+ version: 2.0.0
11
+ platform: ruby
12
+ authors:
13
+ - xiongchiamiov
14
+ - dannytatom
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-08-02 00:00:00 -07:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: curb
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: crack
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: contest
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: test-unit
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ type: :development
77
+ version_requirements: *id004
78
+ description: CLI to Imgur, letting you upload and delete images. If you've got scrot installed, you can also take a screenshot and upload it in one command.
79
+ email:
80
+ - xiong.chiamiov@gmail.com
81
+ - dannytatom@gmail.com
82
+ executables:
83
+ - limgur
84
+ extensions: []
85
+
86
+ extra_rdoc_files:
87
+ - README.md
88
+ files:
89
+ - LICENSE
90
+ - Rakefile
91
+ - README.md
92
+ - bin/limgur
93
+ - lib/limgur.rb
94
+ - test/test.jpg
95
+ - test/test with spaces.jpg
96
+ - test/helper.rb
97
+ - test/test_empty.jpg
98
+ - test/test_limgur.rb
99
+ has_rdoc: true
100
+ homepage: http://github.com/xiongchiamiov/limgur
101
+ licenses: []
102
+
103
+ post_install_message:
104
+ rdoc_options: []
105
+
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ requirements: []
127
+
128
+ rubyforge_project:
129
+ rubygems_version: 1.3.7
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: CLI to Imgur
133
+ test_files:
134
+ - test/test_limgur.rb