Skip to content

Instantly share code, notes, and snippets.

@riderx
Last active January 9, 2023 20:13
Show Gist options
  • Save riderx/d0f2dd37f2f4665e5ca495779478b528 to your computer and use it in GitHub Desktop.
Save riderx/d0f2dd37f2f4665e5ca495779478b528 to your computer and use it in GitHub Desktop.
Cloudflare worker to rewrite podcast number to title for podcastpage
export default {
async fetch(request, env) {
return await handleRequest(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) {
// return new Response("Hello world")
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) {
const endNumb = Number(end)
const destinationURL = await getlink(endNumb)
return Response.redirect(destinationURL, 301)
}
console.log('ici')
return fetch(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment