-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
57 deletions.
There are no files selected for viewing
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
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
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>; | ||
} |
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
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; | ||
} |
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
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 | ||
|
@@ -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) { | ||
|
@@ -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; | ||
} | ||
}) |