Class: YoutubeUtils

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

Overview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_vid(url) ⇒ Object

input: www.youtube.com/watch?v=cRdxXPV9GNQ output: cRdxXPV9GNQ



41
42
43
44
45
46
47
48
49
50
# File 'lib/youtube_utils.rb', line 41

def self.get_vid url
  querys = URI.parse(url).query.split('&')
querys.each {|x|
  a = x.split('=')
  if a[0] == 'v'
    return a[1]
  end
}
return nil
end

.type2suffix(type) ⇒ Object

input: video/webm; codecs=“vp8.0, v outut: webm



54
55
56
# File 'lib/youtube_utils.rb', line 54

def self.type2suffix type
  return type.split(';')[0].split('/')[1]
end

Instance Method Details

#get_videos(youtube_watch_url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/youtube_utils.rb', line 8

def get_videos youtube_watch_url
  res = get_webpage(youtube_watch_url);
  unless res.code == '200'
    puts res.code
    return []
  end
  
  hash = json_to_hash(get_PLAYER_CONFIG(res.body))
  unless hash
    puts "no PLAYER_CONFIG"
    return []
  end
  
  args = hash['args']
  unless args
    puts "no args"
    return []
  end
  
  result = []
  
html5_fmt_map = args['html5_fmt_map']
result.concat(convert_html5_fmt_map(html5_fmt_map)) if html5_fmt_map

fmt_stream_map = args['fmt_stream_map']
fmt_list = args['fmt_list']
result.concat(convert_fmt_stream_map(fmt_stream_map, fmt_list)) if fmt_list&&fmt_stream_map
  
  return result
end