2019-06-29 02:17:56 +00:00
|
|
|
def fetch_trending(trending_type, region, locale)
|
2018-11-20 17:18:12 +00:00
|
|
|
region ||= "US"
|
|
|
|
region = region.upcase
|
|
|
|
|
|
|
|
trending = ""
|
2019-04-15 00:04:10 +00:00
|
|
|
plid = nil
|
|
|
|
|
2018-12-20 22:48:45 +00:00
|
|
|
if trending_type && trending_type != "Default"
|
2021-03-28 05:48:43 +00:00
|
|
|
if trending_type == "Music"
|
|
|
|
trending_type = 1
|
|
|
|
elsif trending_type == "Gaming"
|
|
|
|
trending_type = 2
|
|
|
|
elsif trending_type == "Movies"
|
|
|
|
trending_type = 3
|
|
|
|
end
|
2018-11-20 17:18:12 +00:00
|
|
|
|
2020-06-15 22:33:23 +00:00
|
|
|
response = YT_POOL.client &.get("/feed/trending?gl=#{region}&hl=en").body
|
2018-11-20 17:18:12 +00:00
|
|
|
|
2019-07-11 12:27:42 +00:00
|
|
|
initial_data = extract_initial_data(response)
|
2021-03-28 05:48:43 +00:00
|
|
|
url = initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][trending_type]["tabRenderer"]["endpoint"]["commandMetadata"]["webCommandMetadata"]["url"]
|
|
|
|
url = "#{url}&gl=#{region}&hl=en"
|
2018-11-20 17:18:12 +00:00
|
|
|
|
2021-03-28 05:48:43 +00:00
|
|
|
trending = YT_POOL.client &.get(url).body
|
|
|
|
plid = extract_plid(url)
|
2018-11-20 17:18:12 +00:00
|
|
|
else
|
2020-06-15 22:33:23 +00:00
|
|
|
trending = YT_POOL.client &.get("/feed/trending?gl=#{region}&hl=en").body
|
2018-11-20 17:18:12 +00:00
|
|
|
end
|
|
|
|
|
2020-06-15 22:33:23 +00:00
|
|
|
initial_data = extract_initial_data(trending)
|
|
|
|
trending = extract_videos(initial_data)
|
2018-11-20 17:18:12 +00:00
|
|
|
|
2019-04-15 00:04:10 +00:00
|
|
|
return {trending, plid}
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_plid(url)
|
2020-06-15 22:33:23 +00:00
|
|
|
return url.try { |i| URI.parse(i).query }
|
|
|
|
.try { |i| HTTP::Params.parse(i)["bp"] }
|
2020-03-30 19:27:07 +00:00
|
|
|
.try { |i| URI.decode_www_form(i) }
|
|
|
|
.try { |i| Base64.decode(i) }
|
|
|
|
.try { |i| IO::Memory.new(i) }
|
|
|
|
.try { |i| Protodec::Any.parse(i) }
|
2020-06-15 22:33:23 +00:00
|
|
|
.try &.["44:0:embedded"]?.try &.["2:1:string"]?.try &.as_s
|
2018-11-20 17:18:12 +00:00
|
|
|
end
|