Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

feat: state and logs revisit

Christian Fei542ffb3fcc8e
diff --git a/client/index.html b/client/index.html
index 41f5fd2..a0602bc 100644
--- a/client/index.html
+++ b/client/index.html
@@ -22,9 +22,13 @@
<main></main>
- <details class="download-log">
- <summary>Download Log</summary>
- <div class="lines"></div>
+ <details class="state">
+ <summary>state & logs</summary>
+ <div class="flex mt-1">
+ <div class="flex-1 gap downloading">Downloading</div>
+ <div class="flex-1 gap summarizing">Summarizing</div>
+ </div>
+ <pre class="lines"></pre>
</details>
<dialog id="summary">
diff --git a/client/main.css b/client/main.css
index 521e3b0..c68cd16 100644
--- a/client/main.css
+++ b/client/main.css
@@ -62,6 +62,10 @@ a {
.tar {
text-align: right;
}
+.gap {
+ margin-left: .5rem;
+ margin-right: .5rem;
+}
.hide {
@@ -265,34 +269,33 @@ video-element, video-element video {
-.download-log {
+.state {
position: fixed;
bottom: 10px;
right: 10px;
- width: fit-content;
- max-width: 90%;
+ left: 10px;
+ /* width: fit-content;
+ max-width: 90%; */
overflow: hidden;
+ overflow-y: auto;
z-index: 9000;
- /* height: 200px; */
- font-size: 10px;
+ max-height: 150px;
+ font-size: 1rem;
+ padding: 0.5rem;
background-color: white;
- border: 1px solid lightgrey;
- box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
- overflow-y: auto;
- padding: 10px;
- background-color: #f0f0f0;
color: #000;
border-radius: 8px;
}
-.download-log .lines {
- max-height: 100px;
- overflow-y: scroll;
+.state > div {
+ margin-top: 0.5rem;
}
-
-
-
+.state .lines {
+ font-size: 1rem;
+ max-height: 5rem;
+ overflow-y: scroll;
+}
diff --git a/client/main.js b/client/main.js
index e804eea..17de09f 100644
--- a/client/main.js
+++ b/client/main.js
@@ -9,10 +9,22 @@ eventSource.onmessage = (message) => {
const data = JSON.parse(message.data, {})
console.log('[sse] message', data)
+ if (data.type === 'state' && data.state) {
+ const $state = document.querySelector('.state')
+ if (!$state) { return console.warn('missing $state') }
+ const $downloading = $state.querySelector('.downloading')
+ const $summarizing = $state.querySelector('.summarizing')
+ const downloadingCount = Object.keys(data.state.downloading || {}).length
+ $downloading.innerText = `Downloading: ${downloadingCount}`
+ const summarizingCount = Object.keys(data.state.summarizing || {}).length
+ $summarizing.innerText = `Summarizing: ${summarizingCount}`
+ return
+ }
if (data.type === 'download-log-line' && data.line) {
- const $downloadLog = document.querySelector('.download-log')
- // $downloadLog.open = true
- const $downloadLogLines = document.querySelector('.download-log .lines')
+ const $state = document.querySelector('.state')
+ if (!$state) { return console.warn('missing $state') }
+
+ const $downloadLogLines = $state.querySelector(' .lines')
const text = $downloadLogLines.innerText
let lines = text.split('\n')
lines = lines.join('\n') + '\n' + data.line
diff --git a/lib/router/api.js b/lib/router/api.js
index ad9a297..d216889 100644
--- a/lib/router/api.js
+++ b/lib/router/api.js
@@ -13,7 +13,7 @@ const llmSettings = {
apiKey: process.env.AI_APIKEY || ''
}
-export default function apiHandler(req, res, repo, connections = []) {
+export default function apiHandler(req, res, repo, connections = [], state = {}) {
const url = new URL(req.url, `http://${req.headers.host}`)
if (url.pathname === '/api/channels' && req.method === 'GET')
@@ -23,9 +23,9 @@ export default function apiHandler(req, res, repo, connections = []) {
if (url.pathname === '/api/channels' && req.method === 'DELETE')
return deleteChannelHandler(req, res, repo, connections)
if (url.pathname === '/api/download-video' && req.method === 'POST')
- return downloadVideoHandler(req, res, repo, connections)
+ return downloadVideoHandler(req, res, repo, connections, state)
if (url.pathname === '/api/summarize-video' && req.method === 'POST')
- return summarizeVideoHandler(req, res, repo, connections, llmSettings)
+ return summarizeVideoHandler(req, res, repo, connections, state, llmSettings)
if (url.pathname === '/api/ignore-video' && req.method === 'POST')
return ignoreVideoHandler(req, res, repo, connections)
if (url.pathname === '/api/delete-video' && req.method === 'POST')
@@ -93,7 +93,7 @@ async function deleteChannelHandler(req, res, repo, connections = []) {
return res.end('Channel deleted')
}
-async function downloadVideoHandler(req, res, repo, connections = []) {
+async function downloadVideoHandler(req, res, repo, connections = [], state = {}) {
const body = await getBody(req)
let { id, external } = JSON.parse(body)
@@ -102,34 +102,53 @@ async function downloadVideoHandler(req, res, repo, connections = []) {
res.writeHead(400)
return res.end()
}
- if (isYouTubeUrl(id)) id = extractIdFromUrl(id)
+ if (isYouTubeUrl(id)) { id = extractIdFromUrl(id) }
+
+ state.downloading = state.downloading || {}
+ state.downloading[id] = {lines: []}
- downloadVideo(id, repo, (line) =>
- broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
+ downloadVideo(id, repo, (line) => {
+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections)
+ state.changed = true
+ })
.then(() => {
const video = repo.getVideo(id)
broadcastSSE(JSON.stringify({type: 'downloaded', videoId: id, downloaded: true, video}), connections)
if (external) broadcastSSE(JSON.stringify({type: 'new-videos', videos: [repo.getVideo(id)]}), connections)
})
- .catch((error) =>
- broadcastSSE(JSON.stringify({type: 'download-log-line', line: error.stderr}), connections))
+ .catch((error) => {
+ broadcastSSE(JSON.stringify({type: 'download-log-line', line: error.stderr}), connections)
+ })
+ .finally(() => {
+ delete state.downloading[id]
+ state.changed = true
+ })
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Download started')
}
-async function summarizeVideoHandler(req, res, repo, connections = [], llmSettings = {}) {
+async function summarizeVideoHandler(req, res, repo, connections = [], state = {}, llmSettings = {}) {
const body = await getBody(req)
const { id } = JSON.parse(body)
- summarizeVideo(id, repo, llmSettings, (line) =>
- broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
+ state.summarizing = state.summarizing || {}
+ state.summarizing[id] = {lines: []}
+
+ summarizeVideo(id, repo, llmSettings, (line) => {
+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections)
+ state.changed = true
+ })
.then(({summary, transcript}) =>
broadcastSSE(JSON.stringify({type: 'summary', summary, transcript, videoId: id}), connections))
.catch((error) => {
broadcastSSE(JSON.stringify({type: 'download-log-line', line: error.message}), connections)
broadcastSSE(JSON.stringify({type: 'summary-error', videoId: id}), connections)
})
+ .finally(() => {
+ delete state.summarizing[id]
+ state.changed = true
+ })
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Download started')
diff --git a/lib/server.js b/lib/server.js
index 6df8a3d..533fbe9 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -23,6 +23,11 @@ export function createServer ({repo, state = {}, connections = [], updateVideos
}
}
+ setInterval(() => {
+ if (state.changed) broadcastSSE(JSON.stringify({type: 'state', state}), connections)
+ state.changed = false
+ }, 2000)
+
return http.createServer(async (req, res) => {
try {
const url = new URL(req.url, `http://${req.headers.host}`)
diff --git a/lib/subtitles-summary.js b/lib/subtitles-summary.js
index 8366e48..2372359 100644
--- a/lib/subtitles-summary.js
+++ b/lib/subtitles-summary.js
@@ -10,6 +10,7 @@ export async function summarizeVideo(id, repo, llmSettings = {}, callback = () =
repo.setVideoTranscript(id, cleanedTranscript)
cleanedTranscript = cleanedTranscript.replace('\n', ' ')
fs.writeFileSync(`./data/videos/${id}.en.txt`, cleanedTranscript)
+ callback('prompting llm')
const userPrompt = `
Extract the key points from the following video transcript:
@@ -18,6 +19,7 @@ ${cleanedTranscript}`.trim()
const systemPrompt = "You're a helpful assistant that sumarizes a video transcript in max 10 sentences, extracting the key points"
const summary = await fetchCompletionLMSTUDIO(systemPrompt, userPrompt, llmSettings)
+ callback('received summary')
repo.setVideoSummary(id, summary)
resolve({summary, transcript: cleanedTranscript})
} catch (error) {