mirror of
https://gitea.invidious.io/iv-org/invidious-copy-2022-04-11.git
synced 2024-08-15 00:43:26 +00:00
Add abilty to fetch histortical community posts
This commit is contained in:
parent
2af442ca2f
commit
a35942a1b3
5 changed files with 49 additions and 13 deletions
src/invidious
|
@ -1,13 +1,31 @@
|
||||||
# Fetches channel community posts for the initial page
|
# Fetches channel community posts for the initial page
|
||||||
def fetch_channel_community(ucid)
|
def fetch_channel_community(ucid)
|
||||||
initial_data = YoutubeAPI.browse(ucid, params: "Egljb21tdW5pdHk%3D")
|
initial_data = YoutubeAPI.browse(ucid, params: "Egljb21tdW5pdHk%3D")
|
||||||
return extract_items(initial_data)
|
continuation_token = fetch_continuation_token(initial_data)
|
||||||
|
cursor = continuation_token ? extract_channel_community_cursor(continuation_token) : nil
|
||||||
|
|
||||||
|
return extract_items(initial_data), cursor
|
||||||
end
|
end
|
||||||
|
|
||||||
# Fetches the next batch of community posts after the given cursor
|
# Fetches the next batch of community posts after the given cursor
|
||||||
def fetch_channel_community(ucid, cursor)
|
def fetch_channel_community(ucid, cursor, skip_full_page_check = false)
|
||||||
continuation = produce_channel_community_continuation(ucid, cursor)
|
continuation = produce_channel_community_continuation(ucid, cursor)
|
||||||
initial_data = YoutubeAPI.browse(continuation)
|
initial_data = YoutubeAPI.browse(continuation)
|
||||||
|
|
||||||
|
continuation_token = fetch_continuation_token(initial_data)
|
||||||
|
cursor = continuation_token ? extract_channel_community_cursor(continuation_token) : nil
|
||||||
|
items = extract_items(initial_data)
|
||||||
|
|
||||||
|
if skip_full_page_check
|
||||||
|
else
|
||||||
|
# We want at least four items per page
|
||||||
|
until items.size >= 4 || !cursor
|
||||||
|
more_items, cursor = fetch_channel_community(ucid, cursor, skip_full_page_check = true)
|
||||||
|
items = items + more_items
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return items, cursor
|
||||||
end
|
end
|
||||||
|
|
||||||
def produce_channel_community_continuation(ucid, cursor)
|
def produce_channel_community_continuation(ucid, cursor)
|
||||||
|
@ -15,6 +33,7 @@ def produce_channel_community_continuation(ucid, cursor)
|
||||||
"80226972:embedded" => {
|
"80226972:embedded" => {
|
||||||
"2:string" => ucid,
|
"2:string" => ucid,
|
||||||
"3:string" => cursor || "",
|
"3:string" => cursor || "",
|
||||||
|
"35:string" => "backstage-item-section",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -517,6 +517,8 @@ private module Extractors
|
||||||
raw_items = content["items"].as_a
|
raw_items = content["items"].as_a
|
||||||
elsif content = target["continuationItems"]?
|
elsif content = target["continuationItems"]?
|
||||||
raw_items = content.as_a
|
raw_items = content.as_a
|
||||||
|
elsif content = target["itemSectionContinuation"]?
|
||||||
|
raw_items = content["contents"].as_a
|
||||||
end
|
end
|
||||||
|
|
||||||
return raw_items
|
return raw_items
|
||||||
|
@ -625,6 +627,7 @@ def extract_items(initial_data : Hash(String, JSON::Any), author_fallback : Stri
|
||||||
if unpackaged_data = initial_data["contents"]?.try &.as_h
|
if unpackaged_data = initial_data["contents"]?.try &.as_h
|
||||||
elsif unpackaged_data = initial_data["response"]?.try &.as_h
|
elsif unpackaged_data = initial_data["response"]?.try &.as_h
|
||||||
elsif unpackaged_data = initial_data.dig?("onResponseReceivedActions", 0).try &.as_h
|
elsif unpackaged_data = initial_data.dig?("onResponseReceivedActions", 0).try &.as_h
|
||||||
|
elsif unpackaged_data = initial_data.dig?("onResponseReceivedEndpoints", 0).try &.as_h
|
||||||
else
|
else
|
||||||
unpackaged_data = initial_data
|
unpackaged_data = initial_data
|
||||||
end
|
end
|
||||||
|
|
|
@ -242,17 +242,22 @@ end
|
||||||
|
|
||||||
def fetch_continuation_token(items : Array(JSON::Any))
|
def fetch_continuation_token(items : Array(JSON::Any))
|
||||||
# Fetches the continuation token from an array of items
|
# Fetches the continuation token from an array of items
|
||||||
return items.last["continuationItemRenderer"]?
|
return items.last["continuationItemRenderer"]?.try &.["continuationEndpoint"]["continuationCommand"]["token"].as_s
|
||||||
.try &.["continuationEndpoint"]["continuationCommand"]["token"].as_s
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_continuation_token(initial_data : Hash(String, JSON::Any))
|
def fetch_continuation_token(initial_data : Hash(String, JSON::Any))
|
||||||
# Fetches the continuation token from initial data
|
# Fetches the continuation token from initial data
|
||||||
if initial_data["onResponseReceivedActions"]?
|
if target = initial_data["onResponseReceivedActions"]? || initial_data["onResponseReceivedEndpoints"]?
|
||||||
continuation_items = initial_data["onResponseReceivedActions"][0]["appendContinuationItemsAction"]["continuationItems"]
|
continuation_items = target.dig(0, "appendContinuationItemsAction", "continuationItems")
|
||||||
else
|
else
|
||||||
tab = extract_selected_tab(initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"])
|
tab = extract_selected_tab(initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"])
|
||||||
continuation_items = tab["content"]["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"][0]["gridRenderer"]["items"]
|
target = tab.dig("content", "sectionListRenderer", "contents", 0, "itemSectionRenderer", "contents")
|
||||||
|
|
||||||
|
if continuation_items = target[0]["gridRenderer"]?
|
||||||
|
continuation_items = continuation_items["items"]
|
||||||
|
else
|
||||||
|
continuation_items = target
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return fetch_continuation_token(continuation_items.as_a)
|
return fetch_continuation_token(continuation_items.as_a)
|
||||||
|
|
|
@ -75,14 +75,13 @@ module Invidious::Routes::Channels
|
||||||
end
|
end
|
||||||
|
|
||||||
if continuation
|
if continuation
|
||||||
items = fetch_channel_community(ucid, continuation)
|
items, cursor = fetch_channel_community(ucid, continuation)
|
||||||
else
|
else
|
||||||
items = fetch_channel_community(ucid)
|
items, cursor = fetch_channel_community(ucid)
|
||||||
end
|
end
|
||||||
|
|
||||||
pp items
|
|
||||||
|
|
||||||
return env.redirect "/channel/#{channel.ucid}" if !items || items.empty?
|
return env.redirect "/channel/#{channel.ucid}" if !items || items.empty?
|
||||||
|
|
||||||
templated "community"
|
templated "community"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -111,3 +111,13 @@
|
||||||
</div>
|
</div>
|
||||||
<%- end%>
|
<%- end%>
|
||||||
|
|
||||||
|
<% if cursor %>
|
||||||
|
<div class="pure-g h-box v-box">
|
||||||
|
<div class="pure-u-1 pure-u-md-4-5"></div>
|
||||||
|
<div class="pure-u-1 pure-u-lg-1-5" style="text-align:right">
|
||||||
|
<a href="/channel/<%= channel.ucid %>/community?continuation=<%=cursor%>">
|
||||||
|
<%= translate(locale, "Next page") %>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
Loading…
Reference in a new issue