hensei-api/lib/post_deployment/image_downloader.rb
Justin Edmund 0d5d4d5f59
Jedmund/import (#167)
* Move app/helpers/granblue_wiki to lib/parsers/wiki

This clears up the namespace beginning with "Granblue"

* Removed some top-level Granblue libs

DataImporter and DownloadManager exist inside of the PostDeployment namespace now so these files are redundant

* Fix Downloaders namespace

Our namespace was singular Downloader, now it is plural Downloaders to match the folder name

* Fix import paths

* DownloadManager was moved to downloaders/
* import_data task now uses the PostDeployment version of DataImporter

* Update application.rb

Eager-Load/Autoload the lib/ folder

* Update cors.rb

Add Granblue website and Extension ID to CORS

* Add transformers

Transformers take raw data from Granblue Fantasy and transforms them into hensei-compatible JSON. Transformers heavily borrow from vazkii/hensei-transfer.

* Add ImportController and route

This adds the controller that handles creating a full party from transformed Granblue Fantasy data
2025-01-17 12:02:12 -08:00

84 lines
2.6 KiB
Ruby

# frozen_string_literal: true
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/downloaders/download_manager'
module PostDeployment
class ImageDownloader
include LoggingHelper
STORAGE_DESCRIPTIONS = {
local: 'to local disk',
s3: 'to S3',
both: 'to local disk and S3'
}.freeze
SUPPORTED_TYPES = {
'character' => Granblue::Downloaders::CharacterDownloader,
'summon' => Granblue::Downloaders::SummonDownloader,
'weapon' => Granblue::Downloaders::WeaponDownloader
}.freeze
def initialize(test_mode:, verbose:, storage:, new_records:, updated_records:)
@test_mode = test_mode
@verbose = verbose
@storage = storage
@new_records = new_records
@updated_records = updated_records
end
def run
return if @test_mode
log_header 'Downloading images...', '+'
SUPPORTED_TYPES.each do |type, downloader_class|
download_type_images(type, downloader_class)
end
end
private
def download_type_images(type, downloader_class)
records = (@new_records[type] || []) + (@updated_records[type] || [])
return if records.empty?
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
def download_single_image(type, id, options)
action_text = @test_mode ? 'Would download' : 'Downloading'
storage_text = STORAGE_DESCRIPTIONS[options[:storage]]
log_verbose "#{action_text} images #{storage_text} for #{type} #{id}...\n"
unless @test_mode
Granblue::Downloader::DownloadManager.download_for_object(
type,
id,
**options
)
end
rescue => e
error_message = "Error #{@test_mode ? 'would occur' : 'occurred'} downloading images for #{type} #{id}: #{e.message}"
puts error_message
puts e.backtrace.take(5) if @verbose
end
end
end