A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
log updates
Christian Fei87a91cc89647
diff --git a/lib/server.js b/lib/server.jsindex e4f1b07..1ef3631 100644--- a/lib/server.js+++ b/lib/server.js@@ -7,15 +7,9 @@ import { downloadVideo, getVideosFor } from './youtube.js'export function createServer ({repo, connections = []}) {let lastAdded = Date.now()- updateAndPersistVideos(repo, ({name, videos}) => {- const newVideos = videos.filter(v => v.addedAt > lastAdded)- lastAdded = Date.now()- if (newVideos.length > 0) {- broadcastSSE(JSON.stringify({type: 'new-videos', name, videos: newVideos}), connections)- }- })-- setInterval(() => {+ runUpdateVideos()+ setInterval(runUpdateVideos, 1000 * 60 * 10)+ function runUpdateVideos() {console.log('update videos')updateAndPersistVideos(repo, ({name, videos}) => {const newVideos = videos.filter(v => v.addedAt > lastAdded)@@ -25,7 +19,7 @@ export function createServer ({repo, connections = []}) {broadcastSSE(JSON.stringify({type: 'new-videos', name, videos: newVideos}), connections)}})- }, 1000 * 60 * 10)+ }return http.createServer(async (req, res) => {const url = new URL(req.url, `http://${req.headers.host}`)@@ -112,7 +106,21 @@ export function createServer ({repo, connections = []}) {async function downloadVideoHandler(req, res, connections = []) {const body = await getBody(req)- const { id } = JSON.parse(body)+ let { id, url } = JSON.parse(body)++ if (url) {+ id = url.replace('https://www.youtube.com/watch?v=', '')+ downloadVideo(id, repo, (line) =>+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))+ .then((video) => {+ broadcastSSE(JSON.stringify({type: 'new-videos', videos: [video]}), connections)+ broadcastSSE(JSON.stringify({type: 'downloaded', videoId: video.id}), connections)+ })+ .catch((error) =>+ broadcastSSE(JSON.stringify({type: 'download-log-line', line: error.stderr}), connections))++ return+ }downloadVideo(id, repo, (line) =>broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))@@ -224,12 +232,13 @@ export function createServer ({repo, connections = []}) {async function updateAndPersistVideos (repo, callback = () => {}) {const channels = repo.getChannels()for (const channel of channels) {- updateAndPersistVideosForChannel(channel.name, repo, callback)+ await updateAndPersistVideosForChannel(channel.name, repo, callback)}}async function updateAndPersistVideosForChannel(name, repo, callback = () => {}) {let videos = await getVideosFor(name)+ console.log('received videos for ', name, videos?.length)if (videos) {videos = videos.map(v => patchVideo(v, repo))repo.upsertVideos(videos)