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: display external video right after download started

Christian Feice0d9df536b3
diff --git a/lib/router/api.js b/lib/router/api.js
index 6a5f22e..ae0879f 100644
--- a/lib/router/api.js
+++ b/lib/router/api.js
@@ -126,14 +126,18 @@ async function downloadVideoHandler(req, res, repo, connections = [], state = {}
state.downloading = state.downloading || {}
state.downloading[id] = {lines: []}
+ let broadcastNewVideoOnce = false
downloadVideo(id, repo, (line) => {
+ if (external && !broadcastNewVideoOnce) {
+ broadcastSSE(JSON.stringify({type: 'new-videos', videos: [repo.getVideo(id)]}), connections)
+ broadcastNewVideoOnce = true
+ }
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)
diff --git a/lib/youtube.js b/lib/youtube.js
index 707c082..c1cfe55 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -16,6 +16,9 @@ export async function downloadVideo(id, repo, callback = () => {}) {
if (isUnsupportedUrl(id)) return reject(new Error('unsupported url'))
if (isYouTubeUrl(id)) id = extractIdFromUrl(id)
try {
+ if (!repo.getVideo(id)) {
+ repo.upsertVideos([await getVideo(id)])
+ }
const quality = repo.getVideoQualitySetting()
let location, format
for await (const line of ytdlp.video(id, quality)) {
@@ -26,13 +29,6 @@ export async function downloadVideo(id, repo, callback = () => {}) {
format = location.substring(location.lastIndexOf('.') + 1)
}
}
- if (!repo.getVideo(id)) {
- const video = await getVideo(id)
- video.location = location
- video.format = format
- repo.upsertVideos([video])
- callback(`downloaded external video ${id}`)
- }
const video = repo.updateVideo(id, {location, format})
if (repo.getTranscodeVideosSetting() && video.location && video.format) {