visioner 0.3.0 → 1.0.0

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: dfb460875ffcf77f13a494ce40b05e1dc089fc9c
4
- data.tar.gz: ba23dfdae48c61b58fc88e8bda5c3d0e15c9de1c
3
+ metadata.gz: 751787ddc448eff5b61ed2980e6019dd754d2c7c
4
+ data.tar.gz: 68b9330468e2a53a066d771de4fbfd5f58573fdd
5
5
  SHA512:
6
- metadata.gz: 72d6f000b32cbbb60675d483b452d35cec3b176cfbcab02eee5b99d94c1c3b331f5a9da8100491813410f53786f9a1517d0b084e5ffc13bbb1b94fce887ef072
7
- data.tar.gz: 050e98a0ef8f60b2d4dd8512fd6388b4db22d5c5c08f3505a996687be1adef043cb0d51f26bec33c0fb86d76384885c5cdfdaaccb8c2876b275dd63ca8b0101e
6
+ metadata.gz: 93b4dbb190ea6cec388289d7cbfe5b36c9ccc4e9038bed721d7ffd0925a72e6e697aedc3908227a842d27c64ae66c7d682efc886aad665e4f35ccd0891258648
7
+ data.tar.gz: c1c07d5fd8b62c1841de6c296fa9236d83a947debf525ab53c8b3317151b3f79490eaaf802c327993969d03ffaf28b6ada01a5ec5867c5be7a0a9788679bcfdb
data/README.md CHANGED
@@ -14,32 +14,29 @@ Automatically rename your pictures using Google Vision API and metadata.
14
14
 
15
15
  **Options:**
16
16
 
17
- -c, --country Adds country to the new filename (ex: south-korea)
18
-
19
- -i, --city Adds city to the new filename (ex: seoul)
20
-
21
- -d, --date Adds date to the new filename (ex: 29/07/2016)
17
+ -f, --format FORMAT Desired format for the new filenames
22
18
 
23
19
  -h, --help Displays help
24
20
 
21
+ Format directives:
22
+ label - uses google vision API to find a label
23
+ locality - uses metadata to find the locality / city
24
+ country - uses metadata to find the country
25
+ date - uses metadata to find the original date
26
+ Format example: date_country_label
27
+
25
28
  **Examples:**
26
29
 
27
30
  $ export GOOGLE_VISION_API_KEY=BI34SyB5DhqV5ReVnkmIM79812yux9UFazNdynD
28
31
 
29
- $ bin/visioner /Desktop/travel-south-korea-2016-2/*.jpg
32
+ $ bin/visioner --format label /Desktop/travel-south-korea-2016-2/*.jpg
30
33
  /Desktop/travel-south-korea-2016-2/IMG_213.jpg -> sea.jpg
31
34
  /Desktop/travel-south-korea-2016-2/IMG_214.jpg -> tower.jpg
32
35
  /Desktop/travel-south-korea-2016-2/IMG_215.jpg -> people.jpg
33
36
  /Desktop/travel-south-korea-2016-2/IMG_216.jpg -> sea2.jpg
34
37
 
35
- $ bin/visioner --date /Desktop/travel-south-korea-2016-3/*.jpg
36
- /Desktop/travel-south-korea-2016-3/IMG_213.jpg -> 02-28-2015_sea.jpg
37
- /Desktop/travel-south-korea-2016-3/IMG_214.jpg -> 02-15-2015_tower.jpg
38
- /Desktop/travel-south-korea-2016-3/IMG_215.jpg -> 03-28-2015_people.jpg
39
- /Desktop/travel-south-korea-2016-3/IMG_216.jpg -> 04-02-2015_sea2.jpg
40
-
41
- $ bin/visioner --country --date /Desktop/travel-south-korea-2016-4/*.jpg
42
- /Desktop/travel-south-korea-2016-4/IMG_213.jpg -> south-korea_02-28-2015_sea.jpg
43
- /Desktop/travel-south-korea-2016-4/IMG_214.jpg -> south-korea_03-15-2015_tower.jpg
44
- /Desktop/travel-south-korea-2016-4/IMG_215.jpg -> south-korea_03-28-2015_people.jpg
45
- /Desktop/travel-south-korea-2016-4/IMG_216.jpg -> south-korea_04-02-2015_sea2.jpg
38
+ $ bin/visioner --format country_locality_date_label /Desktop/travel-south-korea-2016-4/*.jpg
39
+ /Desktop/travel-south-korea-2016-4/IMG_213.jpg -> south-korea_seoul_02-28-2015_sea.jpg
40
+ /Desktop/travel-south-korea-2016-4/IMG_214.jpg -> south-korea_seoul_03-15-2015_tower.jpg
41
+ /Desktop/travel-south-korea-2016-4/IMG_215.jpg -> south-korea_seoul_03-28-2015_people.jpg
42
+ /Desktop/travel-south-korea-2016-4/IMG_216.jpg -> south-korea_seoul_04-02-2015_sea2.jpg
data/bin/visioner CHANGED
@@ -6,18 +6,12 @@ require 'optparse'
6
6
  options = {}
7
7
  opts = OptionParser.new
8
8
 
9
- opts.banner = "Visioner automatically renames your pictures using Google API Visioner and metadata.\nMake sure you have set GOOGLE_API_KEY in your env.\n\nUsage: visioner [options] image ..."
9
+ opts.banner = "Visioner automatically renames your pictures using Google API Visioner and metadata.\n"
10
+ opts.banner = opts.banner + "Make sure you have set GOOGLE_API_KEY in your env.\n"
11
+ opts.banner = opts.banner + "Usage: visioner [options] image ..."
10
12
 
11
- opts.on("-c", "--country", "Adds country to the new filename (ex: south-korea)") do |c|
12
- options[:country] = c
13
- end
14
-
15
- opts.on("-l", "--locality", "Adds locality / city to the new filename (ex: seoul)") do |l|
16
- options[:locality] = l
17
- end
18
-
19
- opts.on("-d", "--date", "Adds date to the new filename (ex: 29/07/2016)") do |d|
20
- options[:date] = d
13
+ opts.on("-f", "--format FORMAT", "Desired format for the new filenames") do |f|
14
+ options[:format] = f
21
15
  end
22
16
 
23
17
  opts.on("-h", "--help", "Displays help") do
@@ -27,7 +21,17 @@ end
27
21
 
28
22
  opts.parse!
29
23
 
30
- if ARGV.empty?
24
+ if options[:format].nil?
25
+ puts "Missing option: format"
26
+ puts "Format directives:"
27
+ puts " label - uses google vision API to find a label"
28
+ puts " locality - uses metadata to find the locality / city"
29
+ puts " country - uses metadata to find the country"
30
+ puts " date - uses metadata to find the original date"
31
+ puts "Format example: date_country_label"
32
+ puts opts
33
+ exit 1
34
+ elsif ARGV.empty?
31
35
  puts opts
32
36
  exit 1
33
37
  else
@@ -1,3 +1,3 @@
1
1
  module Visioner
2
- VERSION = "0.3.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/visioner.rb CHANGED
@@ -32,6 +32,52 @@ module Visioner
32
32
  return place
33
33
  end
34
34
 
35
+ def self.get_label(image_name)
36
+ # Convert image to Base 64
37
+ begin
38
+ b64_data = Base64.encode64(File.open(image_name, "rb").read)
39
+ rescue
40
+ puts "Error: can't read file. Exiting..."
41
+ end
42
+
43
+ # Prepare request
44
+ api_key = ENV['GOOGLE_API_KEY']
45
+ content_type = "Content-Type: application/json"
46
+ url = "https://vision.googleapis.com/v1/images:annotate?key=#{api_key}"
47
+ data = {
48
+ "requests": [
49
+ {
50
+ "image": {
51
+ "content": b64_data
52
+ },
53
+ "features": [
54
+ {
55
+ "type": "LABEL_DETECTION",
56
+ "maxResults": 1
57
+ }
58
+ ]
59
+ }
60
+ ]
61
+ }.to_json
62
+ url = URI(url)
63
+ req = Net::HTTP::Post.new(url, initheader = {'Content-Type' =>'application/json'})
64
+ req.body = data
65
+ res = Net::HTTP.new(url.host, url.port)
66
+ res.use_ssl = true
67
+
68
+ label = 'unknown'
69
+ res.start do |http|
70
+ resp = http.request(req)
71
+ json = JSON.parse(resp.body)
72
+ if json && json["responses"] && json["responses"][0]["labelAnnotations"] && json["responses"][0]["labelAnnotations"][0]["description"]
73
+ label = json['responses'][0]['labelAnnotations'][0]['description']
74
+ label = label.tr(" ", "-")
75
+ end
76
+ end
77
+
78
+ return label
79
+ end
80
+
35
81
  def self.rename_all(images, options)
36
82
  images.each do |image_name|
37
83
 
@@ -41,81 +87,46 @@ module Visioner
41
87
  next
42
88
  end
43
89
 
44
-
45
- # Convert image to Base 64
46
- begin
47
- b64_data = Base64.encode64(File.open(image_name, "rb").read)
48
- rescue
49
- puts "Error: can't read file. Exiting..."
90
+ label = ''
91
+ if options[:format].include? 'label'
92
+ label = 'unknown' # Fallback
93
+ label = self.get_label(image_name)
50
94
  end
51
95
 
52
- # Prepare request
53
- api_key = ENV['GOOGLE_API_KEY']
54
- content_type = "Content-Type: application/json"
55
- url = "https://vision.googleapis.com/v1/images:annotate?key=#{api_key}"
56
- data = {
57
- "requests": [
58
- {
59
- "image": {
60
- "content": b64_data
61
- },
62
- "features": [
63
- {
64
- "type": "LABEL_DETECTION",
65
- "maxResults": 1
66
- }
67
- ]
68
- }
69
- ]
70
- }.to_json
71
- url = URI(url)
72
- req = Net::HTTP::Post.new(url, initheader = {'Content-Type' =>'application/json'})
73
- req.body = data
74
- res = Net::HTTP.new(url.host, url.port)
75
- res.use_ssl = true
76
-
77
- # Rename file
78
- name = ""
79
- res.start do |http|
80
- resp = http.request(req)
81
- json = JSON.parse(resp.body)
82
- if json && json["responses"] && json["responses"][0]["labelAnnotations"] && json["responses"][0]["labelAnnotations"][0]["description"]
83
- name = json['responses'][0]['labelAnnotations'][0]['description']
84
- name = name.tr(" ", "-")
85
- end
96
+ date = ''
97
+ if options[:format].include? 'date'
98
+ date = File.mtime(image_name).strftime('%m-%d-%Y') # Fallback
99
+ exif = EXIFR::JPEG.new(image_name)
100
+ date = exif.date_time_original.strftime('%m-%d-%Y') if exif && exif.date_time_original
86
101
  end
87
102
 
88
- unless name.empty?
89
- counter = nil
90
- while File.exist?(File.dirname(image_name) + "/" + name + counter.to_s + File.extname(image_name)) do
91
- counter = 1 if counter == nil
92
- counter = counter.to_i + 1
93
- end
94
-
103
+ country = ''
104
+ if options[:format].include? 'country'
105
+ country = 'unknown' # Fallback
95
106
  exif = EXIFR::JPEG.new(image_name)
107
+ country = self.get_place(exif.gps.latitude, exif.gps.longitude, 'country') if exif && exif.gps_latitude && exif.gps_longitude
108
+ end
96
109
 
97
- date = ''
98
- if options[:date]
99
- date = File.mtime(image_name).strftime('%m-%d-%Y') # Fallback
100
- date = exif.date_time_original.strftime('%m-%d-%Y') + '_' if exif.date_time_original
101
- end
110
+ locality = ''
111
+ if options[:format].include? 'locality'
112
+ locality = 'unknown' # Fallback
113
+ locality = self.get_place(exif.gps.latitude, exif.gps.longitude, 'locality') if exif.gps_latitude && exif.gps_longitude
114
+ end
102
115
 
103
- country = ''
104
- if options[:country]
105
- country = 'unknown' # Fallback
106
- country = self.get_place(exif.gps.latitude, exif.gps.longitude, 'country') + '_' if exif.gps_latitude && exif.gps_longitude
107
- end
116
+ options[:format].sub! 'label', label
117
+ options[:format].sub! 'date', date
118
+ options[:format].sub! 'locality', locality
119
+ options[:format].sub! 'country', country
108
120
 
109
- locality = ''
110
- if options[:locality]
111
- locality = 'unknown' # Fallback
112
- locality = self.get_place(exif.gps.latitude, exif.gps.longitude, 'locality') + '_' if exif.gps_latitude && exif.gps_longitude
113
- end
121
+ counter = nil
122
+ while File.exist?(File.dirname(image_name) + "/" + options[:format] + counter.to_s + File.extname(image_name)) do
123
+ counter = 1 if counter == nil
124
+ counter = counter.to_i + 1
125
+ end
114
126
 
115
- puts "#{image_name} -> #{country + locality + date + name + counter.to_s + File.extname(image_name)}"
127
+ puts "#{image_name} -> #{options[:format] + counter.to_s + File.extname(image_name)}"
116
128
 
117
- File.rename(image_name, File.dirname(image_name) + "/" + country + locality + date + name + counter.to_s + File.extname(image_name))
118
- end
129
+ File.rename(image_name, File.dirname(image_name) + "/" + options[:format] + counter.to_s + File.extname(image_name))
119
130
 
120
131
  end
121
132
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visioner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre-Jean Bergeron