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: remove Promise anti-patterns, cleanup youtube.js

Christian Fei0285c78bac28
diff --git a/lib/youtube.js b/lib/youtube.js
index 7196d90..7e0e091 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -1,8 +1,6 @@
-/* eslint-disable no-async-promise-executor */
import spawn from 'nano-spawn'
import fs from 'fs'
import * as ytdlp from './yt-dlp.js'
-import { execSync } from 'child_process'
import { runWorkerDownload, runWorkerTranscode } from './workers/run-worker.js'
export const ytUrlRegExp = /^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/(?:watch\?v=)?)([\w-]+)/
@@ -24,76 +22,54 @@ function parseDuration (durationStr) {
}
export async function downloadVideo (id, repo, callback = () => {}) {
- return new Promise(async (resolve, reject) => {
- 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
-
- await runWorkerDownload(id, quality, (message) => {
- if (message.status === 'info') {
- console.log(message.line)
- callback(message)
- } else {
- callback(message)
- }
- })
- .then((result) => {
- console.log('Video processing completed', result)
- location = result.location
- format = result.format
- })
- .catch((error) => {
- console.error(`Error: ${error.message}`)
- })
-
- const video = repo.updateVideo(id, { location, format })
-
- if (repo.getTranscodeVideosSetting() && video.location && video.format) {
- await runWorkerTranscode(video.location, video.format, parseDuration(video.duration), (message) => {
- console.log(message.line)
- callback(message)
- })
- execSync(`mv ${location.replace(format, 'tmp.' + format)} ${location}`)
-
- console.log(`successfully transcoded video ${video.location}`)
- callback(`successfully transcoded video ${video.location}`) // eslint-disable-line
- }
+ if (isUnsupportedUrl(id)) throw new Error('unsupported url')
- repo.updateVideo(id, { downloaded: true })
+ const videoId = isYouTubeUrl(id) ? extractIdFromUrl(id) : id
+ if (!repo.getVideo(videoId)) {
+ repo.upsertVideos([await getVideo(videoId)])
+ }
- normalizeSubtitleFiles(id)
- resolve(id)
- } catch (error) {
- console.error(error)
- console.error('Error downloading video:', error.message)
- reject(error)
- }
- })
+ const quality = repo.getVideoQualitySetting()
+ console.log('Video processing completed', await runWorkerDownload(videoId, quality, (message) => {
+ callback(message)
+ }))
+
+ const video = repo.updateVideo(videoId, { location: null, format: null })
+ if (repo.getTranscodeVideosSetting() && video.location && video.format) {
+ await runWorkerTranscode(video.location, video.format, parseDuration(video.duration), (message) => {
+ callback(message)
+ })
+
+ const tmpLocation = video.location.replace(video.format, 'tmp.' + video.format)
+ fs.renameSync(tmpLocation, video.location)
+
+ console.log(`successfully transcoded video ${video.location}`)
+ }
+
+ repo.updateVideo(videoId, { downloaded: true })
+ normalizeSubtitleFiles(videoId)
+
+ return videoId
}
export async function getVideoSubtitles (id, callback = () => {}) {
- return new Promise(async (resolve, reject) => {
- try {
- const transcriptPath = `./data/videos/${id}.en.srt`
- if (!fs.existsSync(transcriptPath)) {
- for await (const line of ytdlp.subtitles(id)) {
- console.log(line)
- callback(line)
- }
- }
- normalizeSubtitleFiles(id)
- const transcript = fs.readFileSync(transcriptPath, 'utf-8')
- resolve(transcript)
- } catch (error) {
- console.error('Error downloading video subtitles:', error.message)
- reject(error)
+ const transcriptPath = `./data/videos/${id}.en.srt`
+ if (!fs.existsSync(transcriptPath)) {
+ for await (const line of ytdlp.subtitles(id)) {
+ console.log(line)
+ callback(line)
}
- })
+ }
+
+ normalizeSubtitleFiles(id)
+
+ try {
+ const transcript = fs.readFileSync(transcriptPath, 'utf-8')
+ return transcript
+ } catch (error) {
+ console.error('Error downloading video subtitles:', error.message)
+ throw error
+ }
}
function normalizeSubtitleFiles (id) {