class YoutubeAudio::Formats

Attributes

url[R]

@return [String]

Public Class Methods

new(url) click to toggle source

@param url [String] youtube link

# File lib/youtube_audio/formats.rb, line 9
def initialize(url)
  @url = url
end

Public Instance Methods

formats() click to toggle source
# File lib/youtube_audio/formats.rb, line 13
def formats
  player_response.formats.select(&:audio?)
end
player_response() click to toggle source
# File lib/youtube_audio/formats.rb, line 17
def player_response
  @player_response ||= ScriptParser.new(script_js).player_response
end

Private Instance Methods

agent() click to toggle source

@return [Mechanize]

# File lib/youtube_audio/formats.rb, line 40
def agent
  @agent ||= ::Mechanize.new
  @agent.user_agent = user_agent
  @agent
end
page() click to toggle source
# File lib/youtube_audio/formats.rb, line 27
def page
  @page ||= agent.get(url)
end
script() click to toggle source

returns a script that contains youtube audio / video metadata @return [String]

# File lib/youtube_audio/formats.rb, line 33
def script
  @script ||= page.search('script').select do |html_content|
    html_content.inner_html.match('ytInitialPlayerConfig = {')
  end&.first&.content
end
script_js() click to toggle source
# File lib/youtube_audio/formats.rb, line 23
def script_js
  @script_js ||= script
end
user_agent() click to toggle source
# File lib/youtube_audio/formats.rb, line 46
def user_agent
  'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit' \
    '/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36'
end