Skip to content

Commit

Permalink
Merge pull request #1 from tonyvince/download-images-concurrently
Browse files Browse the repository at this point in the history
Download images concurrently
  • Loading branch information
tonyvince authored Jul 4, 2023
2 parents dd1e97d + 5af1ea5 commit a59e124
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source 'https://rubygems.org'

ruby "3.2.2"

gem 'concurrent-ruby'
gem 'pry'

group :test do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ GEM
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
coderay (1.1.3)
concurrent-ruby (1.2.2)
crack (0.4.5)
rexml
diff-lcs (1.5.0)
Expand Down Expand Up @@ -36,6 +37,7 @@ PLATFORMS
arm64-darwin-20

DEPENDENCIES
concurrent-ruby
pry
rspec
webmock
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Image Downloader is a command-line tool that downloads images from a list of URL

## Dependencies

- concurrent-ruby
- pry
- rspec
- webmock
Expand Down
36 changes: 22 additions & 14 deletions lib/image_downloader.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
require 'open-uri'
require 'net/http'
require 'concurrent'

class ImageDownloader
def self.download_images(urls, target_dir = "./images")
Dir.mkdir(target_dir) unless File.exist?(target_dir)

pool = Concurrent::FixedThreadPool.new(10)

urls.each do |url|
begin
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
head_response = http.request_head(uri.path)
pool.post do
begin
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
head_response = http.request_head(uri.path)

if head_response["content-type"].start_with? "image/"
URI.open(url) do |u|
file_path = "#{target_dir}/#{File.basename(uri.path)}"
File.open(file_path, 'wb') { |f| f.write(u.read) }
puts "Downloaded: #{url}"
if head_response["content-type"].start_with? "image/"
URI.open(url) do |u|
file_path = "#{target_dir}/#{File.basename(uri.path)}"
File.open(file_path, 'wb') { |f| f.write(u.read) }
puts "Downloaded: #{url}"
end
else
puts "#{url} is not an image. Skipping."
end
else
puts "#{url} is not an image. Skipping."
rescue => e
puts "Error downloading #{url}. Error message: #{e.message}"
end
rescue => e
puts "Error downloading #{url}. Error message: #{e.message}"
end
end

pool.shutdown
pool.wait_for_termination
end
end

0 comments on commit a59e124

Please sign in to comment.