picfisher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: da4504372d6b3cceb6deda036fda5f51f8d46c44726ebed76636aa02c49fa4b9
4
+ data.tar.gz: 446d39d4044826782748ca34c5a346560d6ae5490290afeb371323976ace6b06
5
+ SHA512:
6
+ metadata.gz: 5d2921671fd3e415c4f354ec1f0e67c62d95d3e2a470f4c1dd4db5de44169984d4ad42ee9657e246c6555f1ac18b312d05e8c9e12e37290bda3ae776b582d727
7
+ data.tar.gz: 7be9a0990f49777ddd3d911801d1a3ef61b69c966df20dc483b6c870c9731bf6812109bfe6af22dab5e40f000e2d3b4a523fcef2a4c9673de561787300a9e563
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2024-05-30
9
+
10
+ ### Added
11
+
12
+ - Main features implemented
13
+ - README.md updated
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Fernando Guillen
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,47 @@
1
+ # PicFisher
2
+
3
+ Send the fisher to fish your images for you. Give it a list of image urls in a flexible
4
+ formatted text file and it will go fishing to the internet for you
5
+
6
+ ## Installation
7
+
8
+ ```
9
+ gem install picfisher
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```
15
+ picfisher [images_file_path] [output_directory_path]
16
+ ```
17
+
18
+ For example:
19
+
20
+ ```
21
+ mkdir ~/Downloads/fished_images
22
+ picfisher images.txt ~/Downloads/fished_images
23
+ ```
24
+
25
+ With log
26
+
27
+ ```
28
+ DEBUG_LEVEL=debug picfisher images.txt ~/Downloads/fished_images
29
+ ```
30
+
31
+ Accepted values for `DEBUG_LEVEL`: `debug`, `info`, `error`.
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ Use `exe/picfisher` to try your development version of the command line command.
38
+
39
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fguillen/PicFisher.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
data/TODO.md ADDED
@@ -0,0 +1,7 @@
1
+ # TODO
2
+
3
+ - Create the output directory if it doesn't exist. Or add a parameter to allow the user to decide this.
4
+ - Check if the Downloader is going to overwrite a file that already exists. Add an option to `force` if not stop with an error.
5
+ - Add a progress bar for the downloading phase.
6
+ - Allow multiple concurrent downloads. Add an extra param to decide the number, default `5`
7
+ - Add error handling to the Downloader. Many things can be wrong when connecting to the internet.
data/exe/picfisher ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "picfisher"
6
+
7
+ PicFisher.fish(ARGV[0], ARGV[1])
@@ -0,0 +1,16 @@
1
+ require "open-uri"
2
+
3
+ module PicFisher
4
+ module Downloader
5
+ # TODO: We need to add error handling here
6
+ def self.download(url, output_path)
7
+ PicFisher::Log.debug("Downloading #{url} to #{output_path}")
8
+
9
+ io_stream = OpenURI::open_uri(url)
10
+
11
+ File.open(output_path, "wb") do |f|
12
+ f.write(io_stream.read)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module PicFisher
2
+ module FishingBoat
3
+ # REVIEW: we could use keyword params if we see it adds readability
4
+ def self.fish(images_file_path, output_directory_path)
5
+ PicFisher::Log.info("Fishing from #{images_file_path} to #{output_directory_path}")
6
+
7
+ file_to_string = File.read(images_file_path)
8
+ urls = PicFisher::URLExtractor.extract(file_to_string)
9
+ urls.each do |url|
10
+ sanitized_url = PicFisher::Sanitizer.sanitize_image_url(url)
11
+ PicFisher::Downloader.download(url, "#{output_directory_path}/#{sanitized_url}")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ module PicFisher
2
+ module Log
3
+ def self.debug(message)
4
+ if ["debug"].include? ENV["DEBUG_LEVEL"]
5
+ output(:debug, message)
6
+ end
7
+ end
8
+
9
+ def self.info(message)
10
+ if ["debug", "info"].include? ENV["DEBUG_LEVEL"]
11
+ output(:info, message)
12
+ end
13
+ end
14
+
15
+ def self.error(message)
16
+ if ["debug", "info", "error"].include? ENV["DEBUG_LEVEL"]
17
+ output(:error, message)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def self.output(level, message)
24
+ final_message = "PicFisher [#{level.upcase}] #{message}"
25
+ Kernel.puts(final_message)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ module PicFisher
2
+ module Sanitizer
3
+ def self.sanitize_image_url(url)
4
+ filename_extension = File.extname(url)
5
+ filename_extension_escaped = Regexp.escape(filename_extension) # to escape the dot "."
6
+ result =
7
+ url
8
+ .sub(/#{filename_extension_escaped}$/, "")
9
+ .gsub(/[^\w\s_-]+/, "_")
10
+ .gsub(/(^|\b\s)\s+($|\s?\b)/, "\\1\\2")
11
+ .gsub(/\s+/, "_")
12
+
13
+ result = "#{result}#{filename_extension}"
14
+
15
+ PicFisher::Log.debug("Sanitizing url '#{url}': #{result}")
16
+
17
+ result
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ require "uri"
2
+
3
+ module PicFisher
4
+ module URLExtractor
5
+ # NOTE: using URI.regexp(["http", "https"]) didn't work for me
6
+ URL_REGEX = /https?:\/\/[^\s,]+/
7
+ IMAGE_EXTENSITONS = %w(.jpg .jpeg .png .gif .webp .bmp .ico .svg .tiff or .tif .psd .raw .cr2 .nrw .arw .dng .nef .orf .sr2 .raf .tif .tiff .djvu)
8
+
9
+ def self.extract(string)
10
+ result =
11
+ string
12
+ .scan(URL_REGEX)
13
+ .select { |url| url.end_with?(*IMAGE_EXTENSITONS) }
14
+ .uniq
15
+
16
+ PicFisher::Log.debug("Extracted urls: #{result.join("|")}")
17
+
18
+ result
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PicFisher
4
+ VERSION = "0.1.0"
5
+ end
data/lib/picfisher.rb ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "picfisher/version"
4
+ require_relative "picfisher/sanitizer"
5
+ require_relative "picfisher/url_extractor"
6
+ require_relative "picfisher/downloader"
7
+ require_relative "picfisher/fishing_boat"
8
+ require_relative "picfisher/log"
9
+
10
+ module PicFisher
11
+ class Error < StandardError; end
12
+
13
+ # REVIEW: we could use keyword params if we see it adds readability
14
+ def self.fish(images_file_path, output_directory_path)
15
+ PicFisher::Log.info "PicFisher is running..."
16
+
17
+ if images_file_path.nil? || output_directory_path.nil?
18
+ message = "Use: picfisher [images_file_path] [output_directory_path]"
19
+ PicFisher::Log.error(message)
20
+ raise PicFisher::Error.new(message)
21
+ end
22
+
23
+ if !File.exist?(images_file_path)
24
+ message = "File not found: #{images_file_path}"
25
+ PicFisher::Log.error(message)
26
+ raise PicFisher::Error.new(message)
27
+ end
28
+
29
+ if !Dir.exist?(output_directory_path)
30
+ message = "Directory not found: #{images_file_path}"
31
+ PicFisher::Log.error(message)
32
+ raise PicFisher::Error.new(message)
33
+ end
34
+
35
+ PicFisher::FishingBoat.fish(images_file_path, output_directory_path)
36
+ PicFisher::Log.info "PicFisher is finished!"
37
+ end
38
+ end
data/sig/picfisher.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module PicFisher
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: picfisher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Guillen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: open-uri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: uri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.13.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.13.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.16'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.21'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.21'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.23'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 3.23.1
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '3.23'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 3.23.1
103
+ - !ruby/object:Gem::Dependency
104
+ name: mocha
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.3'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '2.3'
117
+ description: Reading all the image URLs from a given text file and download them to
118
+ a given directory.
119
+ email:
120
+ - fguillen.mail@gmail.com
121
+ executables:
122
+ - picfisher
123
+ extensions: []
124
+ extra_rdoc_files: []
125
+ files:
126
+ - ".rubocop.yml"
127
+ - CHANGELOG.md
128
+ - LICENSE.txt
129
+ - README.md
130
+ - Rakefile
131
+ - TODO.md
132
+ - exe/picfisher
133
+ - lib/picfisher.rb
134
+ - lib/picfisher/downloader.rb
135
+ - lib/picfisher/fishing_boat.rb
136
+ - lib/picfisher/log.rb
137
+ - lib/picfisher/sanitizer.rb
138
+ - lib/picfisher/url_extractor.rb
139
+ - lib/picfisher/version.rb
140
+ - sig/picfisher.rbs
141
+ homepage: https://github.com/fguillen/PicFisher
142
+ licenses:
143
+ - MIT
144
+ metadata:
145
+ homepage_uri: https://github.com/fguillen/PicFisher
146
+ source_code_uri: https://github.com/fguillen/PicFisher
147
+ changelog_uri: https://github.com/fguillen/PicFisher/blob/main/CHANGELOG.md
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: 3.0.0
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubygems_version: 3.5.10
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Command line tool to download images from a text file.
167
+ test_files: []