unsplash_downloader 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2333337629e053100c63d50f2e44831d99390960
4
+ data.tar.gz: 13375fbafb6cdaa7e72ff80b90bd9c3e3a01d2e4
5
+ SHA512:
6
+ metadata.gz: 7d8b9b9f5da1659431653c7af0c0c53bc16a69a6c0b017ec95c2605c8681d3df669c1ea482a4ef53ec8c0a379f79a39abe030ebb3f4f080eb269abdbf349589b
7
+ data.tar.gz: c6008fc0262187e321d6ce82cf27f9ad5b96a24bb59c656604c97662d822c870207be4855157d05122ef501db88765c98cf32c51af068f6714608ee9ce2f02dc
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /unsplash/
11
+ *.gem
12
+ *.rbc
13
+ /.config
14
+ /coverage/
15
+ /InstalledFiles
16
+ /pkg/
17
+ /spec/reports/
18
+ /test/tmp/
19
+ /test/version_tmp/
20
+ /tmp/
21
+
22
+ ## Specific to RubyMotion:
23
+ .dat*
24
+ .repl_history
25
+ build//unsplash/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unsplash_downloader.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Bartłomiej Jacak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # UnsplashDownloader
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/unsplash_downloader`. 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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'unsplash_downloader'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install unsplash_downloader
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. 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]/unsplash_downloader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
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
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "unsplash_downloader"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'unsplash_downloader'
4
+
5
+ UnsplashDownloader::CLI.start(ARGV)
@@ -0,0 +1,122 @@
1
+ module UnsplashDownloader
2
+ class Unsplash
3
+
4
+ attr_accessor :number_of_pages
5
+ attr_accessor :photos_urls
6
+
7
+ def initialize(verbose)
8
+ @mechanize = Mechanize.new
9
+ @all_elements = []
10
+ @source = get_source
11
+ @number_of_pages = count_pages
12
+ @verbose = verbose
13
+ end
14
+
15
+ def get_source
16
+ @mechanize.get('https://unsplash.com/')
17
+ end
18
+
19
+ def count_pages
20
+ @pagination = @source.search('div.pagination')
21
+ @pagination.to_s.match(/(\d+)<\/a> <a class="next_page"/i).captures
22
+ end
23
+
24
+ def get_single_page_elements(number)
25
+ @single_page_elements = @mechanize.get("https://unsplash.com/?page=#{number}").search('div.photo')
26
+ end
27
+
28
+ def merge_all_elements
29
+ for index in 1..@number_of_pages[0].to_i + 1 do
30
+ print "Merging pages: #{index - 1} of #{@number_of_pages[0]}" if @verbose
31
+ get_single_page_elements index
32
+ @single_page_elements.each do |element|
33
+ @all_elements.unshift(element)
34
+ end
35
+ print "\r" if @verbose
36
+ end
37
+ puts "Merging pages: #{@number_of_pages[0]} of #{@number_of_pages[0]}. Done!" if @verbose
38
+ return @all_elements
39
+ end
40
+ end
41
+
42
+ class Photos
43
+ attr_accessor :number_of_photos
44
+ attr_accessor :number_of_new_photos
45
+
46
+ def initialize(path, verbose)
47
+ @mechanize = Mechanize.new
48
+ @photos_urls = []
49
+ @new_photos = []
50
+ @path = path
51
+ @verbose = verbose
52
+ end
53
+
54
+ def get_urls(elements)
55
+ elements.each do |element|
56
+ @photos_urls.push(/https[^\?]*/.match(element.to_s))
57
+ end
58
+ return @photos_urls
59
+ end
60
+
61
+ def save_urls
62
+ FileUtils.mkdir_p(File.join(@path, "unsplash")) unless File.directory?(File.join(@path, "unsplash"))
63
+ file = File.open(File.join(@path, "unsplash", "urls.txt"), "wb") do |line|
64
+ @photos_urls.each do |url|
65
+ line.puts url
66
+ end
67
+ end
68
+ end
69
+
70
+ def count_all
71
+ @number_of_photos = 0
72
+ @photos_urls.each do |line|
73
+ @number_of_photos += 1
74
+ end
75
+ return @number_of_photos
76
+ end
77
+
78
+ def count_all_from_file
79
+ @number_of_photos = 0
80
+ file = File.open(File.join(@path, "unsplash", "urls.txt"))
81
+ file.each do |line|
82
+ @number_of_photos += 1
83
+ end
84
+ return @number_of_photos
85
+ end
86
+
87
+ def count_new
88
+ @new_photos = []
89
+ for i in 1..@number_of_photos
90
+ File.exist?(File.join(@path, "unsplash", "#{i}.jpg")) ? next : @new_photos.push(i)
91
+ end
92
+ @number_of_new_photos = @new_photos.length
93
+ return @number_of_new_photos
94
+ end
95
+
96
+ def download_all
97
+ file = File.open(File.join(@path, "unsplash", "urls.txt"))
98
+ downloaded_photos = 0
99
+ puts "Downloaded #{downloaded_photos} of #{@number_of_photos}." if @verbose
100
+ file.each_with_index do |line, index|
101
+ @mechanize.get(line).save! File.join(@path, "unsplash", "#{index + 1}.jpg")
102
+ downloaded_photos += 1
103
+ print "Downloaded #{downloaded_photos} of #{@number_of_photos}. #{line}" if @verbose
104
+ end
105
+ end
106
+
107
+ def download_new
108
+ file = File.open(File.join(@path, "unsplash", "urls.txt"))
109
+ downloaded_photos = 0
110
+ puts "Downloaded #{downloaded_photos} of #{@number_of_new_photos}." if @verbose
111
+ file.each_with_index do |line, index|
112
+ if @new_photos.include?(index + 1)
113
+ @mechanize.get(line).save File.join(@path, "unsplash", "#{index + 1}.jpg")
114
+ downloaded_photos += 1
115
+ print "Downloaded #{downloaded_photos} of #{@number_of_new_photos}. #{line}" if @verbose
116
+ else
117
+ next
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,3 @@
1
+ module UnsplashDownloader
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,108 @@
1
+ require "unsplash_downloader/version"
2
+ require "unsplash_downloader/unsplash"
3
+
4
+ require "thor"
5
+ require "mechanize"
6
+ require "fileutils"
7
+
8
+ module UnsplashDownloader
9
+ class CLI < Thor
10
+
11
+ option :verbose, :type => :boolean
12
+ option :all, :type => :boolean
13
+
14
+ desc "download PATH [--all] [--verbose]", "Downloads photos to `PATH/unsplash`"
15
+ long_desc <<-LONGDESC
16
+ `download PATH [--all] [--verbose]` will download photos.
17
+ Photos will be saved in `PATH/unsplash`. Defaultly PATH is current dir path.
18
+ Without -all flag it downloads only new photos that are on Unsplash and aren't in folder.
19
+ If `PATH/unsplash` dir doesn't exist it creates dir and downloads all photos.
20
+
21
+ Optionally you can use relative path and there will be created new folder.
22
+
23
+ > $ unsplash_downloader download photos-folder
24
+
25
+ Use --all flag to force download all photos.
26
+ It will overwrite all previously downloaded photos in this folder!
27
+
28
+ > $ unsplash_downloader download photos-folder --all
29
+
30
+ Use --verbose flag to show info about work progress.
31
+
32
+ > $ unsplash_downloader download photos-folder --verbose
33
+ LONGDESC
34
+
35
+ def download(path = Dir.pwd)
36
+ path = File.join(Dir.pwd, path) if path != Dir.pwd
37
+ verbose = options[:verbose]
38
+ unsplash = Unsplash.new(options[:verbose])
39
+ photos = Photos.new(path, options[:verbose])
40
+ unsplash.get_source
41
+ puts "Getting Unsplash source." if verbose
42
+ unsplash.count_pages
43
+ print "Counting pages.." if verbose
44
+ puts unsplash.number_of_pages if verbose
45
+ photos.get_urls(unsplash.merge_all_elements)
46
+ photos.save_urls
47
+ puts "Saving urls to `#{path}/unsplash/unsplash_urls.txt`.." if verbose
48
+ photos.count_all
49
+ puts "There are #{photos.number_of_photos} photos on Unsplash." if verbose
50
+ if options[:all]
51
+ puts "Downloading.." if verbose
52
+ photos.download_all
53
+ else
54
+ number_of_new_photos = photos.count_new
55
+ puts "You don't have #{number_of_new_photos} photos in `#{path}/unsplash` folder." if verbose
56
+ puts "Downloading.." if verbose
57
+ photos.download_new
58
+ end
59
+ puts 'Done!' if verbose
60
+ end
61
+
62
+ desc "get_urls PATH", "Gets photos urls to `urls.txt` file"
63
+
64
+
65
+ desc "count_new PATH", "Counts new photos in `PATH/unsplash` dir. Defaultly PATH is current dir path."
66
+ def count_new(path = Dir.pwd)
67
+ path = File.join(Dir.pwd, path) if path != Dir.pwd
68
+ if File.exist?(File.join(path, "unsplash", "urls.txt"))
69
+ photos = Photos.new(path, false)
70
+ photos.count_all_from_file
71
+ puts "You don't have #{photos.count_new} photos in `#{path}/unsplash` folder."
72
+ else
73
+ puts "File `urls.txt` not found. Creating.."
74
+ unsplash = Unsplash.new(false)
75
+ photos = Photos.new(path, false)
76
+ unsplash.get_source
77
+ unsplash.count_pages
78
+ urls = photos.get_urls(unsplash.merge_all_elements)
79
+ photos.save_urls
80
+ photos.count_all
81
+ puts "You don't have #{photos.count_new} photos in `#{path}/unsplash` folder."
82
+ end
83
+ end
84
+
85
+ desc "count_all", "Counts all Unplash photos. "
86
+ def count_all(path = Dir.pwd)
87
+ path = File.join(Dir.pwd, path) if path != Dir.pwd
88
+ unsplash = Unsplash.new(false)
89
+ photos = Photos.new(path, false)
90
+ puts "Counting.."
91
+ unsplash.get_source
92
+ unsplash.count_pages
93
+ photos.get_urls(unsplash.merge_all_elements)
94
+ puts "There are #{photos.count_all} Unsplash photos."
95
+ end
96
+
97
+ desc "get_urls PATH", "Gets urls to `PATH/unsplash/urls.txt`"
98
+ def get_urls(path = Dir.pwd)
99
+ path = File.join(Dir.pwd, path) if path != Dir.pwd
100
+ unsplash = Unsplash.new(false)
101
+ photos = Photos.new(path, false)
102
+ unsplash.get_source
103
+ unsplash.count_pages
104
+ urls = photos.get_urls(unsplash.merge_all_elements)
105
+ puts urls
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unsplash_downloader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unsplash_downloader"
8
+ spec.version = UnsplashDownloader::VERSION
9
+ spec.authors = ["Barlomiej Jacak"]
10
+ spec.email = ["bartek.jacak@gmail.com"]
11
+
12
+ spec.summary = %q{Unsplash downloader written in Ruby.}
13
+ spec.homepage = "https://github.com/xennon/unsplash-downloader"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_dependency "mechanize", "~> 2.7.0"
24
+ spec.add_dependency "thor"
25
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unsplash_downloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Barlomiej Jacak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mechanize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.7.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.7.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - bartek.jacak@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - bin/unsplash_downloader
84
+ - lib/unsplash_downloader.rb
85
+ - lib/unsplash_downloader/unsplash.rb
86
+ - lib/unsplash_downloader/version.rb
87
+ - unsplash_downloader.gemspec
88
+ homepage: https://github.com/xennon/unsplash-downloader
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.15
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Unsplash downloader written in Ruby.
112
+ test_files: []