2019-06-29 02:17:56 +00:00
|
|
|
def fetch_trending(trending_type, region, locale)
|
2018-11-20 17:18:12 +00:00
|
|
|
headers = HTTP::Headers.new
|
|
|
|
headers["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
|
|
|
|
|
|
|
|
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"
|
2018-11-20 17:18:12 +00:00
|
|
|
trending_type = trending_type.downcase.capitalize
|
|
|
|
|
2019-10-25 16:58:16 +00:00
|
|
|
response = YT_POOL.client &.get("/feed/trending?gl=#{region}&hl=en", headers).body
|
2018-11-20 17:18:12 +00:00
|
|
|
|
2019-07-11 12:27:42 +00:00
|
|
|
initial_data = extract_initial_data(response)
|
2018-11-20 17:18:12 +00:00
|
|
|
|
2019-07-11 12:27:42 +00:00
|
|
|
tabs = initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"]["content"]["sectionListRenderer"]["subMenu"]["channelListSubMenuRenderer"]["contents"].as_a
|
2018-11-20 17:18:12 +00:00
|
|
|
url = tabs.select { |tab| tab["channelListSubMenuAvatarRenderer"]["title"]["simpleText"] == trending_type }[0]?
|
|
|
|
|
|
|
|
if url
|
2019-04-15 00:04:10 +00:00
|
|
|
url["channelListSubMenuAvatarRenderer"]["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"]
|
2018-11-20 17:18:12 +00:00
|
|
|
url = url["channelListSubMenuAvatarRenderer"]["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"].as_s
|
|
|
|
url += "&disable_polymer=1&gl=#{region}&hl=en"
|
2019-10-25 16:58:16 +00:00
|
|
|
trending = YT_POOL.client &.get(url).body
|
2019-04-15 00:04:10 +00:00
|
|
|
plid = extract_plid(url)
|
2018-11-20 17:18:12 +00:00
|
|
|
else
|
2019-10-25 16:58:16 +00:00
|
|
|
trending = YT_POOL.client &.get("/feed/trending?gl=#{region}&hl=en&disable_polymer=1").body
|
2018-11-20 17:18:12 +00:00
|
|
|
end
|
|
|
|
else
|
2019-10-25 16:58:16 +00:00
|
|
|
trending = YT_POOL.client &.get("/feed/trending?gl=#{region}&hl=en&disable_polymer=1").body
|
2018-11-20 17:18:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
trending = XML.parse_html(trending)
|
|
|
|
nodeset = trending.xpath_nodes(%q(//ul/li[@class="expanded-shelf-content-item-wrapper"]))
|
|
|
|
trending = extract_videos(nodeset)
|
|
|
|
|
2019-04-15 00:04:10 +00:00
|
|
|
return {trending, plid}
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_plid(url)
|
2020-03-30 19:27:07 +00:00
|
|
|
plid = URI.parse(url)
|
|
|
|
.try { |i| HTTP::Params.parse(i.query.not_nil!)["bp"] }
|
|
|
|
.try { |i| URI.decode_www_form(i) }
|
|
|
|
.try { |i| Base64.decode(i) }
|
|
|
|
.try { |i| IO::Memory.new(i) }
|
|
|
|
.try { |i| Protodec::Any.parse(i) }
|
|
|
|
.try { |i| i["44:0:embedded"]["2:1:string"].as_s }
|
2019-04-15 00:04:10 +00:00
|
|
|
|
|
|
|
return plid
|
2018-11-20 17:18:12 +00:00
|
|
|
end
|