Class: YoutubeShort

Inherits:
Object
  • Object
show all
Defined in:
lib/yt-download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = "", output = nil) ⇒ YoutubeShort

Returns a new instance of YoutubeShort.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yt-download.rb', line 14

def initialize(url="", output=nil)
	@shorts_url = url
	@shorts_id = url.match(%r"shorts\/([\w]+)").to_s
	if (! output.nil?)
		@path = output
	else
		@path = File.realpath(File.dirname(".")) + "/" # set default path where u running bin file
	end
	@formats = []
	@adaptiveFormats = []
end

Instance Attribute Details

#adaptiveFormatsObject (readonly)

Returns the value of attribute adaptiveFormats.



13
14
15
# File 'lib/yt-download.rb', line 13

def adaptiveFormats
  @adaptiveFormats
end

#formatsObject (readonly)

Returns the value of attribute formats.



13
14
15
# File 'lib/yt-download.rb', line 13

def formats
  @formats
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/yt-download.rb', line 13

def path
  @path
end

#shorts_idObject (readonly)

Returns the value of attribute shorts_id.



13
14
15
# File 'lib/yt-download.rb', line 13

def shorts_id
  @shorts_id
end

#shorts_urlObject (readonly)

Returns the value of attribute shorts_url.



13
14
15
# File 'lib/yt-download.rb', line 13

def shorts_url
  @shorts_url
end

Instance Method Details

#MainObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/yt-download.rb', line 70

def Main
	puts "\n [yt-download] downloading webpage: #{@shorts_id}"
	if (json_data = parseJS(getPage(@shorts_url)))
		json_data.each do | key, value |
			puts " [yt-download] #{key.gsub(/\w+/) do | word | word.capitalize end} : #{value}"
		end
		puts "\n 1). Audio with video (#{@formats.length()})\n 2). Audio / Video only (#{@adaptiveFormats.length()})\n"
		choice = prompt "\n • choice : "
		puts
		all_formats = [@formats, @adaptiveFormats]
		if (choice.to_i - 1) < all_formats.length
			index_formats = all_formats[choice.to_i-1]
			puts "         [:type, :quality, :bitrate] \n\n"
			index_formats.each_with_index do | string, index |
				puts " #{index + 1}). #{string['type']} / #{string['quality']} / #{string['bitrate']}"
			end
			choice = prompt "\n • choice : "
			if (choice.to_i - 1) < index_formats.length
				index_choice = index_formats[choice.to_i - 1]
				filename = json_data["title"] + "." + index_choice["type"].split("/")[-1]
				puts " [yt-download] downloading files: #{filename}"
				if (! index_choice["link"].nil?)
					download_stream(
						index_choice["link"],
						filename,
						@path
					)
					puts " [yt-download] saved as: #{@path}#{filename}"
				else
					abort(" •! exception with signatureCipher, try other shorts url!")
				end
			else
				abort(" •! must be < #{index_formats.length}")
			end
		else	
			abort(" •! must be < #{all_formats.length}")
		end
	else
		abort('failed get video info!')
	end
end

#parseJS(page) ⇒ Object



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
68
# File 'lib/yt-download.rb', line 26

def parseJS(page)
	formats = []
	adaptiveFormats = []
	if (js = page.match(%r"(?<=ytInitialPlayerResponse\s=\s)(.*?)(?=;(?:var\smeta|<\/script>))"))
		data = loadJson(js.to_s)
		if (data.has_key?("playabilityStatus"))
			if (data["playabilityStatus"]["status"].downcase == "ok")
				["formats","adaptiveFormats"].each { | format |
					data["streamingData"][format].each { | stream |
						quality = if stream.has_key?("qualityLabel") then stream["qualityLabel"] else "" end
						opts = {
							"link" => stream["url"],
							"type" => stream["mimeType"].split(";")[0],
							"quality" => quality,
							"bitrate" => stream["bitrate"]
						}
						eval %{
							if !@#{format}.include?(opts)
								@#{format}.append(opts)
							end
						}
								
					}
				}
				video_info = data["videoDetails"]
				tags = video_info.has_key?("keywords") ? video_info["keywords"] : []
				return {
					"author" => video_info["author"],
					"title" => video_info["title"],
					"duration" => video_info["lengthSeconds"],
					"tags" => tags.join(', '),
					"description" => video_info["shortDescription"]
				}
			else
				return false
			end
		else
			return false
		end
	else
		abort(" •! failed get js data!")
	end
end