invidious-experimenting/src/invidious/trending.cr

46 lines
1.6 KiB
Crystal
Raw Normal View History

2019-06-29 07:47:56 +05:30
def fetch_trending(trending_type, region, locale)
2018-11-20 22:48:12 +05:30
region ||= "US"
region = region.upcase
trending = ""
plid = nil
2018-12-21 04:18:45 +05:30
if trending_type && trending_type != "Default"
2018-11-20 22:48:12 +05:30
trending_type = trending_type.downcase.capitalize
2020-06-16 04:03:23 +05:30
response = YT_POOL.client &.get("/feed/trending?gl=#{region}&hl=en").body
2018-11-20 22:48:12 +05:30
2019-07-11 17:57:42 +05:30
initial_data = extract_initial_data(response)
2018-11-20 22:48:12 +05:30
2019-07-11 17:57:42 +05:30
tabs = initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"]["content"]["sectionListRenderer"]["subMenu"]["channelListSubMenuRenderer"]["contents"].as_a
2018-11-20 22:48:12 +05:30
url = tabs.select { |tab| tab["channelListSubMenuAvatarRenderer"]["title"]["simpleText"] == trending_type }[0]?
if url
url["channelListSubMenuAvatarRenderer"]["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"]
2018-11-20 22:48:12 +05:30
url = url["channelListSubMenuAvatarRenderer"]["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"].as_s
2020-06-16 04:03:23 +05:30
url = "#{url}&gl=#{region}&hl=en"
2019-10-25 22:28:16 +05:30
trending = YT_POOL.client &.get(url).body
plid = extract_plid(url)
2018-11-20 22:48:12 +05:30
else
2020-06-16 04:03:23 +05:30
trending = YT_POOL.client &.get("/feed/trending?gl=#{region}&hl=en").body
2018-11-20 22:48:12 +05:30
end
else
2020-06-16 04:03:23 +05:30
trending = YT_POOL.client &.get("/feed/trending?gl=#{region}&hl=en").body
2018-11-20 22:48:12 +05:30
end
2020-06-16 04:03:23 +05:30
initial_data = extract_initial_data(trending)
trending = extract_videos(initial_data)
2018-11-20 22:48:12 +05:30
return {trending, plid}
end
def extract_plid(url)
2020-06-16 04:03:23 +05:30
return url.try { |i| URI.parse(i).query }
.try { |i| HTTP::Params.parse(i)["bp"] }
2020-03-31 00:57:07 +05:30
.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-16 04:03:23 +05:30
.try &.["44:0:embedded"]?.try &.["2:1:string"]?.try &.as_s
2018-11-20 22:48:12 +05:30
end