Class: RubyTube::InnerTube

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

Constant Summary collapse

DEFALUT_CLIENTS =
{
  "WEB" => {
    context: {
      client: {
        clientName: "WEB",
        clientVersion: "2.20200720.00.02"
      }
    },
    header: {"User-Agent": "Mozilla/5.0"},
    api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
  },
  "ANDROID_MUSIC" => {
    context: {
      client: {
        clientName: "ANDROID_MUSIC",
        clientVersion: "5.16.51",
        androidSdkVersion: 30
      }
    },
    header: {"User-Agent": "com.google.android.apps.youtube.music/"},
    api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
  },
  "ANDROID_EMBED" => {
    context: {
      client: {
        clientName: "ANDROID_EMBEDDED_PLAYER",
        clientVersion: "17.31.35",
        clientScreen: "EMBED",
        androidSdkVersion: 30
      }
    },
    header: {"User-Agent": "com.google.android.youtube/"},
    api_key: "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
  }
}
BASE_URL =
"https://www.youtube.com/youtubei/v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: "WEB", use_oauth: false, allow_cache: false) ⇒ InnerTube

Returns a new instance of InnerTube.



43
44
45
46
47
48
49
# File 'lib/rubytube/innertube.rb', line 43

def initialize(client: "WEB", use_oauth: false, allow_cache: false)
  self.context = DEFALUT_CLIENTS[client][:context]
  self.header = DEFALUT_CLIENTS[client][:header]
  self.api_key = DEFALUT_CLIENTS[client][:api_key]
  self.use_oauth = use_oauth
  self.allow_cache = allow_cache
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



41
42
43
# File 'lib/rubytube/innertube.rb', line 41

def access_token
  @access_token
end

#allow_cacheObject

Returns the value of attribute allow_cache.



41
42
43
# File 'lib/rubytube/innertube.rb', line 41

def allow_cache
  @allow_cache
end

#api_keyObject

Returns the value of attribute api_key.



41
42
43
# File 'lib/rubytube/innertube.rb', line 41

def api_key
  @api_key
end

#contextObject

Returns the value of attribute context.



41
42
43
# File 'lib/rubytube/innertube.rb', line 41

def context
  @context
end

#expiresObject

Returns the value of attribute expires.



41
42
43
# File 'lib/rubytube/innertube.rb', line 41

def expires
  @expires
end

#headerObject

Returns the value of attribute header.



41
42
43
# File 'lib/rubytube/innertube.rb', line 41

def header
  @header
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



41
42
43
# File 'lib/rubytube/innertube.rb', line 41

def refresh_token
  @refresh_token
end

#use_oauthObject

Returns the value of attribute use_oauth.



41
42
43
# File 'lib/rubytube/innertube.rb', line 41

def use_oauth
  @use_oauth
end

Instance Method Details

#cache_tokensObject



51
52
53
54
55
# File 'lib/rubytube/innertube.rb', line 51

def cache_tokens
  nil unless allow_cache

  # TODO:
end

#fetch_bearer_tokenObject



61
62
63
# File 'lib/rubytube/innertube.rb', line 61

def fetch_bearer_token
  # TODO:
end

#player(video_id) ⇒ Object



98
99
100
101
102
103
# File 'lib/rubytube/innertube.rb', line 98

def player(video_id)
  endpoint = "#{BASE_URL}/player"
  query = {"videoId" => video_id}

  send(endpoint, query, {context: context})
end

#refresh_bearer_token(force: false) ⇒ Object



57
58
59
# File 'lib/rubytube/innertube.rb', line 57

def refresh_bearer_token(force: false)
  # TODO:
end

#send(endpoint, query, data) ⇒ Object



65
66
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
94
95
96
# File 'lib/rubytube/innertube.rb', line 65

def send(endpoint, query, data)
  if use_oauth
    query.delete(:key)
  end

  headers = {
    "Content-Type": "application/json"
  }

  if use_oauth
    if access_token
      refresh_bearer_token
      headers["Authorization"] = "Bearer #{access_token}"
    else
      fetch_bearer_token
      headers["Authorization"] = "Bearer #{access_token}"
    end
  end

  options = {}
  options[:headers] = headers.merge(header)

  options[:query] = {
    key: api_key,
    contentCheckOk: true,
    racyCheckOk: true
  }.merge(query)
  options[:data] = data

  resp = Request.post(endpoint, options)
  JSON.parse(resp)
end