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: extract id from yt url

Christian Feiff10234a13e3
diff --git a/lib/server.js b/lib/server.js
index 247f787..cb07a56 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 } from './youtube.js'
+import { downloadVideo, extractIdFromUrl, isYouTubeUrl } from './youtube.js'
import { updateAndPersistVideos, updateAndPersistVideosForChannel } from './update-videos.js'
export function createServer ({repo, connections = []}) {
@@ -122,7 +122,9 @@ export function createServer ({repo, connections = []}) {
async function downloadVideoHandler(req, res, repo, connections = []) {
const body = await getBody(req)
- const { id, external } = JSON.parse(body)
+ let { id, external } = JSON.parse(body)
+
+ if (isYouTubeUrl(id)) id = extractIdFromUrl(id)
downloadVideo(id, repo, (line) =>
broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
diff --git a/lib/youtube.js b/lib/youtube.js
index b1eda7b..e8c4140 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -1,6 +1,8 @@
import spawn from 'nano-spawn'
import fs from 'fs'
+export const ytUrlRegExp = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/watch\?v=([^&]+).*/
+
const fetchYoutubeHeaders = {
'Accept-Language': 'en',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
@@ -17,7 +19,7 @@ function getOptionalCookiesPath() {
export async function downloadVideo(id, repo, callback = () => {}) {
return new Promise(async (resolve, reject) => {
- if (id.startsWith('https?://')) id = extractIdFromUrl(id)
+ if (isYouTubeUrl(id)) id = extractIdFromUrl(id)
try {
const cookiesPath = getOptionalCookiesPath()
let cookiesOption = cookiesPath ? `--cookies ${cookiesPath}` : ''
@@ -133,7 +135,7 @@ export async function getVideosFor(channelName) {
}
export async function getVideo (id) {
- if (id.startsWith('https?://')) id = extractIdFromUrl(id)
+ if (isYouTubeUrl(id)) id = extractIdFromUrl(id)
try {
const {output} = await spawn('yt-dlp', ['-j', '--', id])
const json = JSON.parse(output)
@@ -190,6 +192,10 @@ function parseRelativeTime(relativeTime) {
return null
}
-function extractIdFromUrl (url) {
- return url.replace(/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/watch\?v=(.+)$/, '$4')
+export function extractIdFromUrl (url) {
+ return url.replace(ytUrlRegExp, '$4')
+}
+
+export function isYouTubeUrl(url) {
+ return ytUrlRegExp.test(url)
}
\ No newline at end of file
diff --git a/test/youtube.test.js b/test/youtube.test.js
index 7210bf7..fad132b 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 } from '../lib/youtube.js'
+import { getVideosFor, getVideo, extractIdFromUrl, isYouTubeUrl } from '../lib/youtube.js'
test('gets videos for channel', async () => {
const videos = await getVideosFor('veritasium')
@@ -29,4 +29,18 @@ test('gets single video', async () => {
assert.equal(video.publishedTime, '2025-03-05')
assert.ok(video.viewCount)
assert.equal(video.duration, '33:36')
+})
+
+test('extracts id from url', () => {
+ assert.equal(extractIdFromUrl('https://www.youtube.com/watch?v=SOME_ID&pp=something'), 'SOME_ID')
+ assert.equal(extractIdFromUrl('https://www.youtube.com/watch?v=SOME_ID'), 'SOME_ID')
+ assert.equal(extractIdFromUrl('https://youtube.com/watch?v=SOME_ID'), 'SOME_ID')
+ assert.equal(extractIdFromUrl('https://youtu.be/watch?v=SOME_ID'), 'SOME_ID')
+})
+
+test('check if url is a youtube url', () => {
+ assert.ok(isYouTubeUrl('https://www.youtube.com/watch?v=SOME_ID&pp=something'))
+ 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'))
})
\ No newline at end of file