Skip to content

Commit

Permalink
Add script for scraping location names & update CSV
Browse files Browse the repository at this point in the history
- increment IDs from existing CSV
- strip special characters from names to convert into human identifier
  • Loading branch information
laurenball committed Feb 23, 2021
1 parent 919cd68 commit 0f5198a
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pokeapi/scripts/gen8/galar_locations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'nokogiri'
require 'open-uri'
require 'csv'

# region ID for Galar, as listed in 'pokedex/data/csv/regions.csv'
GALAR_ID = '8'

# open location index in Bulbapedia
doc = Nokogiri::HTML(open('https://bulbapedia.bulbagarden.net/wiki/Category:Sword_and_Shield_locations'))

locations = doc.css('div.mw-category-group').map do |category|
category.css('a').map do |location|
# urls have a prepended '/'
full_link = "https://bulbapedia.bulbagarden.net#{location.attribute('href').value}"
{ title: location.attribute('title').value, link: full_link }
end
end.flatten

# remove overarching region location page
locations.delete_if { |location| location[:title] == 'Galar' }
CSV.open('/home/lauren/dev/pokedex/pokedex/data/csv/locations.csv', 'a+') do |location_csv|
# read existing data into memory and grab the last ID from the final row
existing_rows = location_csv.read
highest_location_id = existing_rows.last.first.to_i
first_location_id = highest_location_id + 1

locations.each_with_index do |location, index|
# replace spaces with a hyphen and remove all non-alphanumeric characters to match existing convention
identifier_location_name = location[:title].downcase.gsub(/[^a-z0-9\s]/, '').gsub(' ', '-')

location_csv << [
"#{first_location_id + index}",
GALAR_ID,
identifier_location_name,
]
end
end
96 changes: 96 additions & 0 deletions pokedex/data/csv/locations.csv
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,99 @@ id,region_id,identifier
796,7,aether-paradise
797,7,ultra-space
798,7,malie-city--outer-cape
799,8,axews-eye
800,8,ballimere-lake
801,8,ballonlea
802,8,battle-caf
803,8,battle-tower-generation-viii
804,8,brawlers-cave
805,8,bridge-field
806,8,challenge-beach
807,8,challenge-road
808,8,circhester
809,8,courageous-cavern
810,8,crown-shrine
811,8,crown-tundra
812,8,dappled-grove
813,8,dusty-bowl
814,8,dyna-tree-hill
815,8,east-lake-axewell
816,8,energy-plant
817,8,fields-of-honor
818,8,forest-of-focus
819,8,freezington
820,8,frigid-sea
821,8,frostpoint-field
822,8,galar-mine
823,8,galar-mine-no-2
824,8,giants-bed
825,8,giants-cap
826,8,giants-foot
827,8,giants-mirror
828,8,giants-seat
829,8,glimwood-tangle
830,8,hammerlocke
831,8,hammerlocke-hills
832,8,honeycalm-island
833,8,honeycalm-sea
834,8,hulbury
835,8,iceberg-ruins-galar
836,8,insular-sea
837,8,iron-ruins-galar
838,8,isle-of-armor
839,8,lake-of-outrage
840,8,lakeside-cave
841,8,loop-lagoon
842,8,master-dojo
843,8,max-lair
844,8,meetup-spot
845,8,motostoke
846,8,motostoke-outskirts
847,8,motostoke-riverbank
848,8,north-lake-miloch
849,8,old-cemetery
850,8,path-to-the-peak
851,8,players-house
852,8,pokmon-den
853,8,postwick
854,8,potbottom-desert
855,8,roaringsea-caves
856,8,rock-peak-ruins-galar
857,8,rolling-fields
858,8,rose-tower
859,8,galar-route-1
860,8,galar-route-2
861,8,galar-route-3
862,8,galar-route-4
863,8,galar-route-5
864,8,galar-route-6
865,8,galar-route-7
866,8,galar-route-8
867,8,galar-route-9
868,8,galar-route-10
869,8,route-9-tunnel
870,8,slippery-slope
871,8,slumbering-weald
872,8,snowslide-slope
873,8,soothing-wetlands
874,8,south-lake-miloch
875,8,spikemuth
876,8,splitdecision-ruins
877,8,steppingstone-sea
878,8,stony-wilderness
879,8,stowonside
880,8,threepoint-pass
881,8,tower-of-darkness
882,8,tower-of-waters
883,8,towers-of-two-fists
884,8,training-lowlands
885,8,tunnel-to-the-top
886,8,turffield
887,8,warmup-tunnel
888,8,watchtower-lair
889,8,watchtower-ruins
890,8,wedgehurst
891,8,west-lake-axewell
892,8,wild-area
893,8,workout-sea
894,8,wyndon

0 comments on commit 0f5198a

Please sign in to comment.