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 = {
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,