feat: continuation script

This commit is contained in:
Alex 2022-03-31 15:22:22 +13:00
parent 62c8e90299
commit 79c6b9efdd
4 changed files with 75 additions and 14 deletions

View File

@ -77,11 +77,18 @@ export default {
methods: { methods: {
refreshRecommendations() { refreshRecommendations() {
this.$emit("scroll-to-top"); this.$emit("scroll-to-top");
const continuations = this.$store.state.recommendedVideos.continuations;
this.$store.commit("updateRecommendedVideos", []); this.$store.commit("updateRecommendedVideos", []);
this.$youtube this.$youtube
.recommend() .continuation(
continuations[1].reloadContinuationData.continuation,
"browse"
)
.then((result) => { .then((result) => {
if (result) this.$store.commit("updateRecommendedVideos", result[0]); if (result)
this.$youtube.recommend(result).then((result) => {
if (result) this.$store.commit("updateRecommendedVideos", result);
});
}) })
.catch((error) => this.$logger("Home Page (Nav Refresh)", error, true)); .catch((error) => this.$logger("Home Page (Nav Refresh)", error, true));
}, },

View File

@ -7,8 +7,8 @@
<div> <div>
<!-- Video Loading Animation --> <!-- Video Loading Animation -->
<vid-load-renderer v-if="!recommends" /> <vid-load-renderer v-if="!recommends.contents" />
<horizontal-list-renderer v-else :render="recommends" /> <horizontal-list-renderer v-else :render="recommends.contents[0]" />
</div> </div>
</template> </template>
@ -29,14 +29,12 @@ export default {
}, },
}, },
// The following code is only a demo for debugging purposes, note that each "shelfRenderer" has a "title" value that seems to align to the categories at the top of the vanilla yt app
mounted() { mounted() {
if (!this.recommends.items || !this.recommends.items.length) { if (!this.recommends.items || !this.recommends.items.length) {
this.$youtube this.$youtube
.recommend() .recommend()
.then((result) => { .then((result) => {
if (result) this.recommends = result[0]; if (result) this.recommends = result;
}) })
.catch((error) => this.$logger("Home Page", error, true)); .catch((error) => this.$logger("Home Page", error, true));
} }

View File

@ -95,6 +95,42 @@ class Innertube {
}; };
} }
async getContinuationsAsync(continuation, type) {
let data = { context: this.context, continuation: continuation };
let url
switch (type) {
case "browse":
url = `${constants.URLS.YT_BASE_API}/browse?key=${this.key}`;
break
case "search":
url = `${constants.URLS.YT_BASE_API}/search?key=${this.key}`;
break
case "next":
url = `${constants.URLS.YT_BASE_API}/next?key=${this.key}`;
break
default:
throw ("Invalid type")
}
const response = await Http.post({
url: url,
data: data,
headers: { "Content-Type": "application/json" },
}).catch((error) => error);
if (response instanceof Error) {
return {
success: false,
status_code: response.status,
message: response.message,
};
}
return {
success: true,
status_code: response.status,
data: response.data,
};
}
async getVidAsync(id) { async getVidAsync(id) {
let data = { context: this.context, videoId: id }; let data = { context: this.context, videoId: id };
const responseNext = await Http.post({ const responseNext = await Http.post({
@ -181,11 +217,9 @@ class Innertube {
response.data.output?.playabilityStatus?.status == ("ERROR" || undefined) response.data.output?.playabilityStatus?.status == ("ERROR" || undefined)
) )
throw new Error( throw new Error(
`Could not get information for video: ${ `Could not get information for video: ${response.status_code ||
response.status_code || response.data.output?.playabilityStatus?.status
response.data.output?.playabilityStatus?.status } - ${response.message || response.data.output?.playabilityStatus?.reason
} - ${
response.message || response.data.output?.playabilityStatus?.reason
}` }`
); );
const responseInfo = response.data.output; const responseInfo = response.data.output;

View File

@ -114,6 +114,8 @@ const innertubeModule = {
// Front page recommendation // Front page recommendation
async recommend() { async recommend() {
const response = await InnertubeAPI.getRecommendationsAsync(); const response = await InnertubeAPI.getRecommendationsAsync();
if (!response.success) if (!response.success)
throw new Error("An error occurred and innertube failed to respond"); throw new Error("An error occurred and innertube failed to respond");
@ -125,8 +127,28 @@ const innertubeModule = {
if (video) return video; if (video) return video;
}); });
console.log(final); const continuations = response.data.contents.singleColumnBrowseResultsRenderer.tabs[0]
return final; .tabRenderer.content.sectionListRenderer.continuations
console.log({ continuations: continuations, contents: final });
return { continuations: continuations, contents: final };
},
async recommendContinuation(response) {
const contents = response.continuationContents.sectionListContinuation.contents;
const final = contents.map((shelves) => {
const video = shelves.shelfRenderer?.content?.horizontalListRenderer;
if (video) return video;
});
const continuations = response.continuationContents.sectionListContinuation.continuations
return { continuations: continuations, contents: final };
},
async continuation(continuation, endpoint) {
const response = await InnertubeAPI.getContinuationsAsync(continuation, endpoint);
console.log(response)
if (!response.success) throw new Error("An error occurred and innertube failed to respond");
return response
}, },
async search(query) { async search(query) {