Add support for single sizes

You can now pass one of the sizes in to only download that size for the object.
This commit is contained in:
Justin Edmund 2025-03-01 05:40:34 -08:00
parent ae62d594a8
commit 5955ef2650
4 changed files with 60 additions and 35 deletions

View file

@ -46,15 +46,19 @@ module Granblue
end end
# Download images for all sizes # Download images for all sizes
# @param selected_size [String] The size to download
# @return [void] # @return [void]
def download def download(selected_size = nil)
log_info "-> #{@id}" log_info("-> #{@id}")
return if @test_mode return if @test_mode
SIZES.each_with_index do |size, index| # If a specific size is provided, use only that; otherwise, use all available sizes.
sizes = selected_size ? [selected_size] : SIZES
sizes.each_with_index do |size, index|
path = download_path(size) path = download_path(size)
url = build_url(size) url = build_url(size)
process_download(url, size, path, last: index == SIZES.size - 1) process_download(url, size, path, last: index == sizes.size - 1)
end end
end end
@ -182,7 +186,7 @@ module Granblue
# Log informational message if verbose # Log informational message if verbose
# @param message [String] Message # @param message [String] Message
def log_info(message) def log_info(message)
puts message if @verbose @logger.info(message) if @verbose
end end
# Download elemental variant image # Download elemental variant image
@ -197,12 +201,10 @@ module Granblue
filepath = "#{path}/#{filename}" filepath = "#{path}/#{filename}"
URI.open(url) do |file| URI.open(url) do |file|
content = file.read content = file.read
if content raise "Failed to read content from #{url}" unless content
File.open(filepath, 'wb') do |output|
output.write(content) File.open(filepath, 'wb') do |output|
end output.write(content)
else
raise "Failed to read content from #{url}"
end end
end end
log_info "-> #{size}:\t#{url}..." log_info "-> #{size}:\t#{url}..."

View file

@ -15,24 +15,27 @@ module Granblue
# Downloads images for all variants of a character based on their uncap status. # Downloads images for all variants of a character based on their uncap status.
# Overrides {BaseDownloader#download} to handle character-specific variants. # Overrides {BaseDownloader#download} to handle character-specific variants.
# #
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
# @note Skips download if character is not found in database # @note Skips download if character is not found in database
# @note Downloads FLB/ULB variants only if character has those uncaps # @note Downloads FLB/ULB variants only if character has those uncaps
# @see #download_variants # @see #download_variants
def download def download(selected_size = nil)
character = Character.find_by(granblue_id: @id) character = Character.find_by(granblue_id: @id)
return unless character return unless character
download_variants(character) download_variants(character, selected_size)
end end
private private
# Downloads all variants of a character's images # Downloads all variants of a character's images
#
# @param character [Character] Character model instance to download images for # @param character [Character] Character model instance to download images for
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
# @note Only downloads variants that should exist based on character uncap status # @note Only downloads variants that should exist based on character uncap status
def download_variants(character) def download_variants(character, selected_size = nil)
# All characters have 01 and 02 variants # All characters have 01 and 02 variants
variants = %W[#{@id}_01 #{@id}_02] variants = %W[#{@id}_01 #{@id}_02]
@ -45,18 +48,22 @@ module Granblue
log_info "Downloading character variants: #{variants.join(', ')}" if @verbose log_info "Downloading character variants: #{variants.join(', ')}" if @verbose
variants.each do |variant_id| variants.each do |variant_id|
download_variant(variant_id) download_variant(variant_id, selected_size)
end end
end end
# Downloads a specific variant's images in all sizes # Downloads a specific variant's images in all sizes
#
# @param variant_id [String] Character variant ID (e.g., "3040001000_01") # @param variant_id [String] Character variant ID (e.g., "3040001000_01")
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
def download_variant(variant_id) def download_variant(variant_id, selected_size = nil)
log_info "-> #{variant_id}" if @verbose log_info "-> #{variant_id}" if @verbose
return if @test_mode return if @test_mode
SIZES.each_with_index do |size, index| sizes = selected_size ? [selected_size] : SIZES
sizes.each_with_index do |size, index|
path = download_path(size) path = download_path(size)
url = build_variant_url(variant_id, size) url = build_variant_url(variant_id, size)
process_download(url, size, path, last: index == SIZES.size - 1) process_download(url, size, path, last: index == SIZES.size - 1)
@ -64,8 +71,9 @@ module Granblue
end end
# Builds URL for a specific variant and size # Builds URL for a specific variant and size
#
# @param variant_id [String] Character variant ID # @param variant_id [String] Character variant ID
# @param size [String] Image size variant ("main", "grid", or "square") # @param size [String] Image size variant ("main", "grid", "square", or "detail")
# @return [String] Complete URL for downloading the image # @return [String] Complete URL for downloading the image
def build_variant_url(variant_id, size) def build_variant_url(variant_id, size)
directory = directory_for_size(size) directory = directory_for_size(size)
@ -85,6 +93,7 @@ module Granblue
end end
# Gets directory name for a size variant # Gets directory name for a size variant
#
# @param size [String] Image size variant # @param size [String] Image size variant
# @return [String] Directory name in game asset URL structure # @return [String] Directory name in game asset URL structure
# @note Maps "main" -> "f", "grid" -> "m", "square" -> "s" # @note Maps "main" -> "f", "grid" -> "m", "square" -> "s"

View file

@ -15,25 +15,28 @@ module Granblue
# Downloads images for all variants of a summon based on their uncap status. # Downloads images for all variants of a summon based on their uncap status.
# Overrides {BaseDownloader#download} to handle summon-specific variants. # Overrides {BaseDownloader#download} to handle summon-specific variants.
# #
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
# @note Skips download if summon is not found in database # @note Skips download if summon is not found in database
# @note Downloads ULB and transcendence variants only if summon has those uncaps # @note Downloads ULB and transcendence variants only if summon has those uncaps
# @see #download_variants # @see #download_variants
def download def download(selected_size = nil)
summon = Summon.find_by(granblue_id: @id) summon = Summon.find_by(granblue_id: @id)
return unless summon return unless summon
download_variants(summon) download_variants(summon, selected_size)
end end
private private
# Downloads all variants of a summon's images # Downloads all variants of a summon's images
#
# @param summon [Summon] Summon model instance to download images for # @param summon [Summon] Summon model instance to download images for
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
# @note Only downloads variants that should exist based on summon uncap status # @note Only downloads variants that should exist based on summon uncap status
# @note Handles special transcendence art variants for 6★ summons # @note Handles special transcendence art variants for 6★ summons
def download_variants(summon) def download_variants(summon, selected_size = nil)
# All summons have base variant # All summons have base variant
variants = [@id] variants = [@id]
@ -41,26 +44,28 @@ module Granblue
variants << "#{@id}_02" if summon.ulb variants << "#{@id}_02" if summon.ulb
# Add Transcendence variants if available # Add Transcendence variants if available
if summon.transcendence variants.push("#{@id}_03", "#{@id}_04") if summon.transcendence
variants.push("#{@id}_03", "#{@id}_04")
end
log_info "Downloading summon variants: #{variants.join(', ')}" if @verbose log_info "Downloading summon variants: #{variants.join(', ')}" if @verbose
variants.each do |variant_id| variants.each do |variant_id|
download_variant(variant_id) download_variant(variant_id, selected_size)
end end
end end
# Downloads a specific variant's images in all sizes # Downloads a specific variant's images in all sizes
#
# @param variant_id [String] Summon variant ID (e.g., "2040001000_02") # @param variant_id [String] Summon variant ID (e.g., "2040001000_02")
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
# @note Downloads all size variants (main/grid/square) for the given variant # @note Downloads all size variants (main/grid/square) for the given variant
def download_variant(variant_id) def download_variant(variant_id, selected_size = nil)
log_info "-> #{variant_id}" if @verbose log_info "-> #{variant_id}" if @verbose
return if @test_mode return if @test_mode
SIZES.each_with_index do |size, index| sizes = selected_size ? [selected_size] : SIZES
sizes.each_with_index do |size, index|
path = download_path(size) path = download_path(size)
url = build_variant_url(variant_id, size) url = build_variant_url(variant_id, size)
process_download(url, size, path, last: index == SIZES.size - 1) process_download(url, size, path, last: index == SIZES.size - 1)
@ -68,8 +73,9 @@ module Granblue
end end
# Builds URL for a specific variant and size # Builds URL for a specific variant and size
#
# @param variant_id [String] Summon variant ID # @param variant_id [String] Summon variant ID
# @param size [String] Image size variant ("main", "grid", or "square") # @param size [String] Image size variant ("main", "grid", "square", or "detail")
# @return [String] Complete URL for downloading the image # @return [String] Complete URL for downloading the image
def build_variant_url(variant_id, size) def build_variant_url(variant_id, size)
directory = directory_for_size(size) directory = directory_for_size(size)

View file

@ -16,25 +16,28 @@ module Granblue
# Downloads images for all variants of a weapon based on their uncap status. # Downloads images for all variants of a weapon based on their uncap status.
# Overrides {BaseDownloader#download} to handle weapon-specific variants. # Overrides {BaseDownloader#download} to handle weapon-specific variants.
# #
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
# @note Skips download if weapon is not found in database # @note Skips download if weapon is not found in database
# @note Downloads transcendence variants only if weapon has those uncaps # @note Downloads transcendence variants only if weapon has those uncaps
# @see #download_variants # @see #download_variants
def download def download(selected_size = nil)
weapon = Weapon.find_by(granblue_id: @id) weapon = Weapon.find_by(granblue_id: @id)
return unless weapon return unless weapon
download_variants(weapon) download_variants(weapon, selected_size)
end end
private private
# Downloads all variants of a weapon's images # Downloads all variants of a weapon's images
#
# @param weapon [Weapon] Weapon model instance to download images for # @param weapon [Weapon] Weapon model instance to download images for
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
# @note Only downloads variants that should exist based on weapon uncap status # @note Only downloads variants that should exist based on weapon uncap status
# @note Handles special transcendence art variants for transcendable weapons # @note Handles special transcendence art variants for transcendable weapons
def download_variants(weapon) def download_variants(weapon, selected_size = nil)
# All weapons have base variant # All weapons have base variant
variants = [@id] variants = [@id]
@ -46,19 +49,23 @@ module Granblue
log_info "Downloading weapon variants: #{variants.join(', ')}" if @verbose log_info "Downloading weapon variants: #{variants.join(', ')}" if @verbose
variants.each do |variant_id| variants.each do |variant_id|
download_variant(variant_id) download_variant(variant_id, selected_size)
end end
end end
# Downloads a specific variant's images in all sizes # Downloads a specific variant's images in all sizes
#
# @param variant_id [String] Weapon variant ID (e.g., "1040001000_02") # @param variant_id [String] Weapon variant ID (e.g., "1040001000_02")
# @param selected_size [String] The size to download. If nil, downloads all sizes.
# @return [void] # @return [void]
# @note Downloads all size variants (main/grid/square) for the given variant # @note Downloads all size variants (main/grid/square) for the given variant
def download_variant(variant_id) def download_variant(variant_id, selected_size = nil)
log_info "-> #{variant_id}" if @verbose log_info "-> #{variant_id}" if @verbose
return if @test_mode return if @test_mode
SIZES.each_with_index do |size, index| sizes = selected_size ? [selected_size] : SIZES
sizes.each_with_index do |size, index|
path = download_path(size) path = download_path(size)
url = build_variant_url(variant_id, size) url = build_variant_url(variant_id, size)
process_download(url, size, path, last: index == SIZES.size - 1) process_download(url, size, path, last: index == SIZES.size - 1)
@ -66,8 +73,9 @@ module Granblue
end end
# Builds URL for a specific variant and size # Builds URL for a specific variant and size
#
# @param variant_id [String] Weapon variant ID # @param variant_id [String] Weapon variant ID
# @param size [String] Image size variant ("main", "grid", or "square") # @param size [String] Image size variant ("main", "grid", "square", or "raw")
# @return [String] Complete URL for downloading the image # @return [String] Complete URL for downloading the image
def build_variant_url(variant_id, size) def build_variant_url(variant_id, size)
directory = directory_for_size(size) directory = directory_for_size(size)