Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

fix: better handle unsupported url

Christian Fei7c9e2609bc64
diff --git a/lib/server.js b/lib/server.js
index d3e3f34..540d90d 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -3,7 +3,7 @@ import { URL } from 'url'
import fs from 'fs'
import { summarizeVideo } from './subtitles-summary.js'
import { handleSSE, broadcastSSE } from './sse.js'
-import { downloadVideo, extractIdFromUrl, isYouTubeUrl } from './youtube.js'
+import { downloadVideo, extractIdFromUrl, isUnsupportedUrl, isYouTubeUrl } from './youtube.js'
import { updateAndPersistVideos, updateAndPersistVideosForChannel } from './update-videos.js'
export function createServer ({repo, connections = []}) {
@@ -128,6 +128,11 @@ export function createServer ({repo, connections = []}) {
const body = await getBody(req)
let { id, external } = JSON.parse(body)
+ if (isUnsupportedUrl(id)) {
+ console.log('unsupported url', id)
+ res.writeHead(400)
+ return res.end()
+ }
if (isYouTubeUrl(id)) id = extractIdFromUrl(id)
downloadVideo(id, repo, (line) =>
diff --git a/lib/youtube.js b/lib/youtube.js
index b68f465..9e2d83b 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -19,6 +19,8 @@ function getOptionalCookiesPath() {
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 {
const cookiesPath = getOptionalCookiesPath()
@@ -137,6 +139,7 @@ export async function getVideosFor(channelName) {
}
export async function getVideo (id) {
+ if (isUnsupportedUrl(id)) return null
if (isYouTubeUrl(id)) id = extractIdFromUrl(id)
try {
const {output} = await spawn('yt-dlp', ['-j', '--', id])
@@ -200,4 +203,12 @@ export function extractIdFromUrl (url) {
export function isYouTubeUrl(url) {
return ytUrlRegExp.test(url)
+}
+
+export function isUnsupportedUrl(url) {
+ return !ytUrlRegExp.test(url) && !isVideoId(url)
+}
+
+export function isVideoId(url) {
+ return !/^https?/.test(url)
}
\ No newline at end of file
diff --git a/test/youtube.test.js b/test/youtube.test.js
index fad132b..2f1ae09 100644
--- a/test/youtube.test.js
+++ b/test/youtube.test.js
@@ -1,6 +1,6 @@
import { test } from 'node:test'
import assert from 'assert';
-import { getVideosFor, getVideo, extractIdFromUrl, isYouTubeUrl } from '../lib/youtube.js'
+import { getVideosFor, getVideo, extractIdFromUrl, isYouTubeUrl, isUnsupportedUrl, isVideoId } from '../lib/youtube.js'
test('gets videos for channel', async () => {
const videos = await getVideosFor('veritasium')
@@ -43,4 +43,16 @@ test('check if url is a youtube url', () => {
assert.ok(isYouTubeUrl('https://www.youtube.com/watch?v=SOME_ID'))
assert.ok(isYouTubeUrl('https://youtube.com/watch?v=SOME_ID'))
assert.ok(isYouTubeUrl('https://youtu.be/watch?v=SOME_ID'))
+})
+
+test('checks if url is unsupported', () => {
+ assert.ok(isUnsupportedUrl('https://somesite.com/video'))
+ assert.ok(!isUnsupportedUrl('qJZ1Ez28C-A'))
+})
+
+test('checks if url is a video id', () => {
+ assert.ok(isVideoId('qJZ1Ez28C-A'))
+ assert.ok(isVideoId('SOME_ID'))
+ assert.ok(!isVideoId('https://www.youtube.com/watch?v=SOME_ID'))
+ assert.ok(!isVideoId('https://somesite.com/video'))
})
\ No newline at end of file