Created
January 11, 2023 05:22
-
-
Save riderx/2b12ceacc41fce04364729572014c76f to your computer and use it in GitHub Desktop.
Cloudflare worker
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
export default { | |
async fetch(request, env, context) { | |
const url = new URL(request.url) | |
const end = url.pathname.split('/').pop() | |
// check if end is number with regex | |
const regex = /\d/g | |
const isNumber = regex.test(end) | |
if (isNumber && url.pathname.startsWith('/episode/')) { | |
// is number search for the new name and redirect | |
return await handleRequest(request) | |
} | |
return request; | |
} | |
} | |
const getlink = async (number) => { | |
const resp = await fetch('https://anchor.fm/s/414d1d4/podcast/rss') | |
const data = await resp.text() | |
// search all occurence of https://anchor.fm/indiemakers/episodes/.*< in data | |
// search all occurence of <title><![CDATA[.*]]></title> | |
const regex = /<title><!\[CDATA\[.*\]\]><\/title>/g | |
const links = data.match(regex) | |
const episodes = links.map(link => link | |
.toLowerCase() | |
.normalize('NFD') | |
.replace(']]></title>', '') | |
.replace('<title><![cdata[', '') | |
.replace(/[^a-z0-9 ]/gi, '') | |
.trim() | |
.replace(/ /g, '-') | |
.replace(/,/g, '') | |
.replace(/'/g, '') | |
.toLowerCase(), | |
) | |
const link = episodes[episodes.length - number] | |
const newLink = `https://indiemakers.fr/episode/${link}` | |
return newLink | |
} | |
async function handleRequest(request) { | |
const url = new URL(request.url) | |
const end = url.pathname.split('/').pop() | |
const endNumb = Number(end) | |
console.log('end', endNumb) | |
const destinationURL = await getlink(endNumb) | |
return Response.redirect(destinationURL, 301) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment