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: extractIdFromUrl

Christian Feiae8f62373667
diff --git a/client/components/search-videos.js b/client/components/search-videos.js
index 76e2505..e16fe9c 100644
--- a/client/components/search-videos.js
+++ b/client/components/search-videos.js
@@ -32,11 +32,10 @@ class SearchVideos extends HTMLElement {
}
if (searchTerm.match('https?://')) {
- const id = searchTerm.replace(/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/watch\?v=(.+)$/, '$4')
fetch('/api/download-video', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ id, external: true }),
+ body: JSON.stringify({ id: searchTerm, external: true }),
})
.then(() => console.log('Download started'))
.catch((error) => console.error('Error starting download:', error))
diff --git a/lib/youtube.js b/lib/youtube.js
index b20c8b7..280d517 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -17,7 +17,7 @@ function getOptionalCookiesPath() {
export async function downloadVideo(id, repo, callback = () => {}) {
return new Promise(async (resolve, reject) => {
- if (id.startsWith('http')) id = id.replace(/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/watch\?v=(.+)$/, '$4')
+ if (id.startsWith('https?://')) id = extractIdFromUrl(id)
try {
const cookiesPath = getOptionalCookiesPath()
let cookiesOption = cookiesPath ? `--cookies ${cookiesPath}` : ''
@@ -115,7 +115,7 @@ export async function getVideosFor(channelName) {
}
export async function getVideo (id) {
- if (id.startsWith('http')) id = id.replace(/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/watch\?v=(.+)$/, '$4')
+ if (id.startsWith('https?://')) id = extractIdFromUrl(id)
try {
const {output} = await spawn('yt-dlp', ['-j', '--', id])
const json = JSON.parse(output)
@@ -171,3 +171,7 @@ function parseRelativeTime(relativeTime) {
console.error('Invalid relative time format', relativeTime);
return null
}
+
+function extractIdFromUrl (url) {
+ return url.replace(/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/watch\?v=(.+)$/, '$4')
+}
\ No newline at end of file