Merge pull request #20 from jedmund/rake
Add Rake tasks for compiling URL lists and downloading images
This commit is contained in:
commit
5ab30a06ef
6 changed files with 213 additions and 0 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -31,6 +31,10 @@
|
|||
# Ignore master key for decrypting credentials and more.
|
||||
/config/master.key
|
||||
|
||||
# Ignore exported and downloaded files
|
||||
/export
|
||||
/download
|
||||
|
||||
.DS_Store
|
||||
|
||||
/config/credentials/development.key
|
||||
|
|
|
|||
41
lib/tasks/download_images.rake
Normal file
41
lib/tasks/download_images.rake
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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)
|
||||
|
||||
if !multi
|
||||
print("[#{bar}] #{percents}% ...#{' ' * 20}#{status}\n")
|
||||
else
|
||||
print "\n"
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Exports a list of character URLs for a given size'
|
||||
task :download_images, %i[object size] => :environment do |_t, args|
|
||||
require 'open-uri'
|
||||
|
||||
filename = "export/#{args[:object]}-#{args[:size]}.txt"
|
||||
count = `wc -l #{filename}`.split.first.to_i
|
||||
|
||||
path = "#{Rails.root}/download/#{args[:object]}-#{args[:size]}"
|
||||
FileUtils.mkdir_p(path) unless Dir.exist?(path)
|
||||
|
||||
puts "Downloading #{count} images from #{args[:object]}-#{args[:size]}.txt..."
|
||||
if File.exist?(filename)
|
||||
File.readlines(filename).each_with_index do |line, i|
|
||||
download = URI.parse(line.strip).open
|
||||
download_URI = "#{path}/#{download.base_uri.to_s.split('/')[-1]}"
|
||||
if File.exist?(download_URI)
|
||||
puts "Skipping #{line}"
|
||||
else
|
||||
IO.copy_stream(download, "#{path}/#{download.base_uri.to_s.split('/')[-1]}")
|
||||
_progress_reporter(count: i, total: count, result: download_URI, bar_len: 40, multi: false)
|
||||
end
|
||||
rescue StandardError => e
|
||||
puts "#{e}: #{line}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
38
lib/tasks/export_all.rake
Normal file
38
lib/tasks/export_all.rake
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
namespace :granblue do
|
||||
namespace :export do
|
||||
desc 'Exports files of URLs for every object at every size'
|
||||
task :all do
|
||||
# Run character tasks
|
||||
Rake::Task['granblue:export:character'].invoke('main')
|
||||
Rake::Task['granblue:export:character'].reenable
|
||||
|
||||
Rake::Task['granblue:export:character'].invoke('grid')
|
||||
Rake::Task['granblue:export:character'].reenable
|
||||
|
||||
Rake::Task['granblue:export:character'].invoke('square')
|
||||
Rake::Task['granblue:export:character'].reenable
|
||||
|
||||
# Run weapon tasks
|
||||
Rake::Task['granblue:export:weapon'].invoke('main')
|
||||
Rake::Task['granblue:export:weapon'].reenable
|
||||
|
||||
Rake::Task['granblue:export:weapon'].invoke('grid')
|
||||
Rake::Task['granblue:export:weapon'].reenable
|
||||
|
||||
Rake::Task['granblue:export:weapon'].invoke('square')
|
||||
Rake::Task['granblue:export:weapon'].reenable
|
||||
|
||||
# Run summon tasks
|
||||
Rake::Task['granblue:export:summon'].invoke('main')
|
||||
Rake::Task['granblue:export:summon'].reenable
|
||||
|
||||
Rake::Task['granblue:export:summon'].invoke('grid')
|
||||
Rake::Task['granblue:export:summon'].reenable
|
||||
|
||||
Rake::Task['granblue:export:summon'].invoke('square')
|
||||
Rake::Task['granblue:export:summon'].reenable
|
||||
|
||||
puts 'Exported 9 files'
|
||||
end
|
||||
end
|
||||
end
|
||||
43
lib/tasks/export_character.rake
Normal file
43
lib/tasks/export_character.rake
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
namespace :granblue do
|
||||
namespace :export do
|
||||
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
|
||||
|
||||
desc 'Exports a list of character URLs for a given size'
|
||||
task :character, [:size] => :environment do |_t, args|
|
||||
# Set up options
|
||||
size = args[:size]
|
||||
|
||||
# Include character model
|
||||
Dir.glob("#{Rails.root}/app/models/character.rb").each { |file| require file }
|
||||
|
||||
# Set up filepath
|
||||
dir = "#{Rails.root}/export/"
|
||||
filename = "#{dir}/character-#{size}.txt"
|
||||
FileUtils.mkdir(dir) unless Dir.exist?(dir)
|
||||
|
||||
# Write to file
|
||||
File.open(filename, 'w+') do |f|
|
||||
Character.all.each do |c|
|
||||
f.write("#{build_chara_url("#{c.granblue_id}_01", size)} \n")
|
||||
f.write("#{build_chara_url("#{c.granblue_id}_02", size)} \n")
|
||||
f.write("#{build_chara_url("#{c.granblue_id}_03", size)} \n") if c.flb
|
||||
f.write("#{build_chara_url("#{c.granblue_id}_04", size)} \n") if c.ulb
|
||||
end
|
||||
end
|
||||
|
||||
# CLI Output
|
||||
count = `wc -l #{filename}`.split.first.to_i
|
||||
puts "Wrote #{count} character URLs for \"#{size}\" size"
|
||||
end
|
||||
end
|
||||
end
|
||||
47
lib/tasks/export_summon.rake
Normal file
47
lib/tasks/export_summon.rake
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
namespace :granblue do
|
||||
namespace :export do
|
||||
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
|
||||
|
||||
desc 'Exports a list of summon URLs for a given size'
|
||||
task :summon, [:size] => :environment do |_t, args|
|
||||
# Set up options
|
||||
size = args[:size]
|
||||
|
||||
# Include character model
|
||||
Dir.glob("#{Rails.root}/app/models/summon.rb").each { |file| require file }
|
||||
|
||||
# Set up filepath
|
||||
dir = "#{Rails.root}/export/"
|
||||
filename = "#{dir}/summon-#{size}.txt"
|
||||
FileUtils.mkdir(dir) unless Dir.exist?(dir)
|
||||
|
||||
# Write to file
|
||||
File.open(filename, 'w') do |f|
|
||||
Summon.all.each do |s|
|
||||
series = s.series.to_i
|
||||
f.write("#{build_summon_url(s.granblue_id.to_s, size)} \n")
|
||||
|
||||
# Download second images only for Providence ULBs and Primal summons
|
||||
if series == 3 || (series == 0 && s.ulb)
|
||||
f.write("#{build_summon_url("#{s.granblue_id}_02",
|
||||
size)} \n")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# CLI output
|
||||
count = `wc -l #{filename}`.split.first.to_i
|
||||
puts "Wrote #{count} summon URLs for \"#{size}\" size"
|
||||
end
|
||||
end
|
||||
end
|
||||
40
lib/tasks/export_weapon.rake
Normal file
40
lib/tasks/export_weapon.rake
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
namespace :granblue do
|
||||
namespace :export do
|
||||
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
|
||||
|
||||
desc 'Exports a list of weapon URLs for a given size'
|
||||
task :weapon, [:size] => :environment do |_t, args|
|
||||
# Set up options
|
||||
size = args[:size]
|
||||
|
||||
# Include weapon model
|
||||
Dir.glob("#{Rails.root}/app/models/weapon.rb").each { |file| require file }
|
||||
|
||||
# Set up filepath
|
||||
dir = "#{Rails.root}/export/"
|
||||
filename = "#{dir}/weapon-#{size}.txt"
|
||||
FileUtils.mkdir(dir) unless Dir.exist?(dir)
|
||||
|
||||
# Write to file
|
||||
File.open(filename, 'w') do |f|
|
||||
Weapon.all.each do |w|
|
||||
f.write("#{build_weapon_url(w.granblue_id.to_s, size)} \n")
|
||||
end
|
||||
end
|
||||
|
||||
# CLI output
|
||||
count = `wc -l #{filename}`.split.first.to_i
|
||||
puts "Wrote #{count} weapon URLs for \"#{size}\" size"
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue