A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
download and watch videos
Christian Fei156885cba81a
diff --git a/index.html b/index.htmlindex 266004b..6ea3a1b 100644--- a/index.html+++ b/index.html@@ -61,6 +61,10 @@text-decoration: none;color: black;}++ .video .actions {+ font-size: 0.9rem;+ }</style><style>#open-settings {@@ -91,7 +95,6 @@opacity: 0.85;}</style>-<style>body > header {/* background-color: #f0f0f0; */@@ -163,7 +166,6 @@function handleFoldedChannels (foldedChannels = true) {if (foldedChannels) {- // document.body.classList.add('hide-thumbnails')[...document.querySelectorAll('.channels-container details')].forEach(d => d.open = true)} else {@@ -200,7 +202,7 @@}})- document.getElementById('add-channel-form').addEventListener('submit', async (event) => {+ document.getElementById('add-channel-form').addEventListener('submit', (event) => {event.preventDefault()const channelName = document.getElementById('channel-name').value@@ -240,11 +242,15 @@const $video = document.createElement('div')$video.classList.add('video')$video.innerHTML = `- <a href="${video.url}" target="_blank">+ <div _href="${video.url}" target="_blank"><img src="${video.thumbnail}"/><span>${video.publishedTime}</span> | <span>${video.viewCount}</span> | <span>${video.duration || 'N/A'}</span><br/>${video.title}- </a>+ <div class="actions">+ ${!video.downloaded ? `<span class="action download" data-video-id="${video.id}">⬇️ Download</span>` : ''}+ ${video.downloaded ? `<a href="/videos/${video.id}" target="_blank" class="action watch" data-video-id="${video.id}">👀 watch</span>`: ``}+ </div>+ </div>`$videosContainer.appendChild($video)}@@ -264,6 +270,26 @@handleShowThumbnails(localStorage.getItem(showThumbnailsKey) === 'false')handleFoldedChannels(localStorage.getItem(foldedChannelsKey) === 'false')})+ .then(() => {+ // handle download video action+ document.querySelectorAll('.video .actions .action.download').forEach($downloadAction => {+ $downloadAction.addEventListener('click', (event) => {+ console.log(`Download video from`)+ fetch('/download-video', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ id: event.target.dataset.videoId }),+ })+ .then(() => {+ console.log('Download started')+ alert('Download started')+ })+ .catch((error) => {+ console.error('Error starting download:', error)+ })+ })+ })+ })</script></body></html>diff --git a/server.js b/server.jsindex efc70ca..2a9586a 100644--- a/server.js+++ b/server.js@@ -4,6 +4,7 @@ import { execa } from 'execa'import fs from 'fs'fs.mkdirSync('./data', { recursive: true })+fs.mkdirSync('./data/videos', { recursive: true })class Repository {constructor() {@@ -34,6 +35,9 @@ class Repository {fs.writeFileSync(this.paths.channels, JSON.stringify(this.channels, null, 2))}getVideos () { return this.videos }+ getVideo(id) {+ return Object.values(this.videos).flat().find(video => video.id === id) || null+ }saveChannelVideos(channelName, videos) {this.videos[channelName] = videosthis.saveVideos()@@ -41,6 +45,16 @@ class Repository {saveVideos() {fs.writeFileSync(this.paths.videos, JSON.stringify(this.videos, null, 2))}+ setVideoDownloaded(id) {+ const video = this.getVideo(id)+ console.log('setVideoDownloaded', id, video)+ if (!video) return+ this.videos[video.channelName] = this.videos[video.channelName].map(v => {+ if (v.id === id) return Object.assign(v, {downloaded: true})+ return v+ })+ this.saveVideos()+ }}async function main ({port = 3000} = {}) {@@ -48,6 +62,11 @@ async function main ({port = 3000} = {}) {await updateAndPersistVideos(repo)+ fs.readdirSync('./data/videos').forEach(file => {+ const videoId = file.replace('.mp4', '')+ repo.setVideoDownloaded(videoId)+ })+createServer({repo, port}).listen(port, () => {console.log(`Server running at http://localhost:${port}`)@@ -69,9 +88,10 @@ async function updateAndPersistVideos (repo) {console.log('All videos updated')}-async function downloadVideo(url, path) {+async function downloadVideo(id, repo) {try {- await execa('yt-dlp', [url, '-o', path])+ await execa({stdio:'inherit'})`yt-dlp -f mp4 ${id} -o ./data/videos/${id}.mp4`+ repo.setVideoDownloaded(id)console.log('Download completed')} catch (error) {console.error('Error downloading video:', error)@@ -102,6 +122,7 @@ async function getVideosFor(channelName) {function toInternalVideo(v) {if (!v || !v.richItemRenderer) returnreturn {+ channelName,title: v.richItemRenderer.content.videoRenderer.title?.runs[0].text,url: `https://www.youtube.com/watch?v=${v.richItemRenderer.content.videoRenderer.videoId}`,thumbnail: v.richItemRenderer.content.videoRenderer.thumbnail?.thumbnails[0].url,@@ -144,18 +165,57 @@ function createServer ({repo, port = 3000}) {body += chunk.toString()})req.on('end', () => {- const { url, path } = JSON.parse(body)- downloadVideo(url, path).then(() => {- res.writeHead(200, { 'Content-Type': 'text/plain' })- res.end('Download started')- }).catch(error => {- console.error('Error starting download:', error)- res.writeHead(500, { 'Content-Type': 'text/plain' })- res.end('Internal Server Error')- })+ const { id } = JSON.parse(body)+ downloadVideo(id, repo)+ res.writeHead(200, { 'Content-Type': 'text/plain' })+ res.end('Download started')})return}+ if (parsedUrl.pathname.match('\/videos\/.*') && req.method === 'GET') {+ const videoPath = parsedUrl.pathname.replace('/videos/', './data/videos/') + '.mp4'+ console.log({videoPath})+ if (!fs.existsSync(videoPath)) {+ res.writeHead(404, { 'Content-Type': 'text/plain' })+ res.end('Video not found')+ return+ }+ try {+ const stats = fs.statSync(videoPath)+ //https://stackoverflow.com/questions/4360060/video-streaming-with-html-5-via-node-js/29126190#29126190+ var range = req.headers.range || "";+ var total = stats.size;+ console.log({stats, range, total})+ if (range) {+ var parts = range.replace(/bytes=/, "").split("-");+ var partialstart = parts[0];+ var partialend = parts[1];++ var start = parseInt(partialstart, 10);+ var end = partialend > 0 ? parseInt(partialend, 10) : total-1;++ var chunksize = (end-start)+1;+ res.writeHead(206, {+ "Content-Range": "bytes " + start + "-" + end + "/" + total,+ "Accept-Ranges": "bytes",+ "Content-Length": chunksize,+ "Content-Type": 'video/mp4'+ });+ } else {+ res.writeHead(200, {+ "Accept-Ranges": "bytes",+ "Content-Length": stats.size,+ "Content-Type": 'video/mp4'+ });+ }+ fs.createReadStream(videoPath).pipe(res)+ } catch (error) {+ console.error('error reading file ' + videoPath)+ res.writeHead(404, { 'Content-Type': 'text/plain' })+ res.end('Video not found')+ }+ return+ }const indexHtml = await fs.promises.readFile('index.html', 'utf8')res.writeHead(200, { 'Content-Type': 'text/html' })res.end(indexHtml)