Created
November 2, 2022 00:24
-
-
Save shawnmclean/2011d15e92810dfeaa48cff7428114f1 to your computer and use it in GitHub Desktop.
Spotify Podcast RSS to Contentful Migration
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
import { env } from "process"; | |
import * as Parser from "rss-parser"; | |
import slufigy from "slugify"; | |
import * as Contentful from "contentful-management"; | |
import { htmlToText } from "html-to-text"; | |
const client = Contentful.createClient({ | |
accessToken: env.ACCESS_TOKEN, | |
}); | |
type CustomFeed = { title: string }; | |
type PodcastItem = { | |
creator: string; | |
title: string; | |
link: string; | |
isoDate: string; | |
content: string; | |
}; | |
const parser: Parser<CustomFeed, PodcastItem> = new Parser(); | |
(async () => { | |
const podcastRSS = "https://anchor.fm/s/7c41ab0/podcast/rss"; | |
const feed = await parser.parseURL(podcastRSS); | |
const space = await client.getSpace(env.SPACE_ID); | |
const environment = await space.getEnvironment("master"); | |
for (const item of feed.items) { | |
console.log(item.title); | |
await rowToEntry(environment, item); | |
await new Promise((resolve) => setTimeout(resolve, 200)); | |
} | |
})(); | |
async function rowToEntry(env: Contentful.Environment, row: PodcastItem) { | |
const slug = slufigy(row.title); | |
const description = htmlToText(row.content); | |
return await env.createEntry("podcast", { | |
fields: { | |
title: { | |
"en-US": row.title, | |
}, | |
published: { | |
"en-US": row.isoDate, | |
}, | |
slug: { | |
"en-US": slug, | |
}, | |
description: { | |
"en-US": description, | |
}, | |
content: { | |
"en-US": row.link, | |
}, | |
}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment