New provider: yt playlists

This commit is contained in:
Nicolai Ort 2021-08-12 21:44:58 +02:00
parent b5482fe3c3
commit 73447243a3
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
1 changed files with 26 additions and 13 deletions

View File

@ -5,9 +5,9 @@ require('dotenv').config()
let config = { let config = {
domain: process.env.DOMAIN || "localhost:3000", domain: process.env.DOMAIN || "localhost:3000",
https: (process.env.SSL === 'true') || false, https: (process.env.SSL === 'true') || false,
recognizeProviders: true, recognizeProviders: (process.env.RECOGNIZE_PROVIDERS === 'true') || true,
getBaseUrl(){ getBaseUrl() {
if(config.https){ if (config.https) {
return `https://${config.domain}`; return `https://${config.domain}`;
} }
return `http://${config.domain}`; return `http://${config.domain}`;
@ -35,6 +35,10 @@ fastify.get('/a/:id', async (req, res) => {
fastify.get('/yt/:id', async (req, res) => { fastify.get('/yt/:id', async (req, res) => {
res.redirect(302, `https://youtu.be/${req.params.id}`) 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 //Normal shorturls
fastify.get('/:shortcode', async (req, res) => { 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 it doesn't exist: Generate a new code and proceed to creating a new db entry.
*/ */
if (!shortcode) { if (!shortcode) {
if(config.recognizeProviders){ if (config.recognizeProviders) {
const response = checkKnownProviders(target); const response = checkKnownProviders(target);
if(response){ if (response) {
return response; return response;
} }
} }
@ -119,7 +123,7 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => {
} }
//Create a new db entry //Create a new db entry
await knex('urls').insert({target, shortcode}); await knex('urls').insert({ target, shortcode });
return { return {
url: `${config.getBaseUrl()}/${shortcode}`, url: `${config.getBaseUrl()}/${shortcode}`,
@ -133,19 +137,28 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => {
* @param {string} target The target URL * @param {string} target The target URL
* @returns Standard shortening response if provider recognized or null * @returns Standard shortening response if provider recognized or null
*/ */
function checkKnownProviders(target){ function checkKnownProviders(target) {
if(/^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/.test(target)){ const youtubeVideoID = target.match(/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/)
const shortcode = `yt/${target.match(/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/)[1]}` if (youtubeVideoID) {
const shortcode = `yt/${youtubeVideoID[1]}`
return { return {
url: `${config.getBaseUrl()}/${shortcode}`, url: `${config.getBaseUrl()}/${shortcode}`,
shortcode, shortcode,
target target
} }
} }
const amazonID = target.match(/https?:\/\/(www|smile|)\.?(amazon|smile)\.(com|de)(?:(?:\/.*\/|\/)(?:dp|gp))(\/product\/|\/)([A-Z0-9]+)/)[5]; const youtubePlaylistID = target.match(/(?:youtube(?:-nocookie)?\.com\/)playlist\?(?:.*)list=([a-zA-Z0-9_-]*)(?:&.*|)/)
console.log(amazonID) if (youtubePlaylistID) {
if(amazonID){ const shortcode = `ytpl/${youtubePlaylistID[1]}`
const shortcode = `a/${amazonID}` 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 { return {
url: `${config.getBaseUrl()}/${shortcode}`, url: `${config.getBaseUrl()}/${shortcode}`,
shortcode, shortcode,