diff --git a/src/dinosaurwet/dinosaurwet.controller.ts b/src/dinosaurwet/dinosaurwet.controller.ts index 7d09ee7..cd73881 100644 --- a/src/dinosaurwet/dinosaurwet.controller.ts +++ b/src/dinosaurwet/dinosaurwet.controller.ts @@ -1,5 +1,6 @@ import { Controller, Get, Res } from '@nestjs/common'; import { Response } from 'express'; +import { identity } from 'ramda'; import * as xml from 'xml'; const episodeFiles: [number, string][] = [ @@ -27,7 +28,16 @@ const episodeFiles: [number, string][] = [ const startDate = new Date('2023-10-02'); -const episodes = episodeFiles.map((file, i) => { +type Episode = { + filename: string; + length: number; + url: string; + title: string; + description: string; + fakeReleaseDate: Date; +}; + +const episodes: Episode[] = episodeFiles.map((file, i) => { const [length, filename] = file; return { filename, @@ -40,69 +50,79 @@ const episodes = episodeFiles.map((file, i) => { .trim(), description: 'A bootleg-ish of Dinosar Wet, making the downloaded files more like and RSS feed', - fakeReleaseDate: new Date(startDate).setDate(startDate.getDate() + i * 7), + fakeReleaseDate: new Date( + new Date(startDate).setDate(startDate.getDate() + i * 7), + ), }; }); +const buildFeeds = ( + episodes: Episode[], + dateFn: (date: Date) => Date = identity, +) => { + const feedObject = { + rss: [ + { + _attr: { + version: '2.0', + 'xmlns:atom': 'http://www.w3.org/2005/Atom', + }, + }, + { + channel: [ + { + 'atom:link': { + _attr: { + href: 'https://api.us.dev/dinosaurwet', + rel: 'self', + type: 'application/rss+xml', + }, + }, + }, + { + title: 'Dinosaur Wet - All At Once', + }, + { + link: 'https://sanspantsradio.com', + }, + { + description: + 'A recreation of Dinosaur Wet as an RSS feed for listeners-with-licenses enjoyment.', + }, + { language: 'en-US' }, + ...episodes.map(({ title, url, length }) => ({ + item: [ + { title }, + { guid: url }, + { pubDate: startDate.toUTCString() }, + { + enclosure: [ + { + _attr: { + url, + type: 'audio/mpeg', + length, + }, + }, + ], + }, + ], + })), + ], + }, + ], + }; + + const feed = '' + xml(feedObject); + return feed; +}; + @Controller('dinosaurwet') export class DinosaurwetController { @Get('') getAllAtOnce(@Res() response: Response) { response.header('Content-Type', 'application/xml'); - const feedObject = { - rss: [ - { - _attr: { - version: '2.0', - 'xmlns:atom': 'http://www.w3.org/2005/Atom', - }, - }, - { - channel: [ - { - 'atom:link': { - _attr: { - href: 'https://api.us.dev/dinosaurwet', - rel: 'self', - type: 'application/rss+xml', - }, - }, - }, - { - title: 'Dinosaur Wet - All At Once', - }, - { - link: 'https://sanspantsradio.com', - }, - { - description: - 'A recreation of Dinosaur Wet as an RSS feed for listeners-with-licenses enjoyment.', - }, - { language: 'en-US' }, - ...episodes.map(({ title, url, length }) => ({ - item: [ - { title }, - { guid: url }, - { pubDate: startDate.toUTCString() }, - { - enclosure: [ - { - _attr: { - url, - type: 'audio/mpeg', - length, - }, - }, - ], - }, - ], - })), - ], - }, - ], - }; - - const feed = '' + xml(feedObject); + const feed = buildFeeds(episodes); return response.send(feed); } }