[Ruby] CSV ファイルに記載された URL からファイルをダウンロードするスクリプト
必要に駆られて書いた使い捨てのスクリプト。
TSV ファイルの URL リストを利用して `wget` でダウンロードしています。
“`ruby
# frozen_string_literal: true
require ‘csv’
require ‘uri’
def download_image(uri)
download_path = ‘./download’
`wget #{uri} -P #{download_path}`
sleep 1
end
file = ‘product_images.tsv’
f = CSV.open(file, headers: false, col_sep: “\t”, skip_blanks: true)
f.readline # Skip headers
f.each do |line|
line.each do |str|
uri = str if str =~ URI::DEFAULT_PARSER.make_regexp
download_image(uri) unless uri.nil?
end
end
“`
## 補遺
> – [URI.regexp (Ruby 2.7.0 リファレンスマニュアル)](https://docs.ruby-lang.org/ja/latest/method/URI/s/regexp.html)
> – [Linting Cops – RuboCop: The Ruby Linter that Serves and Protects](https://rubocop.readthedocs.io/en/latest/cops_lint/#linturiregexp)