Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

split into files

Christian Fei77421d3afae7
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..126af91
--- /dev/null
+++ b/index.js
@@ -0,0 +1,26 @@
+import fs from 'fs'
+import Repository from './lib/repository.js'
+import { createServer } from './lib/server.js'
+
+fs.mkdirSync('./data', { recursive: true })
+fs.mkdirSync('./data/videos', { recursive: true })
+
+async function main ({port = 3000} = {}) {
+ const repo = new Repository()
+ const connections = []
+
+ fs.readdirSync('./data/videos').forEach(file => {
+ if (!file.endsWith('.mp4')) return
+ const videoId = file.replace('.mp4', '')
+ repo.setVideoDownloaded(videoId)
+ })
+
+ createServer({repo, port, connections})
+ .listen(port, () => {
+ console.log(`Server running at http://localhost:${port}`)
+ })
+}
+
+if (import.meta.url.endsWith('index.js')) {
+ main()
+}
\ No newline at end of file
diff --git a/server.js b/lib/server.js
similarity index 71%
rename from server.js
rename to lib/server.js
index 130854d..22ad552 100644
--- a/server.js
+++ b/lib/server.js
@@ -1,47 +1,22 @@
import http from 'http'
import { URL } from 'url'
-import { execa } from 'execa'
import fs from 'fs'
-import { summarizeVideo } from './lib/subtitles-summary.js'
-import Repository from './lib/repository.js'
-import { handleSSE, broadcastSSE } from './lib/sse.js'
-import { downloadVideo, getVideosFor } from './lib/youtube.js'
-
-fs.mkdirSync('./data', { recursive: true })
-fs.mkdirSync('./data/videos', { recursive: true })
-
-const connections = []
-
-async function main ({port = 3000} = {}) {
- const repo = new Repository()
-
- fs.readdirSync('./data/videos').forEach(file => {
- if (!file.endsWith('.mp4')) return
- const videoId = file.replace('.mp4', '')
- repo.setVideoDownloaded(videoId)
- })
- updateAndPersistVideos(repo)
-
- setInterval(() => updateAndPersistVideos(repo), 1000 * 60 * 5)
-
- createServer({repo, port})
- .listen(port, () => {
- console.log(`Server running at http://localhost:${port}`)
+import { summarizeVideo } from './subtitles-summary.js'
+import { handleSSE, broadcastSSE } from './sse.js'
+import { downloadVideo, getVideosFor } from './youtube.js'
+export function createServer ({repo, port = 3000, connections = []}) {
+ updateAndPersistVideos(repo, ({name, videos}) => {
+ broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
})
-}
-if (import.meta.url.endsWith('server.js')) {
- main()
-}
+ setInterval(() => {
+ updateAndPersistVideos(repo, ({name, videos}) => {
+ broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
+ })
+ }, 1000 * 60 * 5)
-function createServer ({repo, port = 3000}) {
return http.createServer(async (req, res) => {
const parsedUrl = new URL(req.url, `http://${req.headers.host}`)
- if (parsedUrl.pathname === '/videos') {
- res.writeHead(200, { 'Content-Type': 'application/json' })
- res.end(JSON.stringify(repo.getVideos()))
- return
- }
if (parsedUrl.pathname === '/channels' && req.method === 'POST') {
let body = ''
req.on('data', chunk => {
@@ -72,16 +47,11 @@ function createServer ({repo, port = 3000}) {
const { id } = JSON.parse(body)
const {channelName} = repo.getVideo(id)
const videos = repo.getVideos()[channelName]
- downloadVideo(id, repo, (line) => {
- broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections)
- })
- .then(() => {
- broadcastSSE(JSON.stringify({type: 'channel', name: channelName, videos}), connections)
- })
- .catch((error) => {
- console.error(error)
- broadcastSSE(JSON.stringify({type: 'channel', name: channelName, videos}), connections)
- })
+ downloadVideo(id, repo, (line) =>
+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
+ .catch((error) => console.error(error))
+ .finally(() =>
+ broadcastSSE(JSON.stringify({type: 'channel', name: channelName, videos}), connections))
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Download started')
})
@@ -97,16 +67,11 @@ function createServer ({repo, port = 3000}) {
const {channelName} = repo.getVideo(id)
const videos = repo.getVideos()[channelName]
- summarizeVideo(id, repo, (line) => {
- broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections)
- })
- .then(() => {
- broadcastSSE(JSON.stringify({type: 'channel', name: channelName, videos}), connections)
- })
- .catch((error) => {
- console.error(error)
- broadcastSSE(JSON.stringify({type: 'channel', name: channelName, videos}), connections)
- })
+ summarizeVideo(id, repo, (line) =>
+ broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
+ .catch((error) => console.error(error))
+ .finally(() =>
+ broadcastSSE(JSON.stringify({type: 'channel', name: channelName, videos}), connections))
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Download started')
})
@@ -114,7 +79,6 @@ function createServer ({repo, port = 3000}) {
}
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')
@@ -168,21 +132,21 @@ function createServer ({repo, port = 3000}) {
}
-async function updateAndPersistVideos (repo) {
+async function updateAndPersistVideos (repo, callback = () => {}) {
const channels = repo.getChannels()
for (const channel of channels) {
- updateAndPersistVideosForChannel(channel.name, repo)
+ updateAndPersistVideosForChannel(channel.name, repo, callback)
}
console.log('All videos updated')
}
-async function updateAndPersistVideosForChannel(name, repo) {
+async function updateAndPersistVideosForChannel(name, repo, callback = () => {}) {
console.log('updating videos for', name)
const videos = await getVideosFor(name)
if (videos) {
videos.map(v => patchVideo(v, repo))
repo.saveChannelVideos(name, videos)
- broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
+ callback({name, videos})
}
return videos
}
diff --git a/lib/subtitles-summary.js b/lib/subtitles-summary.js
index c46c62c..f22da54 100644
--- a/lib/subtitles-summary.js
+++ b/lib/subtitles-summary.js
@@ -39,7 +39,7 @@ export async function summarizeVideo(id, repo, callback = () => {}) {
repo.setVideoTranscript(id, cleanedTranscript)
fs.writeFileSync(`./data/videos/${id}-sub.en.txt`, cleanedTranscript)
- const prompt = `Summarize the following video transcript, in max 10 sentences, your output should just contain the summary, don't repeat yourself, keep it short, reply with just the summary: \n\n ${cleanedTranscript}`
+ const prompt = `Summarize the following video transcript, in max 10 sentences, keep it short, don't repeat yourself:\n${cleanedTranscript}`
console.log('prompt', prompt)
const summary = await fetchAIResponseLMSTUDIO(prompt)
console.log('summary', summary)
@@ -63,8 +63,8 @@ async function fetchAIResponseLMSTUDIO (prompt) {
body: JSON.stringify({
"model": "llama-3.2-3b-instruct",
"prompt": prompt,
- "temperature": 0.7,
- // "max_tokens": 10,
+ "temperature": 0.9,
+ "max_tokens": 1000,
"stream": false,
// "stop": "\n"
})
diff --git a/yt.test.js b/test/yt.test.js
similarity index 100%
rename from yt.test.js
rename to test/yt.test.js