Class: BandcampDiscover::Scrapers::Music
- Defined in:
- lib/bandcamp-discover/scrapers/music.rb
Instance Method Summary collapse
-
#initialize(url:, browser:, max_tasks:) ⇒ Music
constructor
A new instance of Music.
- #normalize_tally(tally) ⇒ Object
- #scrape(force: false) ⇒ Object
Constructor Details
#initialize(url:, browser:, max_tasks:) ⇒ Music
Returns a new instance of Music.
9 10 11 12 13 14 |
# File 'lib/bandcamp-discover/scrapers/music.rb', line 9 def initialize(url:, browser:, max_tasks:) super uri = URI.parse(url) @base_url = "#{uri.scheme}://#{uri.host}" end |
Instance Method Details
#normalize_tally(tally) ⇒ Object
45 46 47 48 49 |
# File 'lib/bandcamp-discover/scrapers/music.rb', line 45 def normalize_tally(tally) total = tally.values.sum.to_f tally.transform_values! { |count| count / total } tally.sort_by { |k, v| v }.reverse.to_h end |
#scrape(force: false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bandcamp-discover/scrapers/music.rb', line 16 def scrape(force: false) super do |page| page.goto(@url) album_list = page.wait_for_selector("#music-grid") album_links = album_list.query_selector_all("li.music-grid-item > a") semaphore = Async::Semaphore.new(@max_tasks) albums = album_links.take(20).map do |album_link| semaphore.async do url = album_link[:href].start_with?("https://") ? album_link[:href] : "#{@base_url}#{album_link[:href]}" puts "starting to scrape #{url}" album = Scrapers::Album.new(url: url, browser: @browser).scrape puts "done scraping #{url}" album end end.map(&:wait) albums.map! do |album_url, album_title, , album_player| { url: album_url, title: album_title, tags: , player_url: album_player } end [albums, normalize_tally(albums.map { _1[:tags] }.flatten.tally)] end end |