Add ability to save characters from wiki script
This commit is contained in:
parent
ce9775c838
commit
2107739640
1 changed files with 137 additions and 3 deletions
|
|
@ -9,6 +9,55 @@ require 'pry'
|
|||
class GranblueWiki
|
||||
URL = 'https://gbf.wiki/api.php'
|
||||
|
||||
PROFICIENCY_MAP = {
|
||||
'Sabre' => 1,
|
||||
'Dagger' => 2,
|
||||
'Axe' => 3,
|
||||
'Spear' => 4,
|
||||
'Bow' => 5,
|
||||
'Staff' => 6,
|
||||
'Melee' => 7,
|
||||
'Harp' => 8,
|
||||
'Gun' => 9,
|
||||
'Katana' => 10
|
||||
}
|
||||
|
||||
ELEMENT_MAP = {
|
||||
'Wind' => 1,
|
||||
'Fire' => 2,
|
||||
'Water' => 3,
|
||||
'Earth' => 4,
|
||||
'Dark' => 5,
|
||||
'Light' => 6
|
||||
}
|
||||
|
||||
RARITY_MAP = {
|
||||
'R' => 1,
|
||||
'SR' => 2,
|
||||
'SSR' => 3
|
||||
}
|
||||
|
||||
RACE_MAP = {
|
||||
'Other' => 0,
|
||||
'Human' => 1,
|
||||
'Erune' => 2,
|
||||
'Draph' => 3,
|
||||
'Harvin' => 4,
|
||||
'Primal' => 5
|
||||
}
|
||||
|
||||
GENDER_MAP = {
|
||||
'o' => 0,
|
||||
'm' => 1,
|
||||
'f' => 2,
|
||||
'mf' => 3
|
||||
}
|
||||
|
||||
BOOLEAN_MAP = {
|
||||
'yes' => true,
|
||||
'no' => false
|
||||
}
|
||||
|
||||
def fetch(page)
|
||||
query_params = params(page).map do |key, value|
|
||||
"#{key}=#{value}"
|
||||
|
|
@ -20,13 +69,98 @@ class GranblueWiki
|
|||
response['parse']['wikitext']['*']
|
||||
end
|
||||
|
||||
def save_characters(characters)
|
||||
success = 0
|
||||
characters.each do |chara|
|
||||
success += 1 if parse(chara)
|
||||
end
|
||||
puts "Saved #{success} characters to the database"
|
||||
end
|
||||
def parse(page)
|
||||
parsed = parse_string(fetch(page))
|
||||
info = extract_chara_info(parsed)
|
||||
create_character(info)
|
||||
|
||||
abilities = extract_abilities(parsed)
|
||||
ougis = extract_ougis(parsed)
|
||||
# abilities = extract_abilities(parsed)
|
||||
# ougis = extract_ougis(parsed)
|
||||
|
||||
ap abilities.merge(ougis)
|
||||
# ap abilities.merge(ougis)
|
||||
end
|
||||
|
||||
def extract_chara_info(hash)
|
||||
info = Hash.new
|
||||
|
||||
info[:name] = {
|
||||
en: hash['name'],
|
||||
ja: hash['jpname']
|
||||
}
|
||||
info[:id] = hash['id']
|
||||
info[:charid] = hash['charid'].scan(/\b\d{4}\b/)
|
||||
|
||||
info[:flb] = BOOLEAN_MAP.fetch(hash['5star'], false)
|
||||
info[:ulb] = true if hash['max_evo'].to_i == 6
|
||||
|
||||
info[:rarity] = RARITY_MAP.fetch(hash['rarity'], 0)
|
||||
info[:element] = ELEMENT_MAP.fetch(hash['element'], 0)
|
||||
info[:gender] = GENDER_MAP.fetch(hash['gender'], 0)
|
||||
|
||||
profs = hash['weapon'].to_s.split(',')
|
||||
proficiencies = profs.map.with_index do |prof, i|
|
||||
{ "proficiency#{i + 1}" => PROFICIENCY_MAP[prof] }
|
||||
end
|
||||
info[:proficiencies] = proficiencies.reduce({}, :merge)
|
||||
|
||||
races = hash['race'].to_s.split(',')
|
||||
mapped_races = races.map.with_index do |race, i|
|
||||
{ "race#{i + 1}" => RACE_MAP[race] }
|
||||
end
|
||||
info[:races] = mapped_races.reduce({}, :merge)
|
||||
|
||||
info[:hp] = {
|
||||
min_hp: hash['min_hp'].to_i,
|
||||
max_hp: hash['max_hp'].to_i,
|
||||
max_hp_flb: hash['flb_hp'].to_i
|
||||
}
|
||||
|
||||
info[:atk] = {
|
||||
min_atk: hash['min_atk'].to_i,
|
||||
max_atk: hash['max_atk'].to_i,
|
||||
max_atk_flb: hash['flb_atk'].to_i
|
||||
}
|
||||
|
||||
{ 'info' => info.compact }
|
||||
end
|
||||
|
||||
def create_character(hash)
|
||||
info = hash['info']
|
||||
|
||||
c = Character.new
|
||||
c.granblue_id = info[:id]
|
||||
c.character_id = info[:charid]
|
||||
c.name_en = info[:name][:en]
|
||||
c.name_jp = info[:name][:ja]
|
||||
c.flb = info[:flb]
|
||||
c.ulb = info[:ulb] if info.key?(:ulb)
|
||||
c.rarity = info[:rarity]
|
||||
c.element = info[:element]
|
||||
c.gender = info[:gender]
|
||||
c.race1 = info[:races]['race1']
|
||||
c.race2 = info[:races]['race2'] if info[:races].key?('race2')
|
||||
c.proficiency1 = info[:proficiencies]['proficiency1']
|
||||
c.proficiency2 = info[:proficiencies]['proficiency2'] if info[:proficiencies].key?('proficiency2')
|
||||
c.min_hp = info[:hp][:min_hp]
|
||||
c.max_hp = info[:hp][:max_hp]
|
||||
c.max_hp_flb = info[:hp][:max_hp_flb]
|
||||
c.min_atk = info[:atk][:min_atk]
|
||||
c.max_atk = info[:atk][:max_atk]
|
||||
c.max_atk_flb = info[:atk][:max_atk_flb]
|
||||
|
||||
if c.save
|
||||
puts "Saved #{c.name_en} (#{c.granblue_id}) to the database"
|
||||
true
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
def extract_abilities(hash)
|
||||
|
|
|
|||
Loading…
Reference in a new issue