random_jpg 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -1
- data/bin/random_jpg +1 -1
- data/lib/random_jpg/loader/flickr.rb +20 -0
- data/lib/random_jpg/loader/imgur.rb +21 -0
- data/lib/random_jpg/runner.rb +13 -5
- data/lib/random_jpg/version.rb +1 -1
- data/lib/random_jpg.rb +4 -1
- data/random_jpg.gemspec +1 -1
- data/spec/random_jpg/runner_options_spec.rb +64 -0
- metadata +8 -5
- data/lib/random_jpg/loader.rb +0 -18
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# random_jpg
|
2
2
|
|
3
3
|
random_jpg is a tool for easy downloading **random images from the web** for use in scripts, application test data etc.
|
4
|
-
It runs silently in the background feeding random
|
4
|
+
It runs silently in the background feeding random images to a [named pipe](http://en.wikipedia.org/wiki/Named_pipe) at a specified location, by default `/tmp/random.jpg`. By using a constant location, this simplifies a number of tasks related to downloading images.
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
@@ -51,6 +51,13 @@ For full usage info, run:
|
|
51
51
|
|
52
52
|
$ random_jpg -h
|
53
53
|
|
54
|
+
## Image sources
|
55
|
+
|
56
|
+
Image source can be chosen using `-l LOADER`. Available options:
|
57
|
+
|
58
|
+
* `flickr` (default)
|
59
|
+
* `imgur` (not recommended for production use ;)
|
60
|
+
|
54
61
|
## Info
|
55
62
|
|
56
63
|
random_jpg © 2012 [Łukasz Adamczak](http://czak.pl), read LICENSE file for details.
|
data/bin/random_jpg
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module RandomJpg
|
5
|
+
module Loader
|
6
|
+
class Flickr
|
7
|
+
API_URL = "http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=66c61b93c4723c7c3a3c519728eac252&per_page=1&extras=url_m&format=json"
|
8
|
+
|
9
|
+
def feed(path)
|
10
|
+
response = Net::HTTP.get URI(API_URL)
|
11
|
+
hash = JSON.parse response[14..-2]
|
12
|
+
image_data = Net::HTTP.get URI(hash["photos"]["photo"].first["url_m"])
|
13
|
+
|
14
|
+
File.open(path, "w") do |f|
|
15
|
+
f.write image_data
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module RandomJpg
|
5
|
+
module Loader
|
6
|
+
class Imgur
|
7
|
+
API_URL = "http://imgur.com/gallery/random.json"
|
8
|
+
|
9
|
+
def feed(path)
|
10
|
+
response = Net::HTTP.get URI(API_URL)
|
11
|
+
hash = JSON.parse response
|
12
|
+
image = hash["gallery"].select { |i| i["mimetype"] == "image/jpeg"}.first
|
13
|
+
image_data = Net::HTTP.get URI("http://i.imgur.com/#{image["hash"]}.jpg")
|
14
|
+
|
15
|
+
File.open(path, "w") do |f|
|
16
|
+
f.write image_data
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/random_jpg/runner.rb
CHANGED
@@ -2,12 +2,17 @@ require "optparse"
|
|
2
2
|
|
3
3
|
module RandomJpg
|
4
4
|
class Runner
|
5
|
+
attr_reader :path, :daemon, :force, :loader
|
6
|
+
|
5
7
|
def initialize(args = [])
|
6
8
|
@path = "/tmp/random.jpg"
|
7
9
|
@daemon = false
|
8
10
|
@force = false
|
9
|
-
@loader = Loader.new
|
11
|
+
@loader = Loader::Flickr.new
|
12
|
+
parse_options(args)
|
13
|
+
end
|
10
14
|
|
15
|
+
def parse_options(args)
|
11
16
|
OptionParser.new do |opts|
|
12
17
|
opts.banner = "Usage: random_jpg [options]"
|
13
18
|
opts.separator ""
|
@@ -21,6 +26,9 @@ module RandomJpg
|
|
21
26
|
opts.on("-f", "--force", "Force overwrite if a file exists at given path") do
|
22
27
|
@force = true
|
23
28
|
end
|
29
|
+
opts.on("-l LOADER", [:flickr, :imgur], "Image source (flickr [default], imgur)") do |loader|
|
30
|
+
@loader = Loader::Imgur.new if loader == :imgur
|
31
|
+
end
|
24
32
|
opts.on_tail("-h", "--help", "Show this message") do
|
25
33
|
puts opts
|
26
34
|
exit
|
@@ -29,10 +37,10 @@ module RandomJpg
|
|
29
37
|
end
|
30
38
|
|
31
39
|
def run
|
32
|
-
create_pipe(
|
33
|
-
Process.daemon if
|
34
|
-
trap("SIGINT") { File.unlink(
|
35
|
-
loop {
|
40
|
+
create_pipe(path, force)
|
41
|
+
Process.daemon if daemon
|
42
|
+
trap("SIGINT") { File.unlink(path); exit }
|
43
|
+
loop { loader.feed(path) }
|
36
44
|
end
|
37
45
|
|
38
46
|
def create_pipe(path, force = false)
|
data/lib/random_jpg/version.rb
CHANGED
data/lib/random_jpg.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
|
1
3
|
require "random_jpg/version"
|
2
4
|
require "random_jpg/error"
|
3
5
|
require "random_jpg/runner"
|
4
|
-
require "random_jpg/loader"
|
6
|
+
require "random_jpg/loader/flickr"
|
7
|
+
require "random_jpg/loader/imgur"
|
data/random_jpg.gemspec
CHANGED
@@ -4,7 +4,7 @@ require File.expand_path('../lib/random_jpg/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Łukasz Adamczak"]
|
6
6
|
gem.email = ["lukasz@czak.pl"]
|
7
|
-
gem.description = %q{RandomJpg is a tool for easy downloading random images for use in scripts, application test data etc. It runs silently in the background feeding random
|
7
|
+
gem.description = %q{RandomJpg is a tool for easy downloading random images for use in scripts, application test data etc. It runs silently in the background feeding random images to a named pipe at a specified location, by default /tmp/random.jpg.}
|
8
8
|
gem.summary = %q{A command-line tool for easy downloading & reading random images from the web.}
|
9
9
|
gem.homepage = "https://github.com/czak/random_jpg"
|
10
10
|
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module RandomJpg
|
4
|
+
describe Runner do
|
5
|
+
describe "#parse_options(args)" do
|
6
|
+
context "-p" do
|
7
|
+
it "requires an argument" do
|
8
|
+
expect {
|
9
|
+
subject.parse_options(["-p"])
|
10
|
+
}.to raise_error(OptionParser::MissingArgument)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "-p some/path" do
|
15
|
+
it "sets path to 'some/path'" do
|
16
|
+
subject.parse_options(["-p", "some/path"])
|
17
|
+
subject.path.should == "some/path"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "-d" do
|
22
|
+
it "enables daemon mode" do
|
23
|
+
subject.parse_options(["-d"])
|
24
|
+
subject.daemon.should be_true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "-f" do
|
29
|
+
it "enables force overwrite" do
|
30
|
+
subject.parse_options(["-f"])
|
31
|
+
subject.force.should be_true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "-l" do
|
36
|
+
it "requires an argument" do
|
37
|
+
expect {
|
38
|
+
subject.parse_options(["-l"])
|
39
|
+
}.to raise_error(OptionParser::MissingArgument)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "only accepts arguments: flickr, imgur" do
|
43
|
+
expect {
|
44
|
+
subject.parse_options(["-l", "wrong_loader"])
|
45
|
+
}.to raise_error(OptionParser::InvalidArgument)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "-l flickr" do
|
50
|
+
it "enables the Flickr loader" do
|
51
|
+
subject.parse_options(["-l", "flickr"])
|
52
|
+
subject.loader.should be_an_instance_of Loader::Flickr
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "-l imgur" do
|
57
|
+
it "enables the Imgur loader" do
|
58
|
+
subject.parse_options(["-l", "imgur"])
|
59
|
+
subject.loader.should be_an_instance_of Loader::Imgur
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_jpg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-24 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: RandomJpg is a tool for easy downloading random images for use in scripts,
|
15
|
-
application test data etc. It runs silently in the background feeding random
|
16
|
-
|
15
|
+
application test data etc. It runs silently in the background feeding random images
|
16
|
+
to a named pipe at a specified location, by default /tmp/random.jpg.
|
17
17
|
email:
|
18
18
|
- lukasz@czak.pl
|
19
19
|
executables:
|
@@ -30,10 +30,12 @@ files:
|
|
30
30
|
- bin/random_jpg
|
31
31
|
- lib/random_jpg.rb
|
32
32
|
- lib/random_jpg/error.rb
|
33
|
-
- lib/random_jpg/loader.rb
|
33
|
+
- lib/random_jpg/loader/flickr.rb
|
34
|
+
- lib/random_jpg/loader/imgur.rb
|
34
35
|
- lib/random_jpg/runner.rb
|
35
36
|
- lib/random_jpg/version.rb
|
36
37
|
- random_jpg.gemspec
|
38
|
+
- spec/random_jpg/runner_options_spec.rb
|
37
39
|
- spec/random_jpg/runner_spec.rb
|
38
40
|
- spec/spec_helper.rb
|
39
41
|
homepage: https://github.com/czak/random_jpg
|
@@ -62,5 +64,6 @@ specification_version: 3
|
|
62
64
|
summary: A command-line tool for easy downloading & reading random images from the
|
63
65
|
web.
|
64
66
|
test_files:
|
67
|
+
- spec/random_jpg/runner_options_spec.rb
|
65
68
|
- spec/random_jpg/runner_spec.rb
|
66
69
|
- spec/spec_helper.rb
|
data/lib/random_jpg/loader.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require "net/http"
|
2
|
-
require "json"
|
3
|
-
|
4
|
-
module RandomJpg
|
5
|
-
class Loader
|
6
|
-
API_URL = "http://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=66c61b93c4723c7c3a3c519728eac252&per_page=1&extras=url_m&format=json"
|
7
|
-
|
8
|
-
def feed(path)
|
9
|
-
response = Net::HTTP.get URI(API_URL)
|
10
|
-
hash = JSON.parse response[14..-2]
|
11
|
-
image_data = Net::HTTP.get URI(hash["photos"]["photo"].first["url_m"])
|
12
|
-
|
13
|
-
File.open(path, "w") do |f|
|
14
|
-
f.write image_data
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|