hensei-api/lib/granblue/parsers/wiki.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

122 lines
2.6 KiB
Ruby

# frozen_string_literal: true
require 'httparty'
# GranblueWiki fetches and parses data from gbf.wiki
module Granblue
module Parsers
class Wiki
class_attribute :base_uri
class_attribute :proficiencies
class_attribute :elements
class_attribute :rarities
class_attribute :genders
class_attribute :races
class_attribute :bullets
class_attribute :boolean
self.base_uri = 'https://gbf.wiki/api.php'
self.proficiencies = {
'Sabre' => 1,
'Dagger' => 2,
'Axe' => 3,
'Spear' => 4,
'Bow' => 5,
'Staff' => 6,
'Melee' => 7,
'Harp' => 8,
'Gun' => 9,
'Katana' => 10
}.freeze
self.elements = {
'Wind' => 1,
'Fire' => 2,
'Water' => 3,
'Earth' => 4,
'Dark' => 5,
'Light' => 6
}.freeze
self.rarities = {
'R' => 1,
'SR' => 2,
'SSR' => 3
}.freeze
self.races = {
'Other' => 0,
'Human' => 1,
'Erune' => 2,
'Draph' => 3,
'Harvin' => 4,
'Primal' => 5
}.freeze
self.genders = {
'o' => 0,
'm' => 1,
'f' => 2,
'mf' => 3
}.freeze
self.bullets = {
'cartridge' => 1,
'rifle' => 2,
'parabellum' => 3,
'aetherial' => 4
}.freeze
self.boolean = {
'yes' => true,
'no' => false
}.freeze
def initialize(props: ['wikitext'], debug: false)
@debug = debug
@props = props.join('|')
end
def fetch(page)
query_params = params(page).map do |key, value|
"#{key}=#{value}"
end.join('&')
destination = "#{base_uri}?#{query_params}"
ap "--> Fetching #{destination}" if @debug
response = HTTParty.get(destination)
handle_response(response, page)
end
private
def handle_response(response, page)
case response.code
when 200
if response.key?('error')
raise WikiError.new(code: response['error']['code'],
message: response['error']['info'],
page: page)
end
response['parse']['wikitext']['*']
when 404 then puts "Page #{page} not found"
when 500...600 then puts "Server error: #{response.code}"
end
end
def params(page)
{
action: 'parse',
format: 'json',
page: page,
prop: @props
}
end
end
end
end