-
-
Notifications
You must be signed in to change notification settings - Fork 857
feat: add local personalized trending feed #4234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
9d7d555
fba0f4d
df5b3a7
15c71d6
ee55e97
3677031
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
| <template> | ||
| <h1 v-t="'titles.trending'" class="my-4 text-center font-bold" /> | ||
|
|
||
| <hr /> | ||
|
|
||
| <LoadingIndicatorPage | ||
| :show-content="videos.length != 0" | ||
| class="mx-2 grid grid-cols-1 gap-y-5 max-md:gap-x-3 sm:mx-0 sm:grid-cols-2 md:grid-cols-3 md:gap-x-6 lg:grid-cols-4 xl:grid-cols-5" | ||
|
|
@@ -18,29 +16,108 @@ import { useI18n } from "vue-i18n"; | |
| import LoadingIndicatorPage from "./LoadingIndicatorPage.vue"; | ||
| import VideoItem from "./VideoItem.vue"; | ||
| import { fetchJson, apiUrl } from "@/composables/useApi.js"; | ||
| import { getPreferenceString } from "@/composables/usePreferences.js"; | ||
| import { getPreferenceString, getPreferenceBoolean } from "@/composables/usePreferences.js"; | ||
| import { updateWatched } from "@/composables/useMisc.js"; | ||
| import { fetchDeArrowContent } from "@/composables/useSubscriptions.js"; | ||
| import { getHomePage } from "@/composables/useMisc.js"; | ||
|
|
||
| const route = useRoute(); | ||
| const router = useRouter(); | ||
| const { t } = useI18n(); | ||
|
|
||
| const videos = ref([]); | ||
|
|
||
| async function fetchTrending(region) { | ||
| return await fetchJson(apiUrl() + "/trending", { | ||
| region: region || "US", | ||
| function idbCursorToPromise(store, fn) { | ||
| return new Promise((resolve, reject) => { | ||
| const req = store.openCursor(); | ||
| req.onerror = () => reject(req.error); | ||
| req.onsuccess = e => { | ||
| const cursor = e.target.result; | ||
| if (cursor) { | ||
| fn(cursor.value); | ||
| cursor.continue(); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| async function getPreferredChannels() { | ||
| if (!window.db || !getPreferenceBoolean("watchHistory", false)) return []; | ||
|
|
||
| const tx = window.db.transaction("watch_history", "readonly"); | ||
| const store = tx.objectStore("watch_history"); | ||
| const counts = new Map(); | ||
|
|
||
| await idbCursorToPromise(store, video => { | ||
| if (video.uploaderUrl) { | ||
| const id = video.uploaderUrl.split("/").pop(); | ||
| counts.set(id, (counts.get(id) || 0) + 1); | ||
| } | ||
| }); | ||
|
|
||
| return Array.from(counts.entries()) | ||
| .sort((a, b) => b[1] - a[1]) | ||
| .slice(0, 10) | ||
| .map(([id]) => id); | ||
| } | ||
|
|
||
| async function fetchChannelVideos(channelIds) { | ||
| const results = await Promise.allSettled(channelIds.map(id => fetchJson(apiUrl() + "/channel/" + id))); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the unauthenticated channel feeds api, since this is quite expensive on the server? You can pass multiple channel IDs to it too.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. surely, ill implement the fix..but there seems to be another issue preventing the code in this PR from running, about a week ago i noticed that videos section in channels no longer load, and this affected the trending page too as it reads from there, i dont think its just my side, bcz the piped.video server also had the same issue. |
||
| return results | ||
| .filter(r => r.status === "fulfilled" && r.value?.relatedStreams) | ||
| .flatMap(r => r.value.relatedStreams.slice(0, 5)); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| function interleave(trending, recommended) { | ||
| const out = []; | ||
| let ti = 0, | ||
| ri = 0; | ||
| while (ti < trending.length || ri < recommended.length) { | ||
| if (ti < trending.length) out.push(trending[ti++]); | ||
| if (ti < trending.length) out.push(trending[ti++]); | ||
| if (ti < trending.length) out.push(trending[ti++]); | ||
| if (ri < recommended.length) out.push(recommended[ri++]); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| async function fetchTrending(region) { | ||
| const personalizedTrending = getPreferenceBoolean("personalizedTrending", false); | ||
| const personalizedTrendingOnly = getPreferenceBoolean("personalizedTrendingOnly", false); | ||
|
|
||
| if (!personalizedTrending) { | ||
| return await fetchJson(apiUrl() + "/trending", { region: region || "US" }); | ||
| } | ||
|
|
||
| const [trending, preferredChannels] = await Promise.all([ | ||
| personalizedTrendingOnly ? Promise.resolve([]) : fetchJson(apiUrl() + "/trending", { region: region || "US" }), | ||
| getPreferredChannels(), | ||
| ]); | ||
|
|
||
| if (preferredChannels.length === 0) return trending; | ||
|
|
||
| const recommended = await fetchChannelVideos(preferredChannels); | ||
|
|
||
| const seen = new Set(); | ||
| const dedup = arr => | ||
| arr.filter(v => { | ||
| const id = v.url?.split("v=")[1]; | ||
| if (!id || seen.has(id)) return false; | ||
| seen.add(id); | ||
| return true; | ||
| }); | ||
|
|
||
| if (personalizedTrendingOnly) return dedup(recommended); | ||
|
|
||
| return interleave(dedup(trending), dedup(recommended)); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| onMounted(() => { | ||
| if (route.path == import.meta.env.BASE_URL && getPreferenceString("homepage", "trending") == "feed") { | ||
| return; | ||
| } | ||
| let region = getPreferenceString("region", "US"); | ||
|
|
||
| const region = getPreferenceString("region", "US"); | ||
| fetchTrending(region).then(vids => { | ||
| videos.value = vids; | ||
| updateWatched(videos.value); | ||
|
|
@@ -52,7 +129,7 @@ onActivated(() => { | |
| document.title = t("titles.trending") + " - Piped"; | ||
| if (videos.value.length > 0) updateWatched(videos.value); | ||
| if (route.path == import.meta.env.BASE_URL) { | ||
| let homepage = getHomePage(); | ||
| const homepage = getHomePage(); | ||
| if (homepage !== undefined) router.push(homepage); | ||
| } | ||
| }); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.