Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

helper getBody

Christian Fei375e42b3de0b
diff --git a/lib/server.js b/lib/server.js
index 183718c..967ace0 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -49,66 +49,59 @@ export function createServer ({repo, port = 3000, connections = []}) {
}
}
- function addChannelHandler(req, res) {
- let body = ''
- req.on('data', chunk => {
- body += chunk.toString()
- })
- req.on('end', async () => {
- let { name } = JSON.parse(body)
- name = name.trim()
- const videos = await updateAndPersistVideosForChannel(name, repo)
- if (Array.isArray(videos)) {
- repo.addChannel(name)
- broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
- res.writeHead(201, { 'Content-Type': 'text/plain' })
- res.end('Channel added')
- } else {
- res.writeHead(404, { 'Content-Type': 'text/plain' })
- res.end('Channel not found')
- }
+ 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)
})
}
- function downloadVideoHandler(req, res, connections = []) {
- let body = ''
- req.on('data', chunk => {
- body += chunk.toString()
- })
- req.on('end', () => {
- const { id } = JSON.parse(body)
- const {channelName} = repo.getVideo(id)
- const videos = repo.getChannelVideos(channelName)
- downloadVideo(id, repo, (line) =>
- broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
- .then(() =>
- broadcastSSE(JSON.stringify({type: 'downloaded', videoId: id}), connections))
- .catch((error) => console.error(error))
-
- res.writeHead(200, { 'Content-Type': 'text/plain' })
- res.end('Download started')
- })
+ async function addChannelHandler(req, res) {
+ const body = await getBody(req)
+ let { name } = JSON.parse(body)
+ name = name.trim()
+ const videos = await updateAndPersistVideosForChannel(name, repo)
+ if (Array.isArray(videos)) {
+ repo.addChannel(name)
+ broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
+ res.writeHead(201, { 'Content-Type': 'text/plain' })
+ res.end('Channel added')
+ } else {
+ res.writeHead(404, { 'Content-Type': 'text/plain' })
+ res.end('Channel not found')
+ }
}
- function summarizeVideoHandler(req, res, connections = []) {
- let body = ''
- req.on('data', chunk => {
- body += chunk.toString()
- })
- req.on('end', () => {
- const { id } = JSON.parse(body)
- const {channelName} = repo.getVideo(id)
- const videos = repo.getChannelVideos(channelName)
-
- summarizeVideo(id, repo, (line) =>
- broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
- .then((summary) =>
- broadcastSSE(JSON.stringify({type: 'summary', summary, videoId: id}), connections))
- .catch((error) => console.error(error))
-
- res.writeHead(200, { 'Content-Type': 'text/plain' })
- res.end('Download started')
- })
+ async function downloadVideoHandler(req, res, connections = []) {
+ const body = await getBody(req)
+ const { id } = JSON.parse(body)
+
+ downloadVideo(id, repo, (line) =>
+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
+ .then(() =>
+ broadcastSSE(JSON.stringify({type: 'downloaded', videoId: id}), connections))
+ .catch((error) => console.error(error))
+
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
+ res.end('Download started')
+ }
+
+ async function summarizeVideoHandler(req, res, connections = []) {
+ const body = await getBody(req)
+ const { id } = JSON.parse(body)
+
+ summarizeVideo(id, repo, (line) =>
+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
+ .then(({summary, transcript}) =>
+ broadcastSSE(JSON.stringify({type: 'summary', summary, transcript, videoId: id}), connections))
+ .catch((error) => console.error(error))
+
+ res.writeHead(200, { 'Content-Type': 'text/plain' })
+ res.end('Download started')
}
function watchVideoHandler(req, res) {