Class: YoutubeTools::Downloader

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

Constant Summary collapse

/"fmt_stream_map": ("[\w |:\/\\.?=&\,%-]+")/
NAME_PATTERN =
/-\s+(.*)\s*/
QUALITY =
{ :low => 5, :mp4 => 18, :mid => 34, :hd => 22, :hight => 35 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href, options = {}) ⇒ Downloader

Returns a new instance of Downloader.



9
10
11
12
13
14
15
16
17
18
# File 'lib/youtube_tools/downloader.rb', line 9

def initialize(href, options={})
	@href = href
	@video_folder = options[:path].nil? ? FOLDER_PATH : options[:path]
	@quality = options[:quality].nil? ? set_quality(:low) : set_quality(options[:quality])
	@error_quality = 0
	@links = {}
	
	create_folder
	init_information
end

Instance Attribute Details

#full_pathObject

Returns the value of attribute full_path.



3
4
5
# File 'lib/youtube_tools/downloader.rb', line 3

def full_path
  @full_path
end

#hrefObject

Returns the value of attribute href.



3
4
5
# File 'lib/youtube_tools/downloader.rb', line 3

def href
  @href
end

Returns the value of attribute link_dw.



3
4
5
# File 'lib/youtube_tools/downloader.rb', line 3

def link_dw
  @link_dw
end

Returns the value of attribute links.



3
4
5
# File 'lib/youtube_tools/downloader.rb', line 3

def links
  @links
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/youtube_tools/downloader.rb', line 3

def name
  @name
end

#qualityObject

Returns the value of attribute quality.



3
4
5
# File 'lib/youtube_tools/downloader.rb', line 3

def quality
  @quality
end

Instance Method Details

#download_videoObject

method dowload video from selected folder



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/youtube_tools/downloader.rb', line 26

def download_video
	@percent = @total = 0
	print "Downloading...0%"
	open(@full_path, 'wb') do |file|
		file.write(open(@link_dw, :content_length_proc => lambda {|t|
	 			if t && 0 < t
			 		@total = t        
		 		end
			}, :progress_proc => lambda {|s|
				old_percent = @percent
				@percent = (s * 100)/@total
				print "..#{@percent}%" if @percent != old_percent
			}).read)
	end
	puts "Download complete!"
end

#set_quality(quality) ⇒ Object



20
21
22
23
# File 'lib/youtube_tools/downloader.rb', line 20

def set_quality(quality)
	return QUALITY[quality] if QUALITY.include? quality
	QUALITY[:low]
end