img_fetcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c1abe50bca687139d46cbe12d5830e8a313ab07b52f4ce8c81476ecf7e5e69d3
4
+ data.tar.gz: e77f3a5e85205efa20f57d7a12bca84372575cee3f1bc40de23b54f6d9bdf7c8
5
+ SHA512:
6
+ metadata.gz: b2847ab87e8bdd12163331e53080d63a0fb9801248a7077639b15aa4b678d4bf2a3cb21908ba8f5c5162eb25dcfffb40a63a747a5cdc36fd07d61e1f77346694
7
+ data.tar.gz: 373c86685036c65156dff95312041688557bc044e7986e8b75c2d2220f802138bba03540404d11f58be1b3b6eb9d2faeeca6af4c3d51f60c3691852a0af5e696
@@ -0,0 +1,25 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@0.1.2
4
+
5
+ jobs:
6
+ build:
7
+ docker:
8
+ - image: circleci/ruby:2.7.2
9
+ executor: ruby/default
10
+ steps:
11
+ - checkout
12
+ - run:
13
+ name: Create folders for specs
14
+ command: mkdir tmp && mkdir spec/support/tmp
15
+ - run:
16
+ name: Install bundle
17
+ command: bundle install
18
+ - run:
19
+ name: Run rubocop
20
+ command: bundle exec rubocop --format simple
21
+ - run:
22
+ name: Run tests
23
+ command: bundle exec rspec spec
24
+ - store_artifacts:
25
+ path: coverage
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .byebug_history
10
+ *.gem
11
+ spec/support/tmp/*
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
15
+
16
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,30 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ SuggestExtensions: false
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
8
+ Layout/LineLength:
9
+ Max: 99
10
+
11
+ Style/FrozenStringLiteralComment:
12
+ Enabled: false
13
+
14
+ Layout/EmptyLineAfterGuardClause:
15
+ Enabled: false
16
+
17
+ Naming/MethodParameterName:
18
+ Enabled: false
19
+
20
+ Metrics/BlockLength:
21
+ Exclude:
22
+ - spec/**/*
23
+
24
+ # rubocop-rspec custom configurations
25
+
26
+ require:
27
+ - rubocop-rspec
28
+
29
+ RSpec/ExampleLength:
30
+ Max: 19
@@ -0,0 +1 @@
1
+ 2.7.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at francoprud@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in img_fetcher.gemspec
4
+ gemspec
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ img_fetcher (0.1.0)
5
+ down (~> 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ ast (2.4.1)
13
+ crack (0.4.4)
14
+ diff-lcs (1.4.4)
15
+ docile (1.3.2)
16
+ down (5.2.0)
17
+ addressable (~> 2.5)
18
+ hashdiff (1.0.1)
19
+ parallel (1.20.1)
20
+ parser (2.7.2.0)
21
+ ast (~> 2.4.1)
22
+ public_suffix (4.0.6)
23
+ rainbow (3.0.0)
24
+ rake (12.3.3)
25
+ regexp_parser (2.0.0)
26
+ rexml (3.2.4)
27
+ rspec (3.9.0)
28
+ rspec-core (~> 3.9.0)
29
+ rspec-expectations (~> 3.9.0)
30
+ rspec-mocks (~> 3.9.0)
31
+ rspec-core (3.9.3)
32
+ rspec-support (~> 3.9.3)
33
+ rspec-expectations (3.9.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-mocks (3.9.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.9.0)
39
+ rspec-support (3.9.3)
40
+ rubocop (1.5.2)
41
+ parallel (~> 1.10)
42
+ parser (>= 2.7.1.5)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml
46
+ rubocop-ast (>= 1.2.0, < 2.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 2.0)
49
+ rubocop-ast (1.3.0)
50
+ parser (>= 2.7.1.5)
51
+ rubocop-rspec (2.0.1)
52
+ rubocop (~> 1.0)
53
+ rubocop-ast (>= 1.1.0)
54
+ ruby-progressbar (1.10.1)
55
+ simplecov (0.20.0)
56
+ docile (~> 1.1)
57
+ simplecov-html (~> 0.11)
58
+ simplecov_json_formatter (~> 0.1)
59
+ simplecov-html (0.12.3)
60
+ simplecov_json_formatter (0.1.2)
61
+ unicode-display_width (1.7.0)
62
+ webmock (3.10.0)
63
+ addressable (>= 2.3.6)
64
+ crack (>= 0.3.2)
65
+ hashdiff (>= 0.4.0, < 2.0.0)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ img_fetcher!
72
+ rake (~> 12.0)
73
+ rspec (~> 3.0)
74
+ rubocop (~> 1.5.2)
75
+ rubocop-rspec (~> 2.0.1)
76
+ simplecov (~> 0.20.0)
77
+ webmock (~> 3.10.0)
78
+
79
+ BUNDLED WITH
80
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Franco Prudhomme
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.
@@ -0,0 +1,96 @@
1
+ # ImgFetcher
2
+
3
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop-hq/rubocop)
4
+ [![francoprud](https://circleci.com/gh/francoprud/img_fetcher.svg?style=shield)](https://github.com/francoprud/img_fetcher)
5
+
6
+ ImgFetcher is a command-line tool that given a plaintext file containing URLs (one per line),
7
+ downloads all of them to the local hard disk.
8
+
9
+ ## Installation
10
+
11
+ From [RubyGems](http://rubygems.org/) you can download it with the following command:
12
+
13
+ ```
14
+ $ gem install img_fetcher
15
+ ```
16
+
17
+ Or, you can clone this repository and build and install the gem by yourself:
18
+
19
+ 1. Clone the repository.
20
+ 2. Install Ruby dependencies:
21
+
22
+ ```
23
+ $ bundle install
24
+ ```
25
+
26
+ 3. Build and install the gem with the Rake command:
27
+ ```
28
+ $ bundle exec rake install
29
+ ```
30
+
31
+ ### For development
32
+
33
+ Temporary folders must be created in order to run the tests, so you must run the following bash command to setup the development environment:
34
+
35
+ ```
36
+ $ ./bin/setup
37
+ ```
38
+
39
+ This will run `bundle install`, and create `tmp/` and `spec/support/tmp/` directories.
40
+
41
+ ## Usage
42
+
43
+ After installing the gem, you will be able to run the script with the command line.
44
+
45
+ ```
46
+ $ img_fetcher -f plaintext.txt -o output_directory/
47
+ ```
48
+
49
+ You can type `img_fetcher --help` at the terminal for more information.
50
+ ```
51
+ Usage: img_fetcher -f <file_path> [options...]
52
+ -f, --file FILE_PATH [REQUIRED] Fetch and store the images from each line from the given file
53
+ -o, --output OUTPUT_DIRECTORY Specify the output directory
54
+ -V, --version Show version number and quit
55
+ -v, --verbose Make the operation more talkative
56
+ -t, --threaded Run the command with multiple threads
57
+ ```
58
+
59
+ Regarding the `OUTPUT_DIRECTORY`, folder MUST exist. In case it doesn't, files will be stored in the current directory (`./`).
60
+
61
+ ### Threaded option
62
+
63
+ Regarding the `--threaded` option, it's a basic ruby thread usage. Further improvements will be to limit the amount of threads with a pool of threads. Only the `ImgFetcher::Stats` class is synchronized with a Mutex. I don't really know if `puts` must be synchronized given that it's constantly accessing to stdout.
64
+
65
+ ## Output
66
+
67
+ If `--verbose` option is selected, then the output of the command will be shown at the terminal with the following structure:
68
+
69
+ ```
70
+ FILE ROW INDEX, STATUS, FILE ORIGINAL LINE
71
+ ```
72
+
73
+ The command returns the downloaded files preserving their original filenames (whenever is possible) at the end, starting with 6 random characters to avoid collisions between already existing files.
74
+
75
+ ## Downloading file from URL
76
+
77
+ Regarding the download of files from a URL, the first approach will be using `open-uri`, but knowing that this input will be generated by external users, `open-uri` has some [limitations and security issues](https://janko.io/improving-open-uri/) if it's nothandled carefully. After doing some research, [Down](https://github.com/janko/down) gem takes care of all these issues for you, as well as valid URL, file size, timeouts, number of redirects, connectivity, and more.
78
+
79
+ For this case, we limit the **maximum number of redirects to 0** and **there's no limit about the file size**. Looking for an improvement, both can be added as a command-line option in a future.
80
+
81
+ ## Possible improvements
82
+
83
+ 1. If URLs are repeated along the file, don't fetch them again.
84
+ 2. Creating a pool of threads for further customization.
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/francoprud/img_fetcher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/img_fetcher/blob/master/CODE_OF_CONDUCT.md).
89
+
90
+ ## License
91
+
92
+ ImgFetcher gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
93
+
94
+ ## Code of Conduct
95
+
96
+ Everyone interacting in the ImgFetcher project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/img_fetcher/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,22 @@
1
+ require 'img_fetcher/version'
2
+
3
+ GEM_NAME = -'img_fetcher'
4
+ GEM_VERSION = ImgFetcher::VERSION
5
+
6
+ task default: :build
7
+
8
+ task :build do
9
+ system "gem build #{GEM_NAME}.gemspec"
10
+ end
11
+
12
+ task install: :build do
13
+ system "gem install #{GEM_NAME}-#{GEM_VERSION}.gem"
14
+ end
15
+
16
+ task publish: :build do
17
+ system "gem push #{GEM_NAME}-#{GEM_VERSION}.gem"
18
+ end
19
+
20
+ task :clean do
21
+ system 'rm *.gem'
22
+ end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ require 'img_fetcher'
3
+ require 'optparse'
4
+
5
+ begin
6
+ arguments = ImgFetcher::CommandLine.new.parse!
7
+ ImgFetcher::Main.new(arguments).run
8
+ # Considered rescuing only these to give a clean feedback for user when using the command line tool
9
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument,
10
+ ImgFetcher::CommandLine::MissingOptionError => e
11
+ case e
12
+ when ImgFetcher::CommandLine::MissingOptionError
13
+ puts 'img_fetcher: missing required option!'
14
+ when OptionParser::InvalidOption
15
+ puts 'img_fetcher: invalid options!'
16
+ when OptionParser::MissingArgument
17
+ puts 'img_fetcher: missing arguments on options!'
18
+ end
19
+ puts 'img_fetcher: Type \'img_fetcher --help\' for more information.'
20
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+ set -vx
6
+
7
+ # Set up Ruby dependencies
8
+ bundle install
9
+
10
+ # Create folders required by specs
11
+ if ! [[ -d tmp ]]
12
+ then
13
+ mkdir tmp
14
+ fi
15
+
16
+ if ! [[ -d spec/support/tmp ]]
17
+ then
18
+ mkdir spec/support/tmp
19
+ fi
@@ -0,0 +1,43 @@
1
+ require_relative 'lib/img_fetcher/version'
2
+
3
+ # rubocop:disable Metrics/BlockLength
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'img_fetcher'
6
+ spec.version = ImgFetcher::VERSION
7
+ spec.authors = ['Franco Prudhomme']
8
+ spec.email = ['francoprud@gmail.com']
9
+
10
+ spec.summary = 'Command line tool for downloading images from URLs.'
11
+ spec.description = 'Command line tool that given a plaintext file containing URLs ' \
12
+ '(one per line), downloads all of them to the local hard disk.'
13
+ spec.homepage = 'https://github.com/francoprud/img_fetcher'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.2')
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/francoprud/img_fetcher'
21
+ # spec.metadata['changelog_uri'] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'bin'
29
+ spec.executables = ['img_fetcher']
30
+ spec.require_paths = ['lib']
31
+
32
+ # Dependencies section
33
+ ## Runtime
34
+ spec.add_runtime_dependency 'down', '~> 5.0'
35
+ ## Development & Test
36
+ spec.add_development_dependency 'rake', '~> 12.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.0'
38
+ spec.add_development_dependency 'rubocop', '~> 1.5.2'
39
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.0.1'
40
+ spec.add_development_dependency 'simplecov', '~> 0.20.0'
41
+ spec.add_development_dependency 'webmock', '~> 3.10.0'
42
+ end
43
+ # rubocop:enable Metrics/BlockLength
@@ -0,0 +1,7 @@
1
+ require 'img_fetcher/version'
2
+ require 'img_fetcher/command_line'
3
+ require 'img_fetcher/main'
4
+ require 'img_fetcher/url'
5
+ require 'img_fetcher/stats'
6
+ require 'img_fetcher/helpers/file'
7
+ require 'img_fetcher/helpers/terminal'
@@ -0,0 +1,84 @@
1
+ module ImgFetcher
2
+ class CommandLine
3
+ class MissingOptionError < StandardError; end
4
+ DEFAULT_DIRECTORY = -'./'
5
+
6
+ def initialize
7
+ @arguments = {}
8
+ @options = OptionParser.new
9
+ initialize_options
10
+ end
11
+
12
+ def parse!
13
+ @options.parse!
14
+ check_required_arguments
15
+ @arguments
16
+ end
17
+
18
+ private
19
+
20
+ def initialize_options
21
+ @options.banner = 'Usage: img_fetcher -f <file_path> [options...]'
22
+ file_path_option
23
+ output_directory_option
24
+ version_option
25
+ verbose_option
26
+ threaded_option
27
+ end
28
+
29
+ def file_path_option
30
+ @options.on(
31
+ '-f FILE_PATH', '--file FILE_PATH',
32
+ '[REQUIRED] Fetch and store the images from each line from the given file'
33
+ ) do |file_path|
34
+ @arguments[:file_path] = file_path
35
+ end
36
+ end
37
+
38
+ def output_directory_option
39
+ @options.on(
40
+ '-o OUTPUT_DIRECTORY', '--output OUTPUT_DIRECTORY', 'Specify the output directory'
41
+ ) do |output_directory|
42
+ @arguments[:output_directory] = output_directory
43
+ end
44
+ end
45
+
46
+ def version_option
47
+ @options.on('-V', '--version', 'Show version number and quit') do
48
+ puts ImgFetcher::VERSION
49
+ exit
50
+ end
51
+ end
52
+
53
+ def verbose_option
54
+ @options.on('-v', '--verbose', 'Make the operation more talkative') do
55
+ @arguments[:verbose] = true
56
+ end
57
+ end
58
+
59
+ def threaded_option
60
+ @options.on('-t', '--threaded', 'Run the command with multiple threads') do
61
+ @arguments[:threaded] = true
62
+ end
63
+ end
64
+
65
+ def check_required_arguments
66
+ # Check if file_path is present and is a valid system file
67
+ raise MissingOptionError unless @arguments[:file_path] && File.file?(@arguments[:file_path])
68
+ # Build & check output_directory
69
+ @arguments[:output_directory] = build_and_check_output_directory
70
+ end
71
+
72
+ # Checks that exists, be a valid Directory (or places the default directory),
73
+ # and appends at the end a slash (/)
74
+ def build_and_check_output_directory
75
+ directory = @arguments[:output_directory]
76
+
77
+ if directory && Dir.exist?(directory)
78
+ directory[-1] == '/' ? directory : "#{directory}/"
79
+ else
80
+ DEFAULT_DIRECTORY
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,27 @@
1
+ require 'securerandom'
2
+
3
+ module ImgFetcher
4
+ module Helpers
5
+ class File
6
+ # All the valid image mime types
7
+ VALID_MIME_TYPES = ['image/bmp', 'image/cis-cod', 'image/gif', 'image/ief', 'image/jpeg',
8
+ 'image/jpeg', 'image/jpeg', 'image/pipeg', 'image/svg+xml', 'image/tiff',
9
+ 'image/tiff', 'image/x-cmu-raster', 'image/x-cmx', 'image/x-icon',
10
+ 'image/x-portable-anymap', 'image/x-portable-bitmap',
11
+ 'image/x-portable-graymap', 'image/x-portable-pixmap', 'image/x-rgb',
12
+ 'image/x-xbitmap', 'image/x-xpixmap', 'image/x-xwindowdump'].freeze
13
+
14
+ # SecureRandom is to avoid collisions between already existing files
15
+ def self.save(temp_file, directory)
16
+ if VALID_MIME_TYPES.include?(temp_file.content_type)
17
+ FileUtils.mv(temp_file.path,
18
+ "#{directory}#{SecureRandom.hex(6)}-#{temp_file.original_filename}")
19
+ true
20
+ else
21
+ temp_file.unlink # deletes the temp file
22
+ false
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ module ImgFetcher
2
+ module Helpers
3
+ class Terminal
4
+ def self.print_headlines(file_path, output_directory)
5
+ puts 'Starting process...'
6
+ puts "Reading from: #{file_path}"
7
+ puts "Images will be downloaded to: #{output_directory}\n\n"
8
+ end
9
+
10
+ def self.print_line_status(line, index, status)
11
+ log = status ? 'RETRIEVED' : 'NOT RETRIEVED'
12
+ puts "#{index}, #{log}, #{line}"
13
+ end
14
+
15
+ def self.print_general_status(stats)
16
+ puts "\nRetrieved #{stats.retrieved_images} image(s) from a total of #{stats.total_lines}."
17
+ puts "Total time: #{stats.total_time}."
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,51 @@
1
+ module ImgFetcher
2
+ class Main
3
+ def initialize(arguments)
4
+ @arguments = arguments
5
+ @stats = Stats.new
6
+ @terminal = Helpers::Terminal
7
+ @threads = [] if @arguments[:threaded]
8
+ end
9
+
10
+ def run
11
+ start_stats
12
+ File.open(@arguments[:file_path], 'r').each_with_index do |line, index|
13
+ if @arguments[:threaded]
14
+ @threads << Thread.new { manage_line(line, index + 1) }
15
+ else
16
+ manage_line(line, index + 1)
17
+ end
18
+ end
19
+ @threads.each(&:join) if @arguments[:threaded]
20
+ end_stats
21
+ end
22
+
23
+ private
24
+
25
+ def start_stats
26
+ @stats.start
27
+ return unless @arguments[:verbose]
28
+ @terminal.print_headlines(@arguments[:file_path], @arguments[:output_directory])
29
+ end
30
+
31
+ def end_stats
32
+ @stats.end
33
+ @terminal.print_general_status(@stats) if @arguments[:verbose]
34
+ end
35
+
36
+ def manage_line(line, index)
37
+ @stats.add_line
38
+ download(line, index)
39
+ end
40
+
41
+ def download(line, index)
42
+ temp_file = URL.download(line)
43
+ if temp_file && Helpers::File.save(temp_file, @arguments[:output_directory])
44
+ @stats.add_retrieved_image
45
+ @terminal.print_line_status(line, index, true) if @arguments[:verbose]
46
+ elsif @arguments[:verbose]
47
+ @terminal.print_line_status(line, index, false)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,43 @@
1
+ module ImgFetcher
2
+ class Stats
3
+ attr_reader :total_lines, :retrieved_images
4
+
5
+ def initialize
6
+ @total_lines = 0
7
+ @retrieved_images = 0
8
+ @semaphore = Mutex.new
9
+ @start_time = nil
10
+ @end_time = nil
11
+ end
12
+
13
+ def add_line
14
+ @semaphore.synchronize do
15
+ @total_lines += 1
16
+ end
17
+ end
18
+
19
+ def add_retrieved_image
20
+ @semaphore.synchronize do
21
+ @retrieved_images += 1
22
+ end
23
+ end
24
+
25
+ def start
26
+ @start_time = now
27
+ end
28
+
29
+ def end
30
+ @end_time = now
31
+ end
32
+
33
+ def total_time
34
+ @end_time - @start_time
35
+ end
36
+
37
+ private
38
+
39
+ def now
40
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ require 'down'
2
+
3
+ module ImgFetcher
4
+ class URL
5
+ # On sucess: returns a Tempfile. On failure: returns nil.
6
+ def self.download(line)
7
+ Down.download(line, max_redirects: 0)
8
+
9
+ # Decided to rescue all Down exceptions since they are all related to connection and URL.
10
+ # By rescuing the exceptions, execution will continue and the rest of the URLs will be
11
+ # downloaded.
12
+ # We can notice that we could have one the following: Down::TooLarge, Down::InvalidUrl,
13
+ # Down::TooManyRedirects, Down::ResponseError, Down::ClientError, Down::NotFound,
14
+ # Down::ServerError, Down::ConnectionError, Down::TimeoutError, Down::SSLError.
15
+ rescue Down::Error, Addressable::URI::InvalidURIError
16
+ nil
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ImgFetcher
2
+ VERSION = -'0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: img_fetcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Franco Prudhomme
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: down
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.5.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.20.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.20.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.10.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.10.0
111
+ description: Command line tool that given a plaintext file containing URLs (one per
112
+ line), downloads all of them to the local hard disk.
113
+ email:
114
+ - francoprud@gmail.com
115
+ executables:
116
+ - img_fetcher
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".circleci/config.yml"
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".rubocop.yml"
124
+ - ".ruby-version"
125
+ - CODE_OF_CONDUCT.md
126
+ - Gemfile
127
+ - Gemfile.lock
128
+ - LICENSE.txt
129
+ - README.md
130
+ - Rakefile
131
+ - bin/img_fetcher
132
+ - bin/setup
133
+ - img_fetcher.gemspec
134
+ - lib/img_fetcher.rb
135
+ - lib/img_fetcher/command_line.rb
136
+ - lib/img_fetcher/helpers/file.rb
137
+ - lib/img_fetcher/helpers/terminal.rb
138
+ - lib/img_fetcher/main.rb
139
+ - lib/img_fetcher/stats.rb
140
+ - lib/img_fetcher/url.rb
141
+ - lib/img_fetcher/version.rb
142
+ homepage: https://github.com/francoprud/img_fetcher
143
+ licenses:
144
+ - MIT
145
+ metadata:
146
+ allowed_push_host: https://rubygems.org/
147
+ homepage_uri: https://github.com/francoprud/img_fetcher
148
+ source_code_uri: https://github.com/francoprud/img_fetcher
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: 2.7.2
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubygems_version: 3.1.4
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Command line tool for downloading images from URLs.
168
+ test_files: []