Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

move ignore video logic to server

Christian Fei85ea9b432ca0
diff --git a/client/components/video-element.js b/client/components/video-element.js
index 9de1921..71debaa 100644
--- a/client/components/video-element.js
+++ b/client/components/video-element.js
@@ -25,7 +25,6 @@ class VideoElement extends HTMLElement {
}
render () {
if (!this.video) return
- if (store.includes(store.ignoreVideoKey, this.video.id)) return this.remove()
this.classList.add('video')
this.dataset['videoId'] = this.video.id
this.dataset['date'] = this.video.publishedAt
@@ -133,10 +132,19 @@ class VideoElement extends HTMLElement {
`
}
}
- ignoreVideoHandler (event) {
+ async ignoreVideoHandler (event) {
event.preventDefault()
- store.push(store.ignoreVideoKey, this.video.id)
- this.render()
+
+ await fetch('/ignore-video', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ id: this.video.id }),
+ })
+ .then(() => this.remove())
+ .catch((error) => {
+ console.error('Error ignoring summary:', error)
+ this.render()
+ })
}
filterByChannelHandler (event) {
// find search input, set value to channelname
diff --git a/client/lib/store.js b/client/lib/store.js
index 882f083..c314d60 100644
--- a/client/lib/store.js
+++ b/client/lib/store.js
@@ -1,13 +1,11 @@
export default class Store {
showThumbnailsKey = 'showThumbnails'
lastVideosKey = 'lastVideos'
- ignoreVideoKey = 'ignoreVideo'
ignoreTermsKey = 'ignoreTerms'
constructor() {
if (!localStorage.getItem(this.showThumbnailsKey)) localStorage.setItem(this.showThumbnailsKey, 'true')
if (!localStorage.getItem(this.lastVideosKey)) localStorage.setItem(this.lastVideosKey, '{}')
- if (!localStorage.getItem(this.ignoreVideoKey)) localStorage.setItem(this.ignoreVideoKey, '[]')
if (!localStorage.getItem(this.ignoreTermsKey)) localStorage.setItem(this.ignoreTermsKey, '[]')
}
@@ -16,7 +14,7 @@ export default class Store {
localStorage.setItem(key, localStorage.getItem(key) === 'true' ? 'false' : 'true')
}
get(key) {
- if (![this.showThumbnailsKey, this.lastVideosKey, this.ignoreVideoKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
+ if (![this.showThumbnailsKey, this.lastVideosKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
return JSON.parse(localStorage.getItem(key))
}
set(key, value) {
@@ -27,7 +25,7 @@ export default class Store {
}
}
push(key, item) {
- if (![this.ignoreVideoKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
+ if (![this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
const list = this.get(key)
list.push(item)
this.set(key, list)
@@ -41,7 +39,7 @@ export default class Store {
return item
}
includes(key, item) {
- if (![this.ignoreVideoKey, this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
+ if (![this.ignoreTermsKey].includes(key)) return console.error('invalid key', key)
const list = this.get(key)
return list.includes(item)
diff --git a/client/main.js b/client/main.js
index 92ac463..d1e2d6b 100644
--- a/client/main.js
+++ b/client/main.js
@@ -28,7 +28,6 @@ eventSource.onmessage = (message) => {
const ignoredTerms = window.store.get(window.store.ignoreTermsKey)
const allVideos = data.videos
- .filter(video => !window.store.includes(window.store.ignoreVideoKey, video.id))
.filter(video => video.title.split(' ').every(word => !ignoredTerms.includes(word.toLowerCase().replace(/('s|"|,|:)/,''))))
.sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))
.filter((_, i) => i < 300)
@@ -113,9 +112,9 @@ $addIgnoredTerm.addEventListener('keyup', (event) => {
function applyIgnoredTerms (ignoredTerms) {
const $ignoredTerms = document.getElementById('ignored-terms')
- $ignoredTerms.innerHTML = ignoredTerms.map(term => `<li class="ignored-term">${term}</li>`).join('')
+ $ignoredTerms.innerHTML = ignoredTerms.map(term => /*html*/`<li class="ignored-term">${term}</li>`).join('')
if (ignoredTerms.length === 0) {
- $ignoredTerms.innerHTML = '<li>No ignored terms</li>'
+ $ignoredTerms.innerHTML = /*html*/`<li>No ignored terms</li>`
}
}
diff --git a/lib/repository.js b/lib/repository.js
index 9a09868..1014b85 100644
--- a/lib/repository.js
+++ b/lib/repository.js
@@ -33,6 +33,9 @@ export default class Repository {
}
getVideos () {
return this.videos
+ .filter(v => !v.ignored)
+ .sort((a, b) => +new Date(b.publishedAt) - +new Date(a.publishedAt))
+ .filter((_, i) => i < 100)
}
getVideo(id) {
return this.videos.find((v => v.id === id))
@@ -54,6 +57,13 @@ export default class Repository {
saveVideos() {
fs.writeFileSync(this.paths.videos, JSON.stringify(this.videos, null, 2))
}
+ ignoreVideo(id) {
+ const index = this.videos.findIndex(v => v.id === id)
+ if (index !== -1) {
+ this.videos[index].ignored = true
+ }
+ this.saveVideos()
+ }
setVideoTranscript(id, transcript = '') {
this.videos = this.videos.map(v => {
if (v.id === id) return Object.assign(v, {transcript})
diff --git a/lib/server.js b/lib/server.js
index 18662b0..12d76bd 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -31,6 +31,8 @@ export function createServer ({repo, connections = []}) {
return downloadVideoHandler(req, res, connections)
if (url.pathname === '/summarize-video' && req.method === 'POST')
return summarizeVideoHandler(req, res, connections)
+ if (url.pathname === '/ignore-video' && req.method === 'POST')
+ return ignoreVideoHandler(req, res, connections)
if (url.pathname.match('\/videos\/.*') && req.method === 'GET')
return watchVideoHandler(req, res)
if (url.pathname === '/main.css')
@@ -197,6 +199,14 @@ export function createServer ({repo, connections = []}) {
fileStream.pipe(res)
}
}
+
+ async function ignoreVideoHandler(req, res) {
+ const body = await getBody(req)
+ let { id } = JSON.parse(body)
+ repo.ignoreVideo(id)
+ res.writeHead(200)
+ return res.end()
+ }
}
async function updateAndPersistVideos (repo, callback = () => {}) {