Class: RubyTube::Client
- Inherits:
-
Object
- Object
- RubyTube::Client
- Defined in:
- lib/rubytube/client.rb
Instance Attribute Summary collapse
-
#embed_url ⇒ Object
Returns the value of attribute embed_url.
-
#stream_monostate ⇒ Object
Returns the value of attribute stream_monostate.
-
#video_id ⇒ Object
Returns the value of attribute video_id.
-
#watch_url ⇒ Object
Returns the value of attribute watch_url.
Instance Method Summary collapse
- #author ⇒ Object
- #bypass_age_gate ⇒ Object
- #channel_id ⇒ Object
- #check_availability ⇒ Object
- #fmt_streams ⇒ Object
-
#initialize(url) ⇒ Client
constructor
A new instance of Client.
- #js ⇒ Object
- #js_url ⇒ Object
- #keywords ⇒ Object
- #length ⇒ Object
- #streaming_data ⇒ Object
- #streams ⇒ Object
- #thumbnail_url ⇒ Object
- #title ⇒ Object
- #vid_info ⇒ Object
- #views ⇒ Object
- #watch_html ⇒ Object
Constructor Details
#initialize(url) ⇒ Client
Returns a new instance of Client.
5 6 7 8 9 10 11 12 |
# File 'lib/rubytube/client.rb', line 5 def initialize(url) self.video_id = Extractor.video_id(url) self.watch_url = "https://youtube.com/watch?v=#{video_id}" self. = "https://www.youtube.com/embed/#{video_id}" self.stream_monostate = Monostate.new end |
Instance Attribute Details
#embed_url ⇒ Object
Returns the value of attribute embed_url.
3 4 5 |
# File 'lib/rubytube/client.rb', line 3 def @embed_url end |
#stream_monostate ⇒ Object
Returns the value of attribute stream_monostate.
3 4 5 |
# File 'lib/rubytube/client.rb', line 3 def stream_monostate @stream_monostate end |
#video_id ⇒ Object
Returns the value of attribute video_id.
3 4 5 |
# File 'lib/rubytube/client.rb', line 3 def video_id @video_id end |
#watch_url ⇒ Object
Returns the value of attribute watch_url.
3 4 5 |
# File 'lib/rubytube/client.rb', line 3 def watch_url @watch_url end |
Instance Method Details
#author ⇒ Object
152 153 154 155 156 157 |
# File 'lib/rubytube/client.rb', line 152 def return @author if @author @author = vid_info["videoDetails"]["author"] @author end |
#bypass_age_gate ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rubytube/client.rb', line 119 def bypass_age_gate it = InnerTube.new(client: "ANDROID_EMBED") resp = it.player(video_id) status = resp["playabilityStatus"]["status"] if status == "UNPLAYABLE" raise VideoUnavailable.new(video_id) end @vid_info = resp end |
#channel_id ⇒ Object
166 167 168 169 170 171 |
# File 'lib/rubytube/client.rb', line 166 def channel_id return @channel_id if @channel_id @channel_id = vid_info["videoDetails"]["channelId"] @channel_id end |
#check_availability ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rubytube/client.rb', line 67 def check_availability status, = Extractor.playability_status(watch_html) .each do |reason| case status when "UNPLAYABLE" case reason when "Join this channel to get access to members-only content like this video, and other exclusive perks." raise MembersOnly.new(video_id) when "This live stream recording is not available." raise RecordingUnavailable.new(video_id) else raise VideoUnavailable.new(video_id) end when "LOGIN_REQUIRED" if reason == "This is a private video. Please sign in to verify that you may see it." raise VideoPrivate.new(video_id) end when "ERROR" if reason == "Video unavailable" raise VideoUnavailable.new(video_id) end when "LIVE_STREAM" raise LiveStreamError.new(video_id) end end end |
#fmt_streams ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rubytube/client.rb', line 42 def fmt_streams check_availability return @fmt_streams if @fmt_streams @fmt_streams = [] stream_manifest = Extractor.apply_descrambler(streaming_data) begin Extractor.apply_signature(stream_manifest, vid_info, js) rescue ExtractError js = nil js_url = nil Extractor.apply_signature(stream_manifest, vid_info, js) end for stream in stream_manifest @fmt_streams << Stream.new(stream, stream_monostate) end stream_monostate.title = title stream_monostate.duration = length @fmt_streams end |
#js ⇒ Object
21 22 23 24 25 26 |
# File 'lib/rubytube/client.rb', line 21 def js return @js if @js @js = Request.get(js_url) @js end |
#js_url ⇒ Object
28 29 30 31 32 33 |
# File 'lib/rubytube/client.rb', line 28 def js_url return @js_url if @js_url @js_url = Extractor.js_url(watch_html) @js_url end |
#keywords ⇒ Object
159 160 161 162 163 164 |
# File 'lib/rubytube/client.rb', line 159 def keywords return @keywords if @keywords @keywords = vid_info["videoDetails"]["keywords"] @keywords end |
#length ⇒ Object
138 139 140 141 142 143 |
# File 'lib/rubytube/client.rb', line 138 def length return @length if @length @length = vid_info["videoDetails"]["lengthSeconds"].to_i @length end |
#streaming_data ⇒ Object
35 36 37 38 39 40 |
# File 'lib/rubytube/client.rb', line 35 def streaming_data return vid_info["streamingData"] if vid_info && vid_info.key?("streamingData") bypass_age_gate vid_info["streamingData"] end |
#streams ⇒ Object
95 96 97 98 99 100 |
# File 'lib/rubytube/client.rb', line 95 def streams return @streams if @streams check_availability @streams = StreamQuery.new(fmt_streams) end |
#thumbnail_url ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/rubytube/client.rb', line 102 def thumbnail_url thumbs = vid_info.fetch("videoDetails", {}).fetch("thumbnail", {}).fetch("thumbnails", []) return thumbs[-1]["url"] if thumbs.size > 0 "https://img.youtube.com/vi/#{ideo_id}/maxresdefault.jpg" end |
#title ⇒ Object
131 132 133 134 135 136 |
# File 'lib/rubytube/client.rb', line 131 def title return @title if @title @title = vid_info["videoDetails"]["title"] @title end |
#vid_info ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/rubytube/client.rb', line 110 def vid_info return @vid_info if @vid_info it = InnerTube.new @vid_info = it.player(video_id) @vid_info end |
#views ⇒ Object
145 146 147 148 149 150 |
# File 'lib/rubytube/client.rb', line 145 def views return @views if @views @views = vid_info["videoDetails"]["viewCount"].to_i @views end |