A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
breaking: videos as array
Christian Fei1acf739fccda
diff --git a/client/main.js b/client/main.jsindex c563597..abf285a 100644--- a/client/main.js+++ b/client/main.js@@ -27,8 +27,7 @@ eventSource.onmessage = (message) => {$videosContainer.innerHTML = ''const ignoredTerms = window.store.get(window.store.ignoreTermsKey)- let allVideos = Object.entries(data.videos).reduce((acc, curr) => acc.concat(curr[1]), [])- allVideos = allVideos+ 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))@@ -39,9 +38,15 @@ eventSource.onmessage = (message) => {updateDownloadedVideos(allVideos)updateSummarizedVideos(allVideos)- const channelsList = Object.entries(data.videos).map(([channelName, _]) => channelName)+ const channelsList = data.videos.reduce((acc, video) => {+ if (!acc.includes(video.channelName)) acc.push(video.channelName)+ return acc+ }, []).join(',')document.querySelector('channels-list').dataset['list'] = channelsList}+ if (data.type === 'channel' && data.name && data.videos) {+ console.error('UNHANDLED', data)+ }if (data.type === 'summary' && data.videoId && data.summary && data.transcript) {;[...document.querySelectorAll(`[data-video-id="${data.videoId}"]`)].forEach($video => {if (!$video.dataset['data']) returndiff --git a/lib/repository.js b/lib/repository.jsindex e871561..bc27df4 100644--- a/lib/repository.js+++ b/lib/repository.js@@ -9,7 +9,7 @@ export default class Repository {channels: './data/channels.json',}- if (!fs.existsSync(this.paths.videos)) fs.writeFileSync(this.paths.videos, '{}')+ if (!fs.existsSync(this.paths.videos)) fs.writeFileSync(this.paths.videos, '[]')if (!fs.existsSync(this.paths.channels)) fs.writeFileSync(this.paths.channels, '[]')try {@@ -31,43 +31,46 @@ export default class Repository {saveChannels() {fs.writeFileSync(this.paths.channels, JSON.stringify(this.channels, null, 2))}- getVideos () { return this.videos }+ getVideos () {+ return this.videos+ }getVideo(id) {- return Object.values(this.videos).flat().find(video => video.id === id) || null+ return this.videos.find((v => v.id === id))}getChannelVideos (name) {- return this.videos[name]+ return this.videos.filter(v => v.channelName === name)}- saveChannelVideos(channelName, videos) {- this.videos[channelName] = videos+ upsertVideos(videos) {+ videos.forEach(v => {+ const index = this.videos.findIndex(video => video.id === v.id)+ if (index !== -1) {+ this.videos[index] = v+ } else {+ this.videos.push(v)+ }+ })this.saveVideos()}saveVideos() {fs.writeFileSync(this.paths.videos, JSON.stringify(this.videos, null, 2))}- setVideoTranscript(id, transcript) {- const video = this.getVideo(id)- if (!video) return- this.videos[video.channelName] = this.videos[video.channelName].map(v => {+ setVideoTranscript(id, transcript = '') {+ this.videos = this.videos.map(v => {if (v.id === id) return Object.assign(v, {transcript})return v})this.saveVideos()}- setVideoSummary(id, summary) {- const video = this.getVideo(id)- if (!video) return- this.videos[video.channelName] = this.videos[video.channelName].map(v => {+ setVideoSummary(id, summary = '') {+ this.videos = this.videos.map(v => {if (v.id === id) return Object.assign(v, {summary})return v})this.saveVideos()}- setVideoDownloaded(id) {- const video = this.getVideo(id)- if (!video) return- this.videos[video.channelName] = this.videos[video.channelName].map(v => {- if (v.id === id) return Object.assign(v, {downloaded: true})+ setVideoDownloaded(id, downloaded = true) {+ this.videos = this.videos.map(v => {+ if (v.id === id) return Object.assign(v, {downloaded})return v})this.saveVideos()diff --git a/lib/server.js b/lib/server.jsindex 5b522ed..9062b95 100644--- a/lib/server.js+++ b/lib/server.js@@ -86,7 +86,7 @@ export function createServer ({repo, connections = []}) {const videos = await updateAndPersistVideosForChannel(name, repo)if (Array.isArray(videos)) {repo.addChannel(name)- broadcastSSE(JSON.stringify({type: 'all', videos}), connections)+ broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)res.writeHead(201, { 'Content-Type': 'text/plain' })return res.end('Channel added')}@@ -208,18 +208,15 @@ async function updateAndPersistVideosForChannel(name, repo, callback = () => {})let videos = await getVideosFor(name)if (videos) {videos = videos.map(v => patchVideo(v, repo))- repo.saveChannelVideos(name, videos)+ repo.upsertVideos(videos)callback({name, videos})}return videos}function patchVideo(video, repo) {- const existingVideos = repo.getVideos()- const existingChannelVideos = existingVideos[video.channelName]- if (existingChannelVideos) {- const existingVideo = existingChannelVideos.find(v => v.id === video.id)- Object.assign(video, existingVideo)- }+ const savedVideo = repo.getVideo(video.id)+ if (!savedVideo) return video+ Object.assign(savedVideo, video)return video}\ No newline at end of file