class BillboardScraper

Public Class Methods

new() click to toggle source
# File lib/Hot_100_CLI/billboard_scraper.rb, line 7
def initialize
  doc = Nokogiri::HTML(open('https://www.billboard.com/charts/hot-100'))
  scrape(doc)
end

Public Instance Methods

create_song_from_scraper(song_info) click to toggle source
# File lib/Hot_100_CLI/billboard_scraper.rb, line 35
def create_song_from_scraper(song_info)
  song = Song.create(song_info[:song])
  Artist.create_by_list_name(song_info[:artist]).each { |artist| song.artists << artist }
  song.chart_status = ChartStatus.new(song_info[:chart_status])

  song.chart_status.song = song
  song.artists.each { |artist| artist.songs << song }
end
scrape(doc) click to toggle source
# File lib/Hot_100_CLI/billboard_scraper.rb, line 15
def scrape(doc)
  doc.css('.chart-data .chart-row').map do |entry|
    song_info = {}
    song_info[:chart_status] = { 
      rank: entry.css('.chart-row__current-week').text, 
      previous_week: entry.css('.chart-row__last-week .chart-row__value').text,
      peak_position: entry.css('.chart-row__top-spot .chart-row__value').text,
      weeks_charted: entry.css('.chart-row__weeks-on-chart .chart-row__value').text 
    } 

    song_info[:song] = { title: entry.css('.chart-row__song').text, 
                         spotify_link: parse_spotify_link_if_present(entry), 
                         vevo_link: parse_vevo_link_if_present(entry) }

    song_info[:artist] = { name: entry.css(".chart-row__artist").text.strip }
    create_song_from_scraper(song_info)
  end
end