Update download_images.rake
Most of this task has been extracted into the Downloader libraries
This commit is contained in:
parent
e287176873
commit
0afffbe4e2
1 changed files with 11 additions and 177 deletions
|
|
@ -1,188 +1,22 @@
|
|||
namespace :granblue do
|
||||
def _progress_reporter(count:, total:, result:, bar_len: 40, multi: true)
|
||||
filled_len = (bar_len * count / total).round
|
||||
status = File.basename(result)
|
||||
percents = (100.0 * count / total).round(1)
|
||||
bar = '=' * filled_len + '-' * (bar_len - filled_len)
|
||||
desc 'Downloads images for the given Granblue_IDs'
|
||||
task :download_images, %i[object] => :environment do |_t, args|
|
||||
require_relative '../granblue/downloaders/base_downloader'
|
||||
Dir[Rails.root.join('lib', 'granblue', 'image_downloader', '*.rb')].each { |file| require file }
|
||||
|
||||
if !multi
|
||||
print("[#{bar}] #{percents}% ...#{' ' * 14}#{status}\n")
|
||||
else
|
||||
print "\n"
|
||||
end
|
||||
end
|
||||
object = args[:object]
|
||||
list = args.extras
|
||||
|
||||
def build_weapon_url(id, size)
|
||||
# Set up URL
|
||||
base_url = 'http://gbf.game-a.mbga.jp/assets/img/sp/assets/weapon'
|
||||
extension = '.jpg'
|
||||
|
||||
directory = 'ls' if size.to_s == 'main'
|
||||
directory = 'm' if size.to_s == 'grid'
|
||||
directory = 's' if size.to_s == 'square'
|
||||
|
||||
"#{base_url}/#{directory}/#{id}#{extension}"
|
||||
end
|
||||
|
||||
def build_summon_url(id, size)
|
||||
# Set up URL
|
||||
base_url = 'http://gbf.game-a.mbga.jp/assets/img/sp/assets/summon'
|
||||
extension = '.jpg'
|
||||
|
||||
directory = 'party_main' if size.to_s == 'main'
|
||||
directory = 'party_sub' if size.to_s == 'grid'
|
||||
directory = 's' if size.to_s == 'square'
|
||||
|
||||
"#{base_url}/#{directory}/#{id}#{extension}"
|
||||
end
|
||||
|
||||
def build_chara_url(id, size)
|
||||
# Set up URL
|
||||
base_url = 'http://gbf.game-a.mbga.jp/assets/img/sp/assets/npc'
|
||||
extension = '.jpg'
|
||||
|
||||
directory = 'f' if size.to_s == 'main'
|
||||
directory = 'm' if size.to_s == 'grid'
|
||||
directory = 's' if size.to_s == 'square'
|
||||
|
||||
"#{base_url}/#{directory}/#{id}#{extension}"
|
||||
end
|
||||
|
||||
def download_images(url, size, path)
|
||||
download = URI.parse(url).open
|
||||
download_URI = "#{path}/#{download.base_uri.to_s.split('/')[-1]}"
|
||||
if File.exist?(download_URI)
|
||||
puts "\tSkipping #{size}\t#{url}"
|
||||
else
|
||||
puts "\tDownloading #{size}\t#{url}..."
|
||||
IO.copy_stream(download, "#{path}/#{download.base_uri.to_s.split('/')[-1]}")
|
||||
end
|
||||
rescue OpenURI::HTTPError
|
||||
puts "\t404 returned\t#{url}"
|
||||
end
|
||||
|
||||
def download_elemental_images(url, size, path, filename)
|
||||
filepath = "#{path}/#{filename}"
|
||||
download = URI.parse(url).open
|
||||
puts "\tDownloading #{size}\t#{url}..."
|
||||
IO.copy_stream(download, filepath)
|
||||
rescue OpenURI::HTTPError
|
||||
puts "\t404 returned\t#{url}"
|
||||
end
|
||||
|
||||
def download_chara_images(id)
|
||||
sizes = %w[main grid square]
|
||||
|
||||
url = {
|
||||
'main': build_chara_url(id, 'main'),
|
||||
'grid': build_chara_url(id, 'grid'),
|
||||
'square': build_chara_url(id, 'square')
|
||||
}
|
||||
|
||||
puts "Character #{id}"
|
||||
sizes.each do |size|
|
||||
path = "#{Rails.root}/download/character-#{size}"
|
||||
download_images(url[size.to_sym], size, path)
|
||||
end
|
||||
end
|
||||
|
||||
def download_weapon_images(id)
|
||||
sizes = %w[main grid square]
|
||||
|
||||
url = {
|
||||
'main': build_weapon_url(id, 'main'),
|
||||
'grid': build_weapon_url(id, 'grid'),
|
||||
'square': build_weapon_url(id, 'square')
|
||||
}
|
||||
|
||||
puts "Weapon #{id}"
|
||||
sizes.each do |size|
|
||||
path = "#{Rails.root}/download/weapon-#{size}"
|
||||
download_images(url[size.to_sym], size, path)
|
||||
end
|
||||
end
|
||||
|
||||
def download_summon_images(id)
|
||||
sizes = %w[main grid square]
|
||||
|
||||
url = {
|
||||
'main': build_summon_url(id, 'main'),
|
||||
'grid': build_summon_url(id, 'grid'),
|
||||
'square': build_summon_url(id, 'square')
|
||||
}
|
||||
|
||||
puts "Summon #{id}"
|
||||
sizes.each do |size|
|
||||
path = "#{Rails.root}/download/summon-#{size}"
|
||||
download_images(url[size.to_sym], size, path)
|
||||
list.each do |id|
|
||||
Granblue::Downloader::DownloadManager.download_for_object(object, id)
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Downloads elemental weapon images'
|
||||
task :download_elemental_images, [:id_base] => :environment do |_t, args|
|
||||
id_base = args[:id_base].to_i
|
||||
suffixes = [2, 3, 4, 1, 6, 5]
|
||||
require_relative '../granblue/downloaders/base_downloader'
|
||||
Dir[Rails.root.join('lib', 'granblue', 'image_downloader', '*.rb')].each { |file| require file }
|
||||
|
||||
(1..6).each do |i|
|
||||
id = id_base + (i - 1) * 100
|
||||
|
||||
sizes = %w[main grid square]
|
||||
|
||||
url = {
|
||||
'main': build_weapon_url(id, 'main'),
|
||||
'grid': build_weapon_url(id, 'grid'),
|
||||
'square': build_weapon_url(id, 'square')
|
||||
}
|
||||
|
||||
puts "Elemental Weapon #{id}_#{suffixes[i - 1]}"
|
||||
sizes.each do |size|
|
||||
path = "#{Rails.root}/download/weapon-#{size}"
|
||||
filename = "#{id}_#{suffixes[i - 1]}.jpg"
|
||||
download_elemental_images(url[size.to_sym], size, path, filename)
|
||||
end
|
||||
|
||||
_progress_reporter(count: i, total: 6, result: "Elemental Weapon #{id}_#{suffixes[i - 1]}", bar_len: 40,
|
||||
multi: true)
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Downloads images for the given Granblue_IDs'
|
||||
task :download_images, %i[object] => :environment do |_t, args|
|
||||
object = args[:object]
|
||||
list = args.extras
|
||||
|
||||
list.each do |id|
|
||||
case object
|
||||
when 'character'
|
||||
character = Character.find_by(granblue_id: id)
|
||||
next unless character
|
||||
|
||||
download_chara_images("#{id}_01")
|
||||
download_chara_images("#{id}_02")
|
||||
download_chara_images("#{id}_03") if character.flb
|
||||
download_chara_images("#{id}_04") if character.ulb
|
||||
when 'weapon'
|
||||
weapon = Weapon.find_by(granblue_id: id)
|
||||
next unless weapon
|
||||
|
||||
download_weapon_images(id)
|
||||
|
||||
if weapon.transcendence
|
||||
download_weapon_images("#{id}_02")
|
||||
download_weapon_images("#{id}_03")
|
||||
end
|
||||
when 'summon'
|
||||
summon = Summon.find_by(granblue_id: id)
|
||||
next unless summon
|
||||
|
||||
download_summon_images("#{id}")
|
||||
download_summon_images("#{id}_02") if summon.ulb
|
||||
|
||||
if summon.transcendence
|
||||
download_summon_images("#{id}_03")
|
||||
download_summon_images("#{id}_04")
|
||||
end
|
||||
end
|
||||
end
|
||||
Granblue::Downloader::ElementalWeaponDownloader.new(args[:id_base]).download
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue