Class: FLV::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/flv/video.rb

Constant Summary collapse

FORMATS =
{
  :mp4      => ['mp4_url'],
  :flv      => ['flv_url', 'flv', 'video_url', 'file'],
  :flv_h264 => ['flv_h264']
}
FLASHVARS_XPATHS =
[
  '//@flashvars',
  '//param[@name="flashvars"]/@value',
  '//param[@name="FlashVars"]/@value'
]
EMBEDDED_FLASHVARS_XPATHS =
[
  '//*[contains(.,"flashvars")]',
  '//*[contains(.,"FlashVars")]'
]
FLASHVARS_JAVASCRIPT_XPATHS =
[
  '//script[contains(.,"flashvars")]',
  '//script[contains(.,"jwplayer")]',
]
FLASHVARS_FUNCTION_REGEXP =
/['"]flashvars['"]\s*,\s*['"]([^'"]+)['"]/
FLASHVARS_HASH_REGEXP =
/(?:flashvars\s*=\s*|jwplayer\(['"][a-z]+['"]\).setup\()(\{[^\}]+\})/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Video

Returns a new instance of Video.



18
19
20
21
# File 'lib/flv/video.rb', line 18

def initialize(url)
  @url = URI(url)
  @doc = Nokogiri::HTML(open(@url))
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



16
17
18
# File 'lib/flv/video.rb', line 16

def url
  @url
end

Instance Method Details

#extract_flashvars_from_embedded_htmlObject (protected)



78
79
80
81
82
83
84
# File 'lib/flv/video.rb', line 78

def extract_flashvars_from_embedded_html
  if (attr = @doc.at(EMBEDDED_FLASHVARS_XPATHS.join('|')))
    html = Nokogiri::HTML(attr.inner_text)

    extract_flashvars_from_html(html)
  end
end

#extract_flashvars_from_html(doc = @doc) ⇒ Object (protected)



67
68
69
70
71
# File 'lib/flv/video.rb', line 67

def extract_flashvars_from_html(doc=@doc)
  if (flashvars = doc.at(FLASHVARS_XPATHS.join('|')))
    URI::QueryParams.parse(flashvars.value)
  end
end

#extract_flashvars_from_javascriptObject (protected)



91
92
93
94
95
96
97
98
# File 'lib/flv/video.rb', line 91

def extract_flashvars_from_javascript
  if (script = @doc.at(FLASHVARS_JAVASCRIPT_XPATHS.join('|')))
    code = script.inner_text

    return extract_flashvars_from_javascript_function(code) ||
           extract_flashvars_from_javascript_hash(code)
  end
end

#extract_flashvars_from_javascript_function(code) ⇒ Object (protected)



102
103
104
105
106
# File 'lib/flv/video.rb', line 102

def extract_flashvars_from_javascript_function(code)
  if (match = code.match(FLASHVARS_FUNCTION_REGEXP))
    URI::QueryParams.parse(match[1])
  end
end

#extract_flashvars_from_javascript_hash(code) ⇒ Object (protected)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/flv/video.rb', line 110

def extract_flashvars_from_javascript_hash(code)
  regexp = lambda { |vars|
    
  }

  flashvars = {}

  vars   = FORMATS.values.flatten
  regexp = /['"](#{Regexp.union(vars)})['"]:\s*['"]([^'"]+)['"]/

  if (match = code.match(regexp))
    flashvars[match[1]] = URI.decode(match[2])
  end

  return flashvars
end

#flashvarsObject



27
28
29
30
31
32
33
34
# File 'lib/flv/video.rb', line 27

def flashvars
  @flashvars ||= (
    extract_flashvars_from_html ||
    extract_flashvars_from_embedded_html ||
    extract_flashvars_from_javascript ||
    {}
  )
end

#formatsObject



36
37
38
39
40
# File 'lib/flv/video.rb', line 36

def formats
  @formats ||= FORMATS.keys.select do |format|
    FORMATS[format].any? { |var| flashvars.has_key?(var) }
  end
end

#titleObject



23
24
25
# File 'lib/flv/video.rb', line 23

def title
  @title ||= @doc.at('//title').inner_text
end

#video_urlsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flv/video.rb', line 42

def video_urls
  @video_urls ||= begin
                    urls = {}

                    FORMATS.each do |format,vars|
                      vars.each do |var|
                        if flashvars.has_key?(var)
                          urls[format] = @url.merge(flashvars[var])
                          next
                        end
                      end
                    end

                    urls
                  end
end