Skip to content

Commit

Permalink
add parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Oct 2, 2023
1 parent 873fea5 commit 0e5206b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/follow_the_snow/csv_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def build!
file.sync = true
file.puts 'name,closed,lat,lng,city,state,country,url'

wikipedia.map do |resort|
Parallel.each(wikipedia.resorts, in_threads: 3) do |resort|
logger = @logger.child(resort: resort.name)
address = geo.to_address(lat: resort.lat, lng: resort.lng, logger: logger)
metadata = OpenStruct.new(closed: false)

sleep(rand(0.0..1.0))

file.puts([
resort.name,
metadata.closed,
Expand Down
14 changes: 5 additions & 9 deletions lib/follow_the_snow/scrape/wikipedia.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# frozen_string_literal: true

require 'geo/coord'
require 'http'
require 'nokogiri'
require 'ostruct'
require 'geo/coord'
require 'parallel'

module FollowTheSnow
module Scrape
Wikipedia = Struct.new(:url, :logger, keyword_init: true) do
include Enumerable

def each(&block)
resorts(&block)
end

def resorts
doc = Nokogiri::HTML(HTTP.follow.timeout(10).get(url).to_s)
doc.css('#mw-content-text ul > li > a:first-child').map do |link|
links = doc.css('#mw-content-text ul > li > a:first-child')
Parallel.map(links, in_threads: 4) do |link|
href = link['href']
next if href =~ /Template|Category|Comparison|List|Former/i

Expand Down Expand Up @@ -49,7 +45,7 @@ def resorts
lng: geo.lng,
url: url
})
yield(resort) if block_given?
sleep(rand(0.0..1.0))
resort
end.compact
end
Expand Down

0 comments on commit 0e5206b

Please sign in to comment.