Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

refactor: split api.js monolith into modular handlers

Christian Fei1cbd3159da30
diff --git a/server/http.js b/server/http.js
index cc0cc90..07e8837 100644
--- a/server/http.js
+++ b/server/http.js
@@ -1,7 +1,7 @@
import http from 'http'
import { URL } from 'url'
import { handleSSE, broadcastSSE } from './sse.js'
-import apiHandler from './router/api.js'
+import apiHandler from './router/index.js'
import appHandler from './router/app.js'
import Repository from '../lib/repository.js'
diff --git a/server/router/_helpers.js b/server/router/_helpers.js
new file mode 100644
index 0000000..08d29e9
--- /dev/null
+++ b/server/router/_helpers.js
@@ -0,0 +1,52 @@
+import { broadcastSSE } from '../sse.js'
+import querystring from 'querystring'
+
+const llmDefaults = {
+ model: 'meta-llama-3.1-8b-instruct',
+ host: 'http://127.0.0.1:1234',
+ endpoint: '/v1/chat/completions',
+ apiKey: '',
+ temperature: 0
+}
+
+const llmSettings = {
+ model: process.env.AI_MODEL ?? llmDefaults.model,
+ host: process.env.AI_HOST ?? llmDefaults.host,
+ endpoint: process.env.AI_ENDPOINT ?? llmDefaults.endpoint,
+ apiKey: process.env.AI_APIKEY ?? llmDefaults.apiKey,
+ temperature: process.env.AI_TEMPERATURE ?? llmDefaults.temperature
+}
+
+export function broadcastMessage (type, payload, connections) {
+ return broadcastSSE(JSON.stringify({ type, ...payload }), connections)
+}
+
+export async function parseBody (req, res) {
+ const body = await getBody(req)
+ try {
+ return JSON.parse(body)
+ } catch (err) {
+ res.writeHead(400, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify({ error: 'Invalid JSON body' }))
+ return null
+ }
+}
+
+function getBody (req) {
+ return new Promise((resolve, reject) => {
+ let body = ''
+ req.on('data', chunk => {
+ body += chunk.toString()
+ })
+ req.on('end', () => resolve(body))
+ req.on('error', reject)
+ })
+}
+
+export function getQuery (req) {
+ return (req.url && req.url.indexOf('?') >= 0)
+ ? querystring.parse(req.url.substring(req.url.indexOf('?') + 1))
+ : {}
+}
+
+export { llmSettings }
diff --git a/server/router/api.js b/server/router/api.js
deleted file mode 100644
index 094715b..0000000
--- a/server/router/api.js
+++ /dev/null
@@ -1,440 +0,0 @@
-import fs from 'fs'
-import { URL } from 'url'
-import querystring from 'querystring'
-import { summarizeVideo } from '../../lib/subtitles-summary.js'
-import { broadcastSSE } from '../sse.js'
-import { downloadVideo, extractIdFromUrl, isUnsupportedUrl, isYouTubeUrl } from '../../lib/youtube.js'
-import { updateAndPersistVideosForChannel } from '../../lib/update-videos.js'
-
-const llmDefaults = {
- model: 'meta-llama-3.1-8b-instruct',
- host: 'http://127.0.0.1:1234',
- endpoint: '/v1/chat/completions',
- apiKey: '',
- temperature: 0
-}
-
-const llmSettings = {
- model: process.env.AI_MODEL ?? llmDefaults.model,
- host: process.env.AI_HOST ?? llmDefaults.host,
- endpoint: process.env.AI_ENDPOINT ?? llmDefaults.endpoint,
- apiKey: process.env.AI_APIKEY ?? llmDefaults.apiKey,
- temperature: process.env.AI_TEMPERATURE ?? llmDefaults.temperature
-}
-
-function broadcastMessage (type, payload, connections) {
- return broadcastSSE(JSON.stringify({ type, ...payload }), connections)
-}
-
-const routeTable = [
- { path: '/api/channels', method: 'GET', handler: getChannelHandler },
- { path: '/api/channels', method: 'POST', handler: addChannelHandler },
- { path: '/api/channels', method: 'DELETE', handler: deleteChannelHandler },
- { path: '/api/download-video', method: 'POST', handler: downloadVideoHandler },
- { path: '/api/summarize-video', method: 'POST', handler: summarizeVideoHandler },
- { path: '/api/ignore-video', method: 'POST', handler: ignoreVideoHandler },
- { path: '/api/delete-video', method: 'POST', handler: deleteVideoHandler },
- { path: '/api/videos', method: 'GET', handler: searchVideosHandler },
- { path: '/api/video-quality', method: 'GET', handler: getVideoQualityHandler },
- { path: '/api/video-quality', method: 'POST', handler: setVideoQualityHandler },
- { path: '/api/disk-usage', method: 'GET', handler: diskUsageHandler },
- { path: '/api/reclaim-disk-space', method: 'POST', handler: reclaimDiskSpaceHandler },
- { path: '/api/transcode-videos', method: 'GET', handler: getTranscodeVideosHandler },
- { path: '/api/transcode-videos', method: 'POST', handler: setTranscodeVideosHandler },
- { path: '/api/excluded-terms', method: 'GET', handler: getExcludedTermsHandler },
- { path: '/api/excluded-terms', method: 'POST', handler: addExcludedTermHandler },
- { path: '/api/excluded-terms', method: 'DELETE', handler: removeExcludedTermHandler },
- { pathRegex: /\/api\/videos\//, method: 'GET', handler: watchVideoHandler },
- { pathRegex: /\/api\/captions\//, method: 'GET', handler: captionsHandler }
-]
-
-export default function apiHandler (req, res, repo, connections = [], state = {}) {
- const url = new URL(req.url, `http://${req.headers.host}`)
-
- const match = routeTable.find(r => {
- if (r.path !== undefined && url.pathname !== r.path) return false
- if (r.pathRegex && !r.pathRegex.test(url.pathname)) return false
- if (req.method !== r.method) return false
- return true
- })
-
- if (!match) { res.writeHead(404); return res.end() }
-
- return match.handler(req, res, repo, connections, state)
-}
-
-async function getChannelHandler (req, res, repo, connections = [], state = {}) {
- const channels = repo.getChannels()
- res.writeHead(200, { 'Content-Type': 'application/json' })
- return res.end(JSON.stringify(channels))
-}
-
-async function addChannelHandler (req, res, repo, connections = []) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- let { name } = parsed
- name = name.trim()
- name = name.startsWith('@') ? name.substring(1) : name
-
- if (repo.channelExists(name)) {
- res.writeHead(409)
- return res.end('Channel already added')
- }
-
- const videos = await updateAndPersistVideosForChannel(name, repo)
- if (Array.isArray(videos)) {
- repo.addChannel(name)
- broadcastMessage('new-videos', { name, videos }, connections)
- res.writeHead(201)
- return res.end('Channel added')
- }
-
- res.writeHead(404)
- return res.end('Channel not found')
-}
-
-async function deleteChannelHandler (req, res, repo, connections = [], state = {}) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- let { name } = parsed
- name = name.trim()
-
- if (!repo.channelExists(name)) {
- res.writeHead(409, { 'Content-Type': 'text/plain' })
- return res.end('Channel does not exist')
- }
-
- repo.deleteChannel(name)
- res.writeHead(200, { 'Content-Type': 'text/plain' })
- return res.end('Channel deleted')
-}
-
-async function downloadVideoHandler (req, res, repo, connections = [], state = {}) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- let { id, external } = parsed
-
- if (isUnsupportedUrl(id)) {
- console.log('unsupported url', id)
- res.writeHead(400)
- return res.end()
- }
- if (isYouTubeUrl(id)) { id = extractIdFromUrl(id) }
-
- state.downloading = state.downloading || {}
- state.downloading[id] = { lines: [] }
-
- let broadcastNewVideoOnce = false
- downloadVideo(id, repo, (message) => {
- if (external && !broadcastNewVideoOnce) {
- broadcastMessage('new-videos', { videos: [repo.getVideo(id)] }, connections)
- broadcastNewVideoOnce = true
- }
- if (message.status === 'info') {
- broadcastMessage('download-log-line', { line: message.line }, connections)
- }
- if (message.status === 'progress') {
- const progressPayload = Object.assign({}, message)
- if (!progressPayload.id) progressPayload.id = id
- if (!progressPayload.phase) progressPayload.phase = (progressPayload.frame || (progressPayload.time && progressPayload.time.indexOf(':') >= 0 && !progressPayload.total)) ? 'transcode' : 'download'
- if (progressPayload.phase === 'transcode') {
- broadcastMessage('transcode-progress', { progress: progressPayload }, connections)
- } else {
- broadcastMessage('download-progress', { progress: progressPayload }, connections)
- }
- }
- })
- .then(() => {
- const video = repo.getVideo(id)
- broadcastMessage('downloaded', { videoId: id, downloaded: true, video }, connections)
- })
- .catch((error) => {
- broadcastMessage('download-log-line', { line: error.stderr }, connections)
- })
- .finally(() => {
- delete state.downloading[id]
- })
-
- res.writeHead(200, { 'Content-Type': 'text/plain' })
- res.end('Download started')
-}
-
-async function summarizeVideoHandler (req, res, repo, connections = [], state = {}) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- const { id } = parsed
-
- state.summarizing = state.summarizing || {}
- state.summarizing[id] = { lines: [] }
-
- summarizeVideo(id, repo, llmSettings, (line) => {
- broadcastMessage('download-log-line', { line }, connections)
- })
- .then(({ summary, transcript }) =>
- broadcastMessage('summary', { summary, transcript, videoId: id }, connections))
- .catch((error) => {
- broadcastMessage('download-log-line', { line: error.message }, connections)
- broadcastMessage('summary-error', { videoId: id }, connections)
- })
- .finally(() => {
- delete state.summarizing[id]
- })
-
- res.writeHead(200, { 'Content-Type': 'text/plain' })
- res.end('Download started')
-}
-
-async function ignoreVideoHandler (req, res, repo, connections = [], state = {}) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- const { id, ignore } = parsed
- const ignored = ignore ? repo.ignoreVideo(id) : repo.unignoreVideo(id)
- broadcastMessage('ignored', { videoId: id, ignored }, connections)
- res.writeHead(200, { 'Content-Type': 'application/json' })
- return res.end(JSON.stringify(ignored))
-}
-
-async function deleteVideoHandler (req, res, repo, connections = [], state = {}) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- const { id } = parsed
- repo.deleteVideo(id)
- broadcastMessage('downloaded', { videoId: id, downloaded: false }, connections)
- res.writeHead(200)
- res.end()
-}
-
-export function searchVideosHandler (req, res, repo, connections = [], state = {}) {
- const query = getQuery(req)
- const videos = repo.getVideos(query)
- res.writeHead(200, { 'Content-Type': 'application/json' })
- res.end(JSON.stringify(videos))
-}
-
-function getVideoQualityHandler (req, res, repo, connections = [], state = {}) {
- const videoQuality = repo.getVideoQualitySetting()
- res.writeHead(200, { 'Content-Type': 'application/json' })
- res.end(JSON.stringify(videoQuality))
-}
-
-async function setVideoQualityHandler (req, res, repo, connections = [], state = {}) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- const videoQuality = parsed
-
- const newQuality = repo.setVideoQualitySetting(videoQuality)
- if (!newQuality) {
- res.writeHead(400)
- return res.end()
- }
- res.writeHead(200, { 'Content-Type': 'application/json' })
- res.end(JSON.stringify(newQuality))
-}
-
-function diskUsageHandler (req, res, repo, connections = [], state = {}) {
- const onlyIgnored = getQuery(req).onlyIgnored === 'true'
- const videos = repo.getAllVideos()
-
- const filterFn = onlyIgnored ? video => (video.downloaded || video.transcript) && video.ignored : video => (video.downloaded || video.transcript)
-
- const diskSpaceUsed = videos.filter(filterFn)
- .reduce((total, video) => {
- try {
- const filenames = fs.readdirSync('./data/videos').filter(f => f.startsWith(video.id))
- return total + filenames.reduce((acc, filename) => acc + fs.statSync(`./data/videos/${filename}`).size / Math.pow(10, 9), 0)
- } catch (err) {
- console.error(err)
- return total
- }
- }, 0)
- res.writeHead(200, { 'Content-Type': 'text/plain' })
- res.end(diskSpaceUsed.toFixed(3) + 'GB')
-}
-
-async function reclaimDiskSpaceHandler (req, res, repo, connections = [], state = {}) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- const { onlyIgnored } = parsed
-
- const filterFn = (video) => (video.downloaded || video.transcript)
- ? (onlyIgnored ? video.ignored : true)
- : false
-
- const videos = repo.getAllVideos().filter(filterFn)
- const filenames = await fs.promises.readdir('./data/videos')
- for (const video of videos) {
- try {
- for (const filename of filenames) {
- if (filename.startsWith(video.id)) {
- await fs.promises.unlink(`./data/videos/${filename}`)
- console.log('deleted', filename)
- repo.updateVideo(video.id, { downloaded: false })
- broadcastMessage('download-log-line', { line: `deleted ${filename}` }, connections)
- }
- }
- } catch (err) {
- console.error(`error processing video ${video.id}: ${err.message}`)
- broadcastMessage('download-log-line', { line: `error processing video ${video.id}: ${err.message}` }, connections)
- }
- }
-
- repo.saveVideos()
- res.writeHead(200)
- res.end()
-}
-
-async function getExcludedTermsHandler (req, res, repo, connections = [], state = {}) {
- const excludedTerms = repo.getExcludedTerms()
- res.writeHead(200, { 'Content-Type': 'application/json' })
- res.end(JSON.stringify(excludedTerms))
-}
-async function addExcludedTermHandler (req, res, repo) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- const term = parsed.term
- repo.addExcludedTerm(term)
- res.writeHead(200)
- res.end()
-}
-async function removeExcludedTermHandler (req, res, repo) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- const term = parsed.term
- repo.removeExcludedTerm(term)
- res.writeHead(200)
- res.end()
-}
-async function getTranscodeVideosHandler (req, res, repo) {
- const transcodeVideos = repo.getTranscodeVideosSetting()
- res.writeHead(200, { 'Content-Type': 'application/json' })
- res.end(JSON.stringify(transcodeVideos))
-}
-async function setTranscodeVideosHandler (req, res, repo) {
- const parsed = await parseBody(req, res)
- if (parsed === null) return
- const transcodeVideos = parsed
-
- repo.setTranscodeVideosSetting(transcodeVideos)
- res.writeHead(200)
- res.end()
-}
-
-function watchVideoHandler (req, res, repo, connections = []) {
- const id = req.url.replace('/api/videos/', '').replace(/\.(webm|mp4)$/, '')
- const video = repo.getVideo(id)
- const location = video.location || `./data/videos/${id}.mp4`
- const contentType = video.format ? `video/${video.format}` : 'video/mp4'
- if (!fs.existsSync(location)) {
- res.writeHead(404, { 'Content-Type': 'text/plain' })
- res.end('Video not found')
- broadcastMessage('download-log-line', { line: `video does not exist ${location}` }, connections)
- return
- }
-
- // https://github.com/bootstrapping-microservices/video-streaming-example/blob/master/index.js
- // https://blog.logrocket.com/streaming-video-in-safari/
-
- const options = {}
-
- let start
- let end
-
- const range = req.headers.range
- if (range) {
- const bytesPrefix = 'bytes='
- if (range.startsWith(bytesPrefix)) {
- const bytesRange = range.substring(bytesPrefix.length)
- const parts = bytesRange.split('-')
- if (parts.length === 2) {
- const rangeStart = parts[0] && parts[0].trim()
- if (rangeStart && rangeStart.length > 0) {
- options.start = start = parseInt(rangeStart)
- }
- const rangeEnd = parts[1] && parts[1].trim()
- if (rangeEnd && rangeEnd.length > 0) {
- options.end = end = parseInt(rangeEnd)
- }
- }
- }
- }
-
- res.setHeader('content-type', contentType)
-
- const stat = fs.statSync(location)
-
- const contentLength = stat.size
-
- if (req.method === 'HEAD') {
- res.statusCode = 200
- res.setHeader('accept-ranges', 'bytes')
- res.setHeader('content-length', contentLength)
- return res.end()
- }
- let retrievedLength = contentLength
- if (start !== undefined && end !== undefined) {
- retrievedLength = end - start + 1
- } else if (start !== undefined) {
- retrievedLength = contentLength - start
- }
-
- res.statusCode = (start !== undefined || end !== undefined) ? 206 : 200
-
- res.setHeader('content-length', retrievedLength)
-
- if (range !== undefined) {
- res.setHeader('accept-ranges', 'bytes')
- res.setHeader('content-range', `bytes ${start || 0}-${end || (contentLength - 1)}/${contentLength}`)
- }
-
- broadcastMessage('download-log-line', { line: `video range requested ${location} ${start || 0}-${end || (contentLength - 1)}/${contentLength}` }, connections)
-
- const fileStream = fs.createReadStream(location, options)
- fileStream.on('error', error => {
- console.error(`Error reading file ${location}.`, error)
- broadcastMessage('download-log-line', { line: `video stream error ${location}: ${error.message}` }, connections)
-
- res.writeHead(500)
- res.end()
- })
-
- fileStream.pipe(res)
-}
-
-function captionsHandler (req, res) {
- const captionsPath = './data' + req.url.replace('api/captions', 'videos') + '.en.vtt'
- if (!fs.existsSync(captionsPath)) {
- res.writeHead(404)
- res.end()
- } else {
- const fileStream = fs.createReadStream(captionsPath)
- fileStream.pipe(res)
- }
-}
-
-async function getBody (req) {
- return new Promise((resolve, reject) => {
- let body = ''
- req.on('data', chunk => {
- body += chunk.toString()
- })
- req.on('end', () => resolve(body))
- req.on('error', reject)
- })
-}
-
-async function parseBody (req, res) {
- const body = await getBody(req)
- try {
- return JSON.parse(body)
- } catch (err) {
- res.writeHead(400, { 'Content-Type': 'application/json' })
- res.end(JSON.stringify({ error: 'Invalid JSON body' }))
- return null
- }
-}
-
-function getQuery (req) {
- return (req.url && req.url.indexOf('?') >= 0)
- ? querystring.parse(req.url.substring(req.url.indexOf('?') + 1))
- : {}
-}
diff --git a/server/router/handlers/channels.js b/server/router/handlers/channels.js
new file mode 100644
index 0000000..2bff172
--- /dev/null
+++ b/server/router/handlers/channels.js
@@ -0,0 +1,70 @@
+import { updateAndPersistVideosForChannel } from '../../../lib/update-videos.js'
+import { broadcastMessage, parseBody } from '../_helpers.js'
+
+export function getChannelHandler (req, res, repo, connections = [], state = {}) {
+ const channels = repo.getChannels()
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ return res.end(JSON.stringify(channels))
+}
+
+export async function addChannelHandler (req, res, repo, connections = []) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ let { name } = parsed
+ name = name.trim()
+ name = name.startsWith('@') ? name.substring(1) : name
+
+ if (repo.channelExists(name)) {
+ res.writeHead(409)
+ return res.end('Channel already added')
+ }
+
+ const videos = await updateAndPersistVideosForChannel(name, repo)
+ if (Array.isArray(videos)) {
+ repo.addChannel(name)
+ broadcastMessage('new-videos', { name, videos }, connections)
+ res.writeHead(201)
+ return res.end('Channel added')
+ }
+
+ res.writeHead(404)
+ return res.end('Channel not found')
+}
+
+export async function deleteChannelHandler (req, res, repo, connections = [], state = {}) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ let { name } = parsed
+ name = name.trim()
+
+ if (!repo.channelExists(name)) {
+ res.writeHead(409, { 'Content-Type': 'text/plain' })
+ return res.end('Channel does not exist')
+ }
+
+ repo.deleteChannel(name)
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
+ return res.end('Channel deleted')
+}
+
+export async function ignoreVideoHandler (req, res, repo, connections = [], state = {}) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ const { id, ignore } = parsed
+ const ignored = ignore ? repo.ignoreVideo(id) : repo.unignoreVideo(id)
+ broadcastMessage('ignored', { videoId: id, ignored }, connections)
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ return res.end(JSON.stringify(ignored))
+}
+
+export async function deleteVideoHandler (req, res, repo, connections = [], state = {}) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ const { id } = parsed
+ repo.deleteVideo(id)
+ broadcastMessage('downloaded', { videoId: id, downloaded: false }, connections)
+ res.writeHead(200)
+ res.end()
+}
+
+export default { getChannelHandler, addChannelHandler, deleteChannelHandler, ignoreVideoHandler, deleteVideoHandler }
diff --git a/server/router/handlers/disk-space.js b/server/router/handlers/disk-space.js
new file mode 100644
index 0000000..20ef552
--- /dev/null
+++ b/server/router/handlers/disk-space.js
@@ -0,0 +1,56 @@
+import fs from 'fs'
+import { broadcastMessage, parseBody, getQuery } from '../_helpers.js'
+
+export function diskUsageHandler (req, res, repo, connections = [], state = {}) {
+ const onlyIgnored = getQuery(req).onlyIgnored === 'true'
+ const videos = repo.getAllVideos()
+
+ const filterFn = onlyIgnored ? video => (video.downloaded || video.transcript) && video.ignored : video => (video.downloaded || video.transcript)
+
+ const diskSpaceUsed = videos.filter(filterFn)
+ .reduce((total, video) => {
+ try {
+ const filenames = fs.readdirSync('./data/videos').filter(f => f.startsWith(video.id))
+ return total + filenames.reduce((acc, filename) => acc + fs.statSync(`./data/videos/${filename}`).size / Math.pow(10, 9), 0)
+ } catch (err) {
+ console.error(err)
+ return total
+ }
+ }, 0)
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
+ res.end(diskSpaceUsed.toFixed(3) + 'GB')
+}
+
+export async function reclaimDiskSpaceHandler (req, res, repo, connections = [], state = {}) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ const { onlyIgnored } = parsed
+
+ const filterFn = (video) => (video.downloaded || video.transcript)
+ ? (onlyIgnored ? video.ignored : true)
+ : false
+
+ const videos = repo.getAllVideos().filter(filterFn)
+ const filenames = await fs.promises.readdir('./data/videos')
+ for (const video of videos) {
+ try {
+ for (const filename of filenames) {
+ if (filename.startsWith(video.id)) {
+ await fs.promises.unlink(`./data/videos/${filename}`)
+ console.log('deleted', filename)
+ repo.updateVideo(video.id, { downloaded: false })
+ broadcastMessage('download-log-line', { line: `deleted ${filename}` }, connections)
+ }
+ }
+ } catch (err) {
+ console.error(`error processing video ${video.id}: ${err.message}`)
+ broadcastMessage('download-log-line', { line: `error processing video ${video.id}: ${err.message}` }, connections)
+ }
+ }
+
+ repo.saveVideos()
+ res.writeHead(200)
+ res.end()
+}
+
+export default { diskUsageHandler, reclaimDiskSpaceHandler }
diff --git a/server/router/handlers/downloads.js b/server/router/handlers/downloads.js
new file mode 100644
index 0000000..b3a4164
--- /dev/null
+++ b/server/router/handlers/downloads.js
@@ -0,0 +1,54 @@
+import { downloadVideo, isUnsupportedUrl, extractIdFromUrl, isYouTubeUrl } from '../../../lib/youtube.js'
+import { broadcastMessage, parseBody } from '../_helpers.js'
+
+export async function downloadVideoHandler (req, res, repo, connections = [], state = {}) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ let { id, external } = parsed
+
+ if (isUnsupportedUrl(id)) {
+ console.log('unsupported url', id)
+ res.writeHead(400)
+ return res.end()
+ }
+ if (isYouTubeUrl(id)) { id = extractIdFromUrl(id) }
+
+ state.downloading = state.downloading || {}
+ state.downloading[id] = { lines: [] }
+
+ let broadcastNewVideoOnce = false
+ downloadVideo(id, repo, (message) => {
+ if (external && !broadcastNewVideoOnce) {
+ broadcastMessage('new-videos', { videos: [repo.getVideo(id)] }, connections)
+ broadcastNewVideoOnce = true
+ }
+ if (message.status === 'info') {
+ broadcastMessage('download-log-line', { line: message.line }, connections)
+ }
+ if (message.status === 'progress') {
+ const progressPayload = Object.assign({}, message)
+ if (!progressPayload.id) progressPayload.id = id
+ if (!progressPayload.phase) progressPayload.phase = (progressPayload.frame || (progressPayload.time && progressPayload.time.indexOf(':') >= 0 && !progressPayload.total)) ? 'transcode' : 'download'
+ if (progressPayload.phase === 'transcode') {
+ broadcastMessage('transcode-progress', { progress: progressPayload }, connections)
+ } else {
+ broadcastMessage('download-progress', { progress: progressPayload }, connections)
+ }
+ }
+ })
+ .then(() => {
+ const video = repo.getVideo(id)
+ broadcastMessage('downloaded', { videoId: id, downloaded: true, video }, connections)
+ })
+ .catch((error) => {
+ broadcastMessage('download-log-line', { line: error.stderr }, connections)
+ })
+ .finally(() => {
+ delete state.downloading[id]
+ })
+
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
+ res.end('Download started')
+}
+
+export default { downloadVideoHandler }
diff --git a/server/router/handlers/settings.js b/server/router/handlers/settings.js
new file mode 100644
index 0000000..0e33a0b
--- /dev/null
+++ b/server/router/handlers/settings.js
@@ -0,0 +1,63 @@
+import { parseBody } from '../_helpers.js'
+
+export function getVideoQualityHandler (req, res, repo, connections = [], state = {}) {
+ const videoQuality = repo.getVideoQualitySetting()
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify(videoQuality))
+}
+
+export async function setVideoQualityHandler (req, res, repo, connections = [], state = {}) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ const videoQuality = parsed
+
+ const newQuality = repo.setVideoQualitySetting(videoQuality)
+ if (!newQuality) {
+ res.writeHead(400)
+ return res.end()
+ }
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify(newQuality))
+}
+
+export function getTranscodeVideosHandler (req, res, repo) {
+ const transcodeVideos = repo.getTranscodeVideosSetting()
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify(transcodeVideos))
+}
+
+export async function setTranscodeVideosHandler (req, res, repo) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ const transcodeVideos = parsed
+
+ repo.setTranscodeVideosSetting(transcodeVideos)
+ res.writeHead(200)
+ res.end()
+}
+
+export async function getExcludedTermsHandler (req, res, repo, connections = [], state = {}) {
+ const excludedTerms = repo.getExcludedTerms()
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify(excludedTerms))
+}
+
+export async function addExcludedTermHandler (req, res, repo) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ const term = parsed.term
+ repo.addExcludedTerm(term)
+ res.writeHead(200)
+ res.end()
+}
+
+export async function removeExcludedTermHandler (req, res, repo) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ const term = parsed.term
+ repo.removeExcludedTerm(term)
+ res.writeHead(200)
+ res.end()
+}
+
+export default { getVideoQualityHandler, setVideoQualityHandler, getTranscodeVideosHandler, setTranscodeVideosHandler, getExcludedTermsHandler, addExcludedTermHandler, removeExcludedTermHandler }
diff --git a/server/router/handlers/summaries.js b/server/router/handlers/summaries.js
new file mode 100644
index 0000000..bd6e113
--- /dev/null
+++ b/server/router/handlers/summaries.js
@@ -0,0 +1,29 @@
+import { summarizeVideo } from '../../../lib/subtitles-summary.js'
+import { broadcastMessage, parseBody, llmSettings } from '../_helpers.js'
+
+export async function summarizeVideoHandler (req, res, repo, connections = [], state = {}) {
+ const parsed = await parseBody(req, res)
+ if (parsed === null) return
+ const { id } = parsed
+
+ state.summarizing = state.summarizing || {}
+ state.summarizing[id] = { lines: [] }
+
+ summarizeVideo(id, repo, llmSettings, (line) => {
+ broadcastMessage('download-log-line', { line }, connections)
+ })
+ .then(({ summary, transcript }) =>
+ broadcastMessage('summary', { summary, transcript, videoId: id }, connections))
+ .catch((error) => {
+ broadcastMessage('download-log-line', { line: error.message }, connections)
+ broadcastMessage('summary-error', { videoId: id }, connections)
+ })
+ .finally(() => {
+ delete state.summarizing[id]
+ })
+
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
+ res.end('Download started')
+}
+
+export default { summarizeVideoHandler }
diff --git a/server/router/handlers/videos.js b/server/router/handlers/videos.js
new file mode 100644
index 0000000..e792973
--- /dev/null
+++ b/server/router/handlers/videos.js
@@ -0,0 +1,103 @@
+import fs from 'fs'
+import { broadcastMessage, getQuery } from '../_helpers.js'
+
+export function searchVideosHandler (req, res, repo, connections = [], state = {}) {
+ const query = getQuery(req)
+ const videos = repo.getVideos(query)
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify(videos))
+}
+
+export function watchVideoHandler (req, res, repo, connections = []) {
+ const id = req.url.replace('/api/videos/', '').replace(/\.(webm|mp4)$/, '')
+ const video = repo.getVideo(id)
+ const location = video.location || `./data/videos/${id}.mp4`
+ const contentType = video.format ? `video/${video.format}` : 'video/mp4'
+ if (!fs.existsSync(location)) {
+ res.writeHead(404, { 'Content-Type': 'text/plain' })
+ res.end('Video not found')
+ broadcastMessage('download-log-line', { line: `video does not exist ${location}` }, connections)
+ return
+ }
+
+ // https://github.com/bootstrapping-microservices/video-streaming-example/blob/master/index.js
+ // https://blog.logrocket.com/streaming-video-in-safari/
+
+ const options = {}
+
+ let start
+ let end
+
+ const range = req.headers.range
+ if (range) {
+ const bytesPrefix = 'bytes='
+ if (range.startsWith(bytesPrefix)) {
+ const bytesRange = range.substring(bytesPrefix.length)
+ const parts = bytesRange.split('-')
+ if (parts.length === 2) {
+ const rangeStart = parts[0] && parts[0].trim()
+ if (rangeStart && rangeStart.length > 0) {
+ options.start = start = parseInt(rangeStart)
+ }
+ const rangeEnd = parts[1] && parts[1].trim()
+ if (rangeEnd && rangeEnd.length > 0) {
+ options.end = end = parseInt(rangeEnd)
+ }
+ }
+ }
+ }
+
+ res.setHeader('content-type', contentType)
+
+ const stat = fs.statSync(location)
+
+ const contentLength = stat.size
+
+ if (req.method === 'HEAD') {
+ res.statusCode = 200
+ res.setHeader('accept-ranges', 'bytes')
+ res.setHeader('content-length', contentLength)
+ return res.end()
+ }
+ let retrievedLength = contentLength
+ if (start !== undefined && end !== undefined) {
+ retrievedLength = end - start + 1
+ } else if (start !== undefined) {
+ retrievedLength = contentLength - start
+ }
+
+ res.statusCode = (start !== undefined || end !== undefined) ? 206 : 200
+
+ res.setHeader('content-length', retrievedLength)
+
+ if (range !== undefined) {
+ res.setHeader('accept-ranges', 'bytes')
+ res.setHeader('content-range', `bytes ${start || 0}-${end || (contentLength - 1)}/${contentLength}`)
+ }
+
+ broadcastMessage('download-log-line', { line: `video range requested ${location} ${start || 0}-${end || (contentLength - 1)}/${contentLength}` }, connections)
+
+ const fileStream = fs.createReadStream(location, options)
+ fileStream.on('error', error => {
+ console.error(`Error reading file ${location}.`, error)
+ broadcastMessage('download-log-line', { line: `video stream error ${location}: ${error.message}` }, connections)
+
+ res.writeHead(500)
+ res.end()
+ })
+
+ fileStream.pipe(res)
+}
+
+export function captionsHandler (req, res) {
+ const captionsPath = './data' + req.url.replace('api/captions', 'videos') + '.en.vtt'
+ if (!fs.existsSync(captionsPath)) {
+ res.writeHead(404)
+ res.end()
+ } else {
+ const fileStream = fs.createReadStream(captionsPath)
+ fileStream.pipe(res)
+ }
+}
+
+export default { searchVideosHandler, watchVideoHandler, captionsHandler }
diff --git a/server/router/index.js b/server/router/index.js
new file mode 100644
index 0000000..90be583
--- /dev/null
+++ b/server/router/index.js
@@ -0,0 +1,72 @@
+import channels from './handlers/channels.js'
+import videos from './handlers/videos.js'
+import downloads from './handlers/downloads.js'
+import summaries from './handlers/summaries.js'
+import settings from './handlers/settings.js'
+import diskSpace from './handlers/disk-space.js'
+
+const routeMap = {
+ // Channels: GET, POST, DELETE
+ 'GET /api/channels': channels.getChannelHandler,
+ 'POST /api/channels': channels.addChannelHandler,
+ 'DELETE /api/channels': channels.deleteChannelHandler,
+
+ // Download: POST
+ 'POST /api/download-video': downloads.downloadVideoHandler,
+
+ // Summarize: POST
+ 'POST /api/summarize-video': summaries.summarizeVideoHandler,
+
+ // Ignore video: POST
+ 'POST /api/ignore-video': channels.ignoreVideoHandler,
+
+ // Delete video: POST
+ 'POST /api/delete-video': channels.deleteVideoHandler,
+
+ // Videos: GET (search)
+ 'GET /api/videos': videos.searchVideosHandler,
+
+ // Video quality: GET, POST
+ 'GET /api/video-quality': settings.getVideoQualityHandler,
+ 'POST /api/video-quality': settings.setVideoQualityHandler,
+
+ // Disk usage: GET
+ 'GET /api/disk-usage': diskSpace.diskUsageHandler,
+
+ // Reclaim: POST
+ 'POST /api/reclaim-disk-space': diskSpace.reclaimDiskSpaceHandler,
+
+ // Transcode: GET, POST
+ 'GET /api/transcode-videos': settings.getTranscodeVideosHandler,
+ 'POST /api/transcode-videos': settings.setTranscodeVideosHandler,
+
+ // Excluded terms: GET, POST, DELETE
+ 'GET /api/excluded-terms': settings.getExcludedTermsHandler,
+ 'POST /api/excluded-terms': settings.addExcludedTermHandler,
+ 'DELETE /api/excluded-terms': settings.removeExcludedTermHandler
+}
+
+export default function apiHandler (req, res, repo, connections = [], state = {}) {
+ const path = req.url.split('?')[0]
+
+ // Exact match with method
+ const key = `${req.method} ${path}`
+ if (routeMap[key]) {
+ return routeMap[key](req, res, repo, connections, state)
+ }
+
+ // Regex-style matches (path-based patterns with method check)
+ if (req.method === 'GET' && path.startsWith('/api/videos/')) {
+ return videos.watchVideoHandler(req, res, repo, connections)
+ }
+ if (req.method === 'GET' && path.startsWith('/api/captions/')) {
+ return videos.captionsHandler(req, res)
+ }
+
+ // 404 fallback
+ res.writeHead(404)
+ return res.end()
+}
+
+// Re-export individual handlers for tests that import them directly
+export { searchVideosHandler } from './handlers/videos.js'
diff --git a/test/search.test.js b/test/search.test.js
index d05346f..1d0a162 100644
--- a/test/search.test.js
+++ b/test/search.test.js
@@ -1,7 +1,7 @@
import { test } from 'node:test'
import fs from 'fs'
import assert from 'assert'
-import { searchVideosHandler } from '../server/router/api.js'
+import { searchVideosHandler } from '../server/router/index.js'
import Repository from '../lib/repository.js'
const req = {}