Simplify ImageDownloader

This commit is contained in:
Justin Edmund 2025-01-15 19:14:13 -08:00
parent df1bc9427a
commit c76b79e4b8

View file

@ -1,6 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../logging_helper' require_relative '../logging_helper'
require_relative '../granblue/downloaders/base_downloader'
require_relative '../granblue/downloaders/character_downloader'
require_relative '../granblue/downloaders/weapon_downloader'
require_relative '../granblue/downloaders/summon_downloader'
require_relative '../granblue/downloaders/elemental_weapon_downloader'
require_relative '../granblue/download_manager'
module PostDeployment module PostDeployment
class ImageDownloader class ImageDownloader
@ -12,6 +18,12 @@ module PostDeployment
both: 'to local disk and S3' both: 'to local disk and S3'
}.freeze }.freeze
SUPPORTED_TYPES = {
'character' => Granblue::Downloader::CharacterDownloader,
'summon' => Granblue::Downloader::SummonDownloader,
'weapon' => Granblue::Downloader::WeaponDownloader
}.freeze
def initialize(test_mode:, verbose:, storage:, new_records:, updated_records:) def initialize(test_mode:, verbose:, storage:, new_records:, updated_records:)
@test_mode = test_mode @test_mode = test_mode
@verbose = verbose @verbose = verbose
@ -21,35 +33,34 @@ module PostDeployment
end end
def run def run
return if @test_mode
log_header 'Downloading images...', '+' log_header 'Downloading images...', '+'
[@new_records, @updated_records].each do |records| SUPPORTED_TYPES.each do |type, downloader_class|
records.each do |type, items| download_type_images(type, downloader_class)
next if items.empty?
download_type_images(type, items)
end
end end
end end
private private
def download_type_images(type, items) def download_type_images(type, downloader_class)
if @verbose records = (@new_records[type] || []) + (@updated_records[type] || [])
log_header "Processing #{type.pluralize} (#{items.size} records)...", "-" return if records.empty?
puts "\n"
puts "\nDownloading #{type} images..." if @verbose
records.each do |record|
# Get the ID either from the granblue_id hash key (test mode) or method (normal mode)
id = record.respond_to?(:granblue_id) ? record.granblue_id : record[:granblue_id]
downloader = downloader_class.new(
id,
test_mode: @test_mode,
verbose: @verbose,
storage: @storage
)
downloader.download
end end
download_options = {
test_mode: @test_mode,
verbose: @verbose,
storage: @storage
}
items.each do |item|
id = @test_mode ? item[:granblue_id] : item.id
download_single_image(type, id, download_options)
end
end end
def download_single_image(type, id, options) def download_single_image(type, id, options)