Sha256: bd62ca18ef5841a16f8618c0b934c75f2e8efcefb2e3663878cf905afc936e66

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# Playlist downloader, calls SongDownloader for each song in playlist
class PlaylistDownloader < Downloader
  LINKS_CSS_PATH = '#pl-video-table tr td a'.freeze

  def self.playlist?(url)
    url.include? YOUTUBE_BASE_URL + '/playlist?list='
  end

  def initialize(url, counter, settings)
    @url = url
    @urls = urls
    @name = parse_name(@url.scrapify[:title]) + '/'
    @counter = counter.dup
    @counter.playlist_current = 1
    @counter.playlist_total = @urls.size
    @settings = settings.dup
    @settings.subfolder = @name
  end

  def urls
    html = Nokogiri::HTML(open(url).read)
    html.css(LINKS_CSS_PATH)
        .select { |link| link.attr('href').include?('watch') }
        .map { |link| YOUTUBE_HTTPS_URL + link.attr('href') }
        .uniq
  rescue
    SimpleLogger.couldnt_fetch_playlist_url
    []
  end

  def self.download(url, counter, settings)
    playlist = new(url, counter, settings)
    playlist.urls.each do |song_url|
      SongDownloader.download(song_url, playlist.counter, playlist.settings)
      playlist.counter.playlist_current += 1
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ytsongdw-0.2.0 lib/playlist_downloader.rb
ytsongdw-0.1.4 lib/playlist_downloader.rb
ytsongdw-0.1.3 lib/playlist_downloader.rb