From 73447243a31110c8ea667bf868a816f80dd1eb25 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 12 Aug 2021 21:44:58 +0200 Subject: [PATCH] New provider: yt playlists --- src/server.js | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/server.js b/src/server.js index c4c9141..c5a0565 100644 --- a/src/server.js +++ b/src/server.js @@ -5,9 +5,9 @@ require('dotenv').config() let config = { domain: process.env.DOMAIN || "localhost:3000", https: (process.env.SSL === 'true') || false, - recognizeProviders: true, - getBaseUrl(){ - if(config.https){ + recognizeProviders: (process.env.RECOGNIZE_PROVIDERS === 'true') || true, + getBaseUrl() { + if (config.https) { return `https://${config.domain}`; } return `http://${config.domain}`; @@ -35,6 +35,10 @@ fastify.get('/a/:id', async (req, res) => { fastify.get('/yt/:id', async (req, res) => { res.redirect(302, `https://youtu.be/${req.params.id}`) }) +//Automagic Youtube redirects on /ytpl/ +fastify.get('/ytpl/:id', async (req, res) => { + res.redirect(302, `https://youtube.com/playlist?list=${req.params.id}`) +}) //Normal shorturls fastify.get('/:shortcode', async (req, res) => { @@ -82,9 +86,9 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => { * If it doesn't exist: Generate a new code and proceed to creating a new db entry. */ if (!shortcode) { - if(config.recognizeProviders){ + if (config.recognizeProviders) { const response = checkKnownProviders(target); - if(response){ + if (response) { return response; } } @@ -119,7 +123,7 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => { } //Create a new db entry - await knex('urls').insert({target, shortcode}); + await knex('urls').insert({ target, shortcode }); return { url: `${config.getBaseUrl()}/${shortcode}`, @@ -133,19 +137,28 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => { * @param {string} target The target URL * @returns Standard shortening response if provider recognized or null */ -function checkKnownProviders(target){ - if(/^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/.test(target)){ - const shortcode = `yt/${target.match(/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/)[1]}` +function checkKnownProviders(target) { + const youtubeVideoID = target.match(/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/) + if (youtubeVideoID) { + const shortcode = `yt/${youtubeVideoID[1]}` return { url: `${config.getBaseUrl()}/${shortcode}`, shortcode, target } } - const amazonID = target.match(/https?:\/\/(www|smile|)\.?(amazon|smile)\.(com|de)(?:(?:\/.*\/|\/)(?:dp|gp))(\/product\/|\/)([A-Z0-9]+)/)[5]; - console.log(amazonID) - if(amazonID){ - const shortcode = `a/${amazonID}` + const youtubePlaylistID = target.match(/(?:youtube(?:-nocookie)?\.com\/)playlist\?(?:.*)list=([a-zA-Z0-9_-]*)(?:&.*|)/) + if (youtubePlaylistID) { + const shortcode = `ytpl/${youtubePlaylistID[1]}` + return { + url: `${config.getBaseUrl()}/${shortcode}`, + shortcode, + target + } + } + const amazonID = target.match(/https?:\/\/(www|smile|)\.?(amazon|smile)\.(de)(?:(?:\/.*\/|\/)(?:dp|gp))(\/product\/|\/)([A-Z0-9]+)/); + if (amazonID) { + const shortcode = `a/${amazonID[5]}` return { url: `${config.getBaseUrl()}/${shortcode}`, shortcode,