visioner 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e13126577086c8113fd97ddaa358503ad72a8fae
4
- data.tar.gz: 07a31b3248cb29b8e5b67b8ffedf9cde2fd55924
3
+ metadata.gz: 3ad07e8c2e46392159f5c62a4a956d4e646ab4f3
4
+ data.tar.gz: fcc1116040567630a84f1c96c4f94d6c899d5bee
5
5
  SHA512:
6
- metadata.gz: d90f9c44dc00aae708ab3ac64eea5019e906ea2b1aa628301a0446caeb468f40a28ca5e29312401b4978694dacff50cafd495089fd2cf647b62f716d60cce3e5
7
- data.tar.gz: 2bca6f64ac31dfc1e0152de4e7f67d6baa5113d7636c05313001b3e110713d5d2868fb02908185c78796443a3925d17aa2e7d7aa6adeeda7fd472f538e4e650b
6
+ metadata.gz: 91018c0cd1ef95db035ee992d996c75f6a7dcf17df4b51a4127ec90e5d421d5c5f1b7b52a27eca92c625f8a3718c7c5162c3ff2ed4243c48d9eddf8596c711b7
7
+ data.tar.gz: a5482df04d15ddba0fa81fca3dfec6700377b6b8f0594dd15290a52c25e3681ae46b8b795fe81399de55e19ad8437da2e2f89daf08503eb83c28a11a8f6f67aa
data/README.md CHANGED
@@ -1,41 +1,43 @@
1
1
  # Visioner
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/visioner`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Automatically rename your pictures using Google Vision API and metadata.
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'visioner'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install visioner
7
+ $ bundle install
8
+ $ rake install
22
9
 
23
10
  ## Usage
24
11
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/visioner.
36
-
37
-
38
- ## License
39
-
40
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
12
+ $ export GOOGLE_VISION_API_KEY=...
13
+ $ bin/visioner [options] image ...
14
+
15
+ **Options:**
16
+
17
+ -c, --country Adds country to the new filename (ex: south-korea)
18
+
19
+ -d, --date Adds date to the new filename (ex: 29/07/2016)
20
+
21
+ -h, --help Displays help
22
+
23
+ **Examples:**
24
+
25
+ $ export GOOGLE_VISION_API_KEY=BI34SyB5DhqV5ReVnkmIM79812yux9UFazNdynD
26
+
27
+ $ bin/visioner /Desktop/travel-south-korea-2016-2/*.jpg
28
+ /Desktop/travel-south-korea-2016-2/IMG_213.jpg -> sea.jpg
29
+ /Desktop/travel-south-korea-2016-2/IMG_214.jpg -> tower.jpg
30
+ /Desktop/travel-south-korea-2016-2/IMG_215.jpg -> people.jpg
31
+ /Desktop/travel-south-korea-2016-2/IMG_216.jpg -> sea2.jpg
32
+
33
+ $ bin/visioner --date /Desktop/travel-south-korea-2016-3/*.jpg
34
+ /Desktop/travel-south-korea-2016-3/IMG_213.jpg -> 02-28-2015_sea.jpg
35
+ /Desktop/travel-south-korea-2016-3/IMG_214.jpg -> 02-15-2015_tower.jpg
36
+ /Desktop/travel-south-korea-2016-3/IMG_215.jpg -> 03-28-2015_people.jpg
37
+ /Desktop/travel-south-korea-2016-3/IMG_216.jpg -> 04-02-2015_sea2.jpg
38
+
39
+ $ bin/visioner --country --date /Desktop/travel-south-korea-2016-4/*.jpg
40
+ /Desktop/travel-south-korea-2016-4/IMG_213.jpg -> south-korea_02-28-2015_sea.jpg
41
+ /Desktop/travel-south-korea-2016-4/IMG_214.jpg -> south-korea_03-15-2015_tower.jpg
42
+ /Desktop/travel-south-korea-2016-4/IMG_215.jpg -> south-korea_03-28-2015_people.jpg
43
+ /Desktop/travel-south-korea-2016-4/IMG_216.jpg -> south-korea_04-02-2015_sea2.jpg
data/bin/visioner CHANGED
@@ -1,11 +1,31 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'visioner'
4
+ require 'optparse'
5
+
6
+ options = {}
7
+ opts = OptionParser.new
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 ..."
10
+
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("-d", "--date", "Adds date to the new filename (ex: 29/07/2016)") do |d|
16
+ options[:date] = d
17
+ end
18
+
19
+ opts.on("-h", "--help", "Displays help") do
20
+ puts opts
21
+ exit
22
+ end
23
+
24
+ opts.parse!
4
25
 
5
26
  if ARGV.empty?
6
- puts "Visioner automatically renames your pictures using Google API Visioner"
7
- puts "Usage: visioner image ..."
27
+ puts opts
8
28
  exit 1
9
29
  else
10
- Visioner.rename_all(ARGV)
30
+ Visioner.rename_all(ARGV, options)
11
31
  end
@@ -1,3 +1,3 @@
1
1
  module Visioner
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/visioner.rb CHANGED
@@ -2,21 +2,53 @@ require 'visioner/version'
2
2
  require 'base64'
3
3
  require 'net/http'
4
4
  require 'json'
5
+ require 'exifr'
6
+ require 'optparse'
5
7
 
6
8
  module Visioner
7
-
8
- def self.rename_all(images)
9
+
10
+ def self.get_country(latitude, longitude)
11
+ # Prepare request
12
+ url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=#{latitude},#{longitude}"
13
+ url = URI(url)
14
+ req = Net::HTTP::Get.new(url)
15
+ res = Net::HTTP.new(url.host, url.port)
16
+ res.use_ssl = true
17
+
18
+ # Get country
19
+ country = 'unknown'
20
+ res.start do |http|
21
+ resp = http.request(req)
22
+ json = JSON.parse(resp.body)
23
+ if json && json['status'] == 'OK' && json['results'][0]['address_components'] && json['results'][0]['address_components'].select {|address_component| address_component['types'][0] == 'country' }
24
+ country = json['results'][0]['address_components'].select {|address_component| address_component['types'][0] == 'country' }
25
+ country = country[0]['long_name']
26
+ country = country.downcase.tr(" ", "-")
27
+ end
28
+ end
29
+
30
+ return country
31
+ end
32
+
33
+ def self.rename_all(images, options)
9
34
  images.each do |image_name|
10
35
 
36
+ # Check file extension
37
+ if File.extname(image_name) != '.jpg'
38
+ puts "Error: can only rename jpg files. Continuing..."
39
+ next
40
+ end
41
+
42
+
11
43
  # Convert image to Base 64
12
44
  begin
13
45
  b64_data = Base64.encode64(File.open(image_name, "rb").read)
14
46
  rescue
15
- puts "Error: can't read file"
47
+ puts "Error: can't read file. Exiting..."
16
48
  end
17
49
 
18
50
  # Prepare request
19
- api_key = ENV['GOOGLE_VISION_API_KEY']
51
+ api_key = ENV['GOOGLE_API_KEY']
20
52
  content_type = "Content-Type: application/json"
21
53
  url = "https://vision.googleapis.com/v1/images:annotate?key=#{api_key}"
22
54
  data = {
@@ -34,8 +66,6 @@ module Visioner
34
66
  }
35
67
  ]
36
68
  }.to_json
37
-
38
- # Make request
39
69
  url = URI(url)
40
70
  req = Net::HTTP::Post.new(url, initheader = {'Content-Type' =>'application/json'})
41
71
  req.body = data
@@ -49,18 +79,34 @@ module Visioner
49
79
  json = JSON.parse(resp.body)
50
80
  if json && json["responses"] && json["responses"][0]["labelAnnotations"] && json["responses"][0]["labelAnnotations"][0]["description"]
51
81
  name = json['responses'][0]['labelAnnotations'][0]['description']
82
+ name = name.tr(" ", "-")
52
83
  end
53
84
  end
54
85
 
55
86
  unless name.empty?
56
- counter = ''
87
+ counter = nil
57
88
  while File.exist?(File.dirname(image_name) + "/" + name + counter.to_s + File.extname(image_name)) do
89
+ counter = 1 if counter == nil
58
90
  counter = counter.to_i + 1
59
91
  end
60
92
 
61
- puts "#{image_name} -> #{name + counter.to_s + File.extname(image_name)}"
93
+ exif = EXIFR::JPEG.new(image_name)
94
+
95
+ date = ''
96
+ if options[:date]
97
+ date = File.mtime(image_name).strftime('%m-%d-%Y') # Fallback
98
+ date = exif.date_time_original.strftime('%m-%d-%Y') + '_' if exif.date_time_original
99
+ end
100
+
101
+ country = ''
102
+ if options[:country]
103
+ country = 'unknown' # Fallback
104
+ country = self.get_country(exif.gps.latitude, exif.gps.longitude) + '_' if exif.gps_latitude && exif.gps_longitude
105
+ end
106
+
107
+ puts "#{image_name} -> #{country + date + name + counter.to_s + File.extname(image_name)}"
62
108
 
63
- File.rename(image_name, File.dirname(image_name) + "/" + name + counter.to_s + File.extname(image_name))
109
+ File.rename(image_name, File.dirname(image_name) + "/" + country + date + name + counter.to_s + File.extname(image_name))
64
110
  end
65
111
 
66
112
  end
data/visioner.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Pierre-Jean Bergeron"]
10
10
  spec.email = ["pj@bergeron.io"]
11
11
 
12
- spec.summary = %q{Automatically rename your pictures using Google Vision API.}
12
+ spec.summary = %q{Automatically rename your pictures using Google Vision API and metadata.}
13
13
  spec.homepage = "https://github.com/berpj/visioner"
14
14
  spec.license = "MIT"
15
15
 
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.11"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "exifr", "~> 1.2"
23
24
  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.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre-Jean Bergeron
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: exifr
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
41
55
  description:
42
56
  email:
43
57
  - pj@bergeron.io
@@ -82,5 +96,5 @@ rubyforge_project:
82
96
  rubygems_version: 2.5.1
83
97
  signing_key:
84
98
  specification_version: 4
85
- summary: Automatically rename your pictures using Google Vision API.
99
+ summary: Automatically rename your pictures using Google Vision API and metadata.
86
100
  test_files: []