flickrage 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9ee1a397bd1661afccb8def2e52abca69b50eec
4
- data.tar.gz: 763e29b08d260383ca77c1b6ed45c9884bc44dfb
3
+ metadata.gz: 847af86d9e43d42df3b1b988ac8a0d9bff7aff75
4
+ data.tar.gz: 590ec13f794cb3a22b1d2182ddf76088e147c8c8
5
5
  SHA512:
6
- metadata.gz: 59850dd2bd9e17027df0c114328830fe78f97f965077c71af40747864844f3b8ff308244f114a707af5cfc7c6506f68b112b7487046e344f4597cd6a90fb945d
7
- data.tar.gz: c4208d19727f769550235a01c9f8f637a08785b39be5dad142d56011b8c99eeabb688a85e38da4eb746233e59d9aebf6ec78b0adf99b77f9550dcc1acfe05b20
6
+ metadata.gz: de533e78326980bf3e4f91fd0eea9eab3c062c930c5cba58b5e856f7e856a9b0543bdb0f0779b1f1b82c6af5ab9709dbf4a0a3b37c053002be2bd6040d3a432d
7
+ data.tar.gz: 5fa397e6a465cd2d82ca5c94c9d82f48edead68da0541001b2ee80cf2dce12776fa90b808e39e7496953ec3a6659242407f6df11b4e2e965e5eff023014be948
data/README.md CHANGED
@@ -93,6 +93,7 @@ Provide your own words dictionary:
93
93
  [--file-name=./some.png] # Name for the file with collage.
94
94
  [--dict-path=/usr/share/dict/words] # Path to the file with multiline words (dictionary).
95
95
  -c, [--cleanup], [--no-cleanup] # Cleanup files before collage.
96
+ -t, [--tagged-search], [--no-tagged-search] # Search by tags.
96
97
  -v, [--verbose], [--no-verbose] # Verbose mode.
97
98
  -q, [--quiet], [--no-quiet] # Quiet mode. If don't need any messages and in console.
98
99
  [--flickr-api-key=YOURLONGAPIKEY] # FLICKR_API_KEY. if you can't use environment.
data/lib/flickrage.rb CHANGED
@@ -31,6 +31,8 @@ module Flickrage
31
31
  setting :pool_size, 5
32
32
  setting :pool
33
33
 
34
+ setting :tagged_search, false
35
+
34
36
  setting :search_timeout, 30
35
37
  setting :download_timeout, 10
36
38
 
data/lib/flickrage/cli.rb CHANGED
@@ -111,6 +111,11 @@ module Flickrage
111
111
  type: :boolean,
112
112
  aliases: %w(-c),
113
113
  desc: 'Cleanup files before collage.'
114
+ method_option :tagged_search,
115
+ default: false,
116
+ aliases: %w(-t),
117
+ type: :boolean,
118
+ desc: 'Search by tags.'
114
119
  method_option :verbose,
115
120
  type: :boolean,
116
121
  aliases: %w(-v),
@@ -200,6 +205,8 @@ module Flickrage
200
205
  config.max = options['max'] if options['max']
201
206
  config.grid = options['grid'] if options['grid']
202
207
  config.output = options['output']
208
+
209
+ config.tagged_search = options['tagged_search']
203
210
  end
204
211
  end
205
212
 
@@ -9,7 +9,7 @@ module Flickrage
9
9
 
10
10
  def run(image)
11
11
  uri = gen_uri(image.url)
12
- image.file_name = file_name(uri)
12
+ image.file_name = file_name(uri) unless image.file_name
13
13
  download_file(uri, image.local_path)
14
14
  check_image(image)
15
15
  rescue StandardError => e
@@ -19,45 +19,53 @@ module Flickrage
19
19
 
20
20
  private
21
21
 
22
- def download_file(uri, path, limit = Flickrage.config.download_timeout)
22
+ def download_file(uri, path, mode = 'wb')
23
+ File.open(path, mode) do |file|
24
+ file.flock(File::LOCK_EX)
25
+ parse_file(uri, file)
26
+ end
27
+ rescue => e
28
+ FileUtils.rm_f(path) if File.size(path).zero?
29
+ raise e
30
+ end
31
+
32
+ def parse_file(uri, file, limit = Flickrage.config.download_timeout)
23
33
  raise Flickrage::DownloadError, 'Redirect limit arrived' if limit.zero?
24
34
 
25
35
  Net::HTTP.start(uri.host, uri.port,
26
36
  use_ssl: uri.scheme == 'https') do |conn|
27
- request = Net::HTTP::Get.new(uri)
28
- response = conn.request request
29
-
30
- case response
31
- when Net::HTTPSuccess
32
- write_file(path, response.body)
33
- when Net::HTTPRedirection
34
- download_file(gen_uri(response['location']),
35
- path,
36
- limit - 1)
37
- else
38
- response.error!
37
+ conn.request_get(uri.path) do |response|
38
+ case response
39
+ when Net::HTTPSuccess
40
+ response.read_body do |seg|
41
+ file << seg
42
+ end
43
+ when Net::HTTPRedirection
44
+ download_file(gen_uri(response['location']),
45
+ path,
46
+ limit - 1)
47
+ else
48
+ response.error!
49
+ end
39
50
  end
40
51
  end
41
52
  end
42
53
 
43
- def write_file(path, body, mode = 'wb')
44
- File.open(path, mode) do |file|
45
- file.flock(File::LOCK_EX)
46
- file << body
47
- end
48
- end
49
-
50
54
  def gen_uri(url)
51
55
  URI.parse(url)
52
56
  end
53
57
 
54
58
  def file_name(uri)
55
- File.basename(uri.path)
59
+ "#{nsec}_" + File.basename(uri.path)
56
60
  end
57
61
 
58
62
  def check_image(image)
59
63
  File.exist?(image.local_path) ? image.finish_download : image
60
64
  end
65
+
66
+ def nsec
67
+ Time.now.nsec
68
+ end
61
69
  end
62
70
  end
63
71
  end
@@ -4,6 +4,12 @@ module Flickrage
4
4
  class Search
5
5
  include Flickrage::Helpers::Log
6
6
 
7
+ attr_reader :tagged_search
8
+
9
+ def initialize
10
+ @tagged_search = Flickrage.config.tagged_search
11
+ end
12
+
7
13
  def run(keyword)
8
14
  result = search(keyword)
9
15
 
@@ -31,7 +37,7 @@ module Flickrage
31
37
  end
32
38
 
33
39
  def search(keyword)
34
- flickr.photos.search(params(text: keyword))
40
+ flickr.photos.search(params(search_query(keyword)))
35
41
  end
36
42
 
37
43
  def title(text)
@@ -48,6 +54,14 @@ module Flickrage
48
54
  pages: 1
49
55
  }.merge(opts)
50
56
  end
57
+
58
+ def search_query(keyword)
59
+ if tagged_search
60
+ {tags: [keyword]}
61
+ else
62
+ {text: keyword}
63
+ end
64
+ end
51
65
  end
52
66
  end
53
67
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Flickrage
3
- VERSION = '0.1.5'
3
+ VERSION = '0.1.6'
4
4
  end
@@ -5,7 +5,7 @@ module Flickrage
5
5
  include Flickrage::Helpers::Dict
6
6
 
7
7
  def call
8
- keywords = opts['keywords'] || %w()
8
+ keywords = opts['keywords']&.first(Flickrage.config.max) || %w()
9
9
 
10
10
  keywords += sample_words(Flickrage.config.max - keywords.size) if keywords.size < Flickrage.config.max
11
11
  keys = keywords.join(', ')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flickrage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Merkulov