Class: Cherrypicker::Youtube

Inherits:
PluginBase show all
Defined in:
lib/cherrypicker/plugins/youtube.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PluginBase

inherited

Constructor Details

#initialize(link, opts = {}) ⇒ Youtube

Returns a new instance of Youtube.



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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cherrypicker/plugins/youtube.rb', line 13

def initialize(link, opts={})
 
   o = {
     :location => nil,
   }.merge(opts)
 
   @link         = link
   @filename     = ""
   @location     = o[:location]
   @download_url = ""
 
	video_id = @link[/v[\/=](.*)/,1]
   video_info = Cherrypicker::remote_query("http://www.youtube.com/get_video_info?video_id=#{video_id}").body
 
	#converting the huge infostring into a hash. simply by splitting it at the & and then splitting it into key and value arround the =
	#credit https://github.com/rb2k/viddl-rb/blob/master/plugins/youtube.rb
	video_info_hash = Hash[*video_info.split("&").collect { |v| 
     key, value = v.split "="
     value = CGI::unescape(value) if value
     if key =~ /_map/
       value = value.split(",")
       value = if key == "fmt_map"
           Hash[*value.collect{ |v| 
             k2, *v2 = v.split("/")
             [k2, v2]
           }.flatten(1)]
         elsif key == "fmt_url_map" || key == "fmt_stream_map"
           Hash[*value.collect { |v| v.split("|")}.flatten]
       end
     end
		[key, value]
	}.flatten]

	#Standard = 34 <- flv
	#Medium = 18 <- mp4
	#High = 35 <- flv
	#720p = 22 <- mp4
	#1080p = 37 <- mp4
	#mobile = 17 <- 3gp
	# --> 37 > 22 > 35 > 18 > 34 > 17
	formats = video_info_hash["fmt_map"].keys

	format_ext = {}
	format_ext["37"] = ["mp4", "MP4 Highest Quality 1920x1080"]
	format_ext["22"] = ["mp4", "MP4 1280x720"]
	format_ext["35"] = ["flv", "FLV 854x480"]
	format_ext["34"] = ["flv", "FLV 640x360"]
	format_ext["18"] = ["mp4", "MP4 480x270"]
	format_ext["17"] = ["3gp", "3gp"]
	format_ext["5"] = ["flv", "old default?"]

   download_url = video_info_hash["fmt_url_map"][formats.first]
	@filename = video_info_hash["title"].delete("\"'").gsub(/[^0-9A-Za-z]/, '_') + "." + format_ext[formats.first].first		
	@download_url = download_url
end

Instance Attribute Details

#download_urlObject

Returns the value of attribute download_url.



7
8
9
# File 'lib/cherrypicker/plugins/youtube.rb', line 7

def download_url
  @download_url
end

#filenameObject

Returns the value of attribute filename.



7
8
9
# File 'lib/cherrypicker/plugins/youtube.rb', line 7

def filename
  @filename
end

Returns the value of attribute link.



7
8
9
# File 'lib/cherrypicker/plugins/youtube.rb', line 7

def link
  @link
end

#locationObject

Returns the value of attribute location.



7
8
9
# File 'lib/cherrypicker/plugins/youtube.rb', line 7

def location
  @location
end

Class Method Details

.matches_provider?(url) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/cherrypicker/plugins/youtube.rb', line 9

def self.matches_provider?(url)
  url.include?("youtube.com")
end

Instance Method Details

#downloadObject



69
70
71
# File 'lib/cherrypicker/plugins/youtube.rb', line 69

def download
  Cherrypicker::download_file(@download_url, :location => @location, :filename  => @filename)
end