Sha256: b867f65336f34591e52e1954dc9845cb303cae7567cd8109254137ae82b800ec

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module PlaylistParser
  YoutubeRegex = /(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/
  
  class Playlist
    attr_reader :playlistID, :playlist, :playlist_data
    
    def initialize(link = nil)
      counter = 'video0000'
      link ||= link_generator
      @playlist_data = parse_page(Nokogiri::HTML(open(link).read))
      @playlist_data.each_pair do |video_name, video_link|
        instance_variable_set('@playlistID', rand(1..(2**30 - 1)))
        instance_variable_set("@#{counter.succ!}", video_name => "https://#{video_link}")
        self.class.send(:attr_reader, counter)
      end
    end
    
    def link_generator(ln = nil)
      print 'Please Enter A Correct Playlist Link: ' unless ln || ARGV[0]
      ln ||= ARGV[0] || gets.chomp
      if ln && is_playlist?(ln)
        return ln
      else
        link_generator
      end
    end
    
    def parse_page(page)
      playlist_data = page.css('table.pl-video-table')
      if playlist_data
        songs_hashes = Hash[playlist_data.css('a.pl-video-title-link').map do |ln|
                              [ln.text.strip, "https://www.youtube.com#{ln['href']}".match(YoutubeRegex)[0]]
                            end]
      end
      songs_hashes
    end
    
    def is_playlist?(link)
      link.include? 'list' && 'playlist'
    end


    
    def save_playlist
      File.write("Playlist-#{Time.now.to_a[3..5].join('-')}.yml", @playlist_data.to_yaml)
    end

    def save_smplayer_playlist
      f = File.open("smplayerPlaylist-#{@playlistID}-#{Time.now.to_a[3..5].join('-')}.m3u", "w+")
      f.write("#EXTM3U\n")
      @playlist_data.each_pair do |k, v|
        f.puts("#EXTINF:0,#{k}\n#{v}\n")
      end
      f.close
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tubedl-0.1.7 lib/tubedl/playlist_parser.rb