-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support YouTube clips via HTML og:video:url parsing
- Loading branch information
Showing
10 changed files
with
169 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const headers = new Headers({ 'content-type': 'application/json' }); | ||
|
||
/** | ||
* @param {string} clip | ||
*/ | ||
export const getYouTubeClipURL = async (clip) => { | ||
const body = JSON.stringify({ clip }); | ||
|
||
return fetch(`/api/utils/youtube`, { method: 'POST', body, headers }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// @ts-ignore | ||
import worker from './worker?nodeWorker'; | ||
|
||
/** | ||
* @param {{ url: string }} workerData | ||
* @returns {Promise<string>} | ||
*/ | ||
export default function youtubeClipURL(workerData) { | ||
return new Promise((resolve, reject) => { | ||
const w = worker({ workerData }); | ||
|
||
w.on('message', resolve); | ||
w.on('error', reject); | ||
|
||
w.on('exit', (/** @type {number} */ code) => { | ||
if (code !== 0) { | ||
reject(new Error(`Worker stopped with exit code ${code}`)); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { error, json } from '@sveltejs/kit'; | ||
|
||
import youtubeClipURL from '$lib/worker/youtubeClipURL'; | ||
|
||
export async function POST({ request }) { | ||
const { clip } = await request.json(); | ||
|
||
if (clip) { | ||
const res = await youtubeClipURL({ url: clip }); | ||
|
||
return json(res); | ||
} | ||
|
||
return error(400); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'linkedom-global'; | ||
|
||
import { workerData, parentPort } from 'node:worker_threads'; | ||
|
||
const youtubeClipURLWorker = async () => { | ||
/** | ||
* @type {{ url: string }} | ||
*/ | ||
const { url } = workerData; | ||
|
||
const parsedURL = new URL('', url); | ||
|
||
if (parsedURL.hostname !== 'www.youtube.com' && parsedURL.hostname !== 'youtube.com') { | ||
parentPort?.postMessage(url); | ||
|
||
return; | ||
} | ||
|
||
const text = await (await fetch(url)).text(); | ||
|
||
const html = new DOMParser().parseFromString(text, 'text/html'); | ||
|
||
/** @type {HTMLMetaElement?} */ | ||
let metaVideoURLTag; | ||
try { | ||
metaVideoURLTag = html.querySelector('meta[property="og:video:url"]'); | ||
} catch { | ||
// noop | ||
} | ||
|
||
parentPort?.postMessage(metaVideoURLTag?.content || url); | ||
}; | ||
|
||
try { | ||
youtubeClipURLWorker(); | ||
} catch (error) { | ||
console.error('toHTMLWorker error', error); | ||
} |