Skip to content

Commit

Permalink
Split in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
alecsg77 committed Mar 20, 2022
1 parent 2f78761 commit e62655a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 57 deletions.
5 changes: 2 additions & 3 deletions src/RaiPlaySound.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare namespace RaiPlaySound {
declare module 'RaiPlaySound' {

interface PageUpdated {
date: string;
Expand Down Expand Up @@ -225,5 +225,4 @@ declare namespace RaiPlaySound {
track_info: TrackInfo;
}

}

}
7 changes: 7 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fetch from 'node-fetch';
import { ProgrammaInfo } from 'RaiPlaySound';

export async function fetchProgrammaAsync(programma: string) {
const response = await fetch(`https://www.raiplaysound.it/programmi/${programma}.json`);
return await response.json() as Promise<ProgrammaInfo>;
}
47 changes: 47 additions & 0 deletions src/feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Feed } from 'feed';
import moment from 'moment';
import { ProgrammaInfo } from 'RaiPlaySound';
export { Feed } from 'feed';

export function newProgrammaFeed(programmaInfo: ProgrammaInfo) {
const feed = new Feed({
title: programmaInfo.podcast_info.title,
description: programmaInfo.podcast_info.description,
id: programmaInfo.podcast_info.uniquename,
link: 'https://www.raiplaysound.it/' + programmaInfo.podcast_info.weblink,
language: "it-it",
image: 'https://www.raiplaysound.it/' + programmaInfo.podcast_info.image,
copyright: "Rai - Radiotelevisione Italiana Spa",
updated: moment(programmaInfo.block.update_date, "DD-MM-YYYY hh:mm:ss").toDate(), // optional, default = today
generator: "RaiPlay Sound",
author: {
name: "RaiPlay Sound",
email: "[email protected]"
}
});

programmaInfo.podcast_info.genres.forEach(genre => feed.addCategory(genre.name));
programmaInfo.podcast_info.subgenres.forEach(subgenre => feed.addCategory(subgenre.name));

programmaInfo.block.cards.forEach(post => {
feed.addItem({
title: post.episode_title,
link: 'https://www.raiplaysound.it/' + post.weblink,
id: post.uniquename,
description: post.description,
date: moment(post.literal_publication_date, "DD MMM YYYY", "it").toDate() || moment(post.create_date, "DD-MM-YYYY").toDate(),
image: 'https://www.raiplaysound.it/' + post.image,
audio: {
url: (post.downloadable_audio || post.audio).url,
title: (post.downloadable_audio || post.audio).title,
type: 'audio/mpeg'
}
});
});

feed.addContributor({
email: programmaInfo.podcast_info.contacts.email,
});

return feed;
}
59 changes: 5 additions & 54 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import express from 'express';
import fetch from 'node-fetch';
import pino from 'pino-http';
import { Feed } from 'feed';
import moment from 'moment';
import cache from './cache';
import { fetchProgrammaAsync } from './api';
import { newProgrammaFeed } from './feed';

const app = express()
const port = 3000
Expand All @@ -12,8 +11,8 @@ app.use(pino());

app.get('/:programma', cache(60), async (req, res, next) => {
try {
const programmaInfo = await getProgrammaInfoAsync(`https://www.raiplaysound.it/programmi/${req.params.programma}.json`);
const feed = generateFeed(programmaInfo);
const programmaInfo = await fetchProgrammaAsync(req.params.programma);
const feed = newProgrammaFeed(programmaInfo);
res.set('Content-Type', 'application/rss+xml');
res.send(feed.rss2());
} catch (error) {
Expand All @@ -23,52 +22,4 @@ app.get('/:programma', cache(60), async (req, res, next) => {

app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})

async function getProgrammaInfoAsync(url: string) {
const response = await fetch(url);
return await response.json() as Promise<RaiPlaySound.ProgrammaInfo>;
}

function generateFeed(programmaInfo: RaiPlaySound.ProgrammaInfo) {
const feed = new Feed({
title: programmaInfo.podcast_info.title,
description: programmaInfo.podcast_info.description,
id: programmaInfo.podcast_info.uniquename,
link: 'https://www.raiplaysound.it/' + programmaInfo.podcast_info.weblink,
language: "it-it",
image: 'https://www.raiplaysound.it/' + programmaInfo.podcast_info.image,
copyright: "Rai - Radiotelevisione Italiana Spa",
updated: moment(programmaInfo.block.update_date, "DD-MM-YYYY hh:mm:ss").toDate(), // optional, default = today
generator: "RaiPlay Sound",
author: {
name: "RaiPlay Sound",
email: "[email protected]"
}
});

programmaInfo.podcast_info.genres.forEach(genre => feed.addCategory(genre.name));
programmaInfo.podcast_info.subgenres.forEach(subgenre => feed.addCategory(subgenre.name));

programmaInfo.block.cards.forEach(post => {
feed.addItem({
title: post.episode_title,
link: 'https://www.raiplaysound.it/' + post.weblink,
id: post.uniquename,
description: post.description,
date: moment(post.literal_publication_date, "DD MMM YYYY", "it").toDate() || moment(post.create_date, "DD-MM-YYYY").toDate(),
image: 'https://www.raiplaysound.it/' + post.image,
audio: {
url: (post.downloadable_audio || post.audio).url,
title: (post.downloadable_audio || post.audio).title,
type: 'audio/mpeg'
}
});
});

feed.addContributor({
email: programmaInfo.podcast_info.contacts.email,
});

return feed;
}
})

0 comments on commit e62655a

Please sign in to comment.