Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

wip update videos

Christian Fei222ae6d3c61a
diff --git a/client/main.js b/client/main.js
index d1e2d6b..6f9c799 100644
--- a/client/main.js
+++ b/client/main.js
@@ -40,17 +40,17 @@ eventSource.onmessage = (message) => {
}, []).join(',')
document.querySelector('channels-list').dataset['list'] = channelsList
}
- if (data.type === 'channel' && data.name && data.videos) {
- console.warn('unhandled', data)
- // data.videos.forEach(video => {
- // for (const $videoElement of $videosContainer.querySelectorAll('video-element')) {
- // const videoDate = new Date($videoElement.dataset['date'])
- // if (new Date(video.publishedAt) < videoDate) {
- // // $videoElement.prepend(createVideoElement(video))
- // break
- // }
- // }
- // })
+ if (data.type === 'new-videos' && data.videos) {
+ console.warn('new-videos', data)
+ data.videos.forEach(video => {
+ for (const $videoElement of $videosContainer.querySelectorAll('video-element')) {
+ const videoDate = new Date($videoElement.dataset['date'])
+ if (new Date(video.publishedAt) < videoDate) {
+ $videoElement.parentNode.insertBefore(createVideoElement(video), $videoElement.nextSibling)
+ break
+ }
+ }
+ })
}
if (data.type === 'summary-error' && data.videoId) {
const $videoElement = document.querySelector(`[data-video-id="${data.videoId}"]`)
@@ -168,3 +168,5 @@ function createVideoElement (video) {
$video.dataset['videoId'] = video.id
return $video
}
+
+window.createVideoElement = createVideoElement
diff --git a/lib/repository.js b/lib/repository.js
index 1014b85..deca5d6 100644
--- a/lib/repository.js
+++ b/lib/repository.js
@@ -38,7 +38,7 @@ export default class Repository {
.filter((_, i) => i < 100)
}
getVideo(id) {
- return this.videos.find((v => v.id === id))
+ return this.videos.find(v => v.id === id)
}
getChannelVideos (name) {
return this.videos.filter(v => v.channelName === name)
diff --git a/lib/server.js b/lib/server.js
index a873154..6406486 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -6,15 +6,19 @@ import { handleSSE, broadcastSSE } from './sse.js'
import { downloadVideo, getVideosFor } from './youtube.js'
export function createServer ({repo, connections = []}) {
+ let lastAdded = Date.now()
updateAndPersistVideos(repo, ({name, videos}) => {
- broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
+ const newVideos = videos.filter(v => v.addedAt > lastAdded)
+ broadcastSSE(JSON.stringify({type: 'new-videos', name, videos: newVideos}), connections)
})
-
+
setInterval(() => {
+ console.log('update videos')
updateAndPersistVideos(repo, ({name, videos}) => {
- broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
+ const newVideos = videos.filter(v => v.addedAt > lastAdded)
+ broadcastSSE(JSON.stringify({type: 'new-videos', name, videos: newVideos}), connections)
})
- }, 1000 * 60 * 10)
+ }, 1000 * 60 * 1)
return http.createServer(async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`)
@@ -230,7 +234,7 @@ async function updateAndPersistVideosForChannel(name, repo, callback = () => {})
function patchVideo(video, repo) {
const savedVideo = repo.getVideo(video.id)
- if (!savedVideo) return video
+ if (!savedVideo) return Object.assign(video, {addedAt: Date.now()})
Object.assign(savedVideo, video)
return savedVideo
}
\ No newline at end of file