95 lines
2.9 KiB
JavaScript
95 lines
2.9 KiB
JavaScript
const {seasonEpisodeFormat, episodeNumber} = require('../../utils/filters');
|
|
const fs = require('fs/promises');
|
|
path = require('path')
|
|
|
|
module.exports = {
|
|
"layout": "episode",
|
|
"tags":['episode'],
|
|
"eleventyComputed": {
|
|
"episode": "{{page.fileSlug | episodeNumber: episode}}",
|
|
"image": async (data) => {
|
|
const image_locations_promises = [
|
|
data.image,
|
|
`${data.page.filePathStem}.jpg`,
|
|
`${data.page.filePathStem}.png`,
|
|
'../image.jpg',
|
|
'../image.png',
|
|
'../../image.jpg',
|
|
'../../image.png'
|
|
].filter(image_path=>image_path).map((image_path)=>
|
|
fs.access(path.resolve(data.page.inputPath, image_path))
|
|
.then(()=>path.resolve(data.page.filePathStem, image_path))
|
|
.catch(()=>null)
|
|
)
|
|
return (await Promise.all(image_locations_promises)).find(i=>i)
|
|
},
|
|
"podcast": podcastData
|
|
}
|
|
}
|
|
|
|
async function podcastData (data) {
|
|
var file_stem
|
|
if (data.season == 1) {
|
|
file_stem = `ep${data.episode}`
|
|
} else if (data.season == 2) {
|
|
file_stem = `${seasonEpisodeFormat(null, {...data, episodePrefix: "ep"}).toLowerCase()}`
|
|
} else {
|
|
file_stem = `${seasonEpisodeFormat(null, data).toLowerCase()}`
|
|
}
|
|
const mp3_exists_promise = new Promise ((resolve, reject) => {
|
|
if (data.podcast.enclosureUrl) {
|
|
resolve(data.podcast.enclosureUrl)
|
|
} else {
|
|
const url = `${data.site.cdn}/${file_stem}.mp3`
|
|
fetch(url, { method: "HEAD" })
|
|
.then((res)=>{
|
|
if (res.ok) {
|
|
resolve(`${url}`)
|
|
} else {
|
|
reject(new Error(`No file at ${url}`));
|
|
}
|
|
})
|
|
}
|
|
}).catch((e)=>{return null})
|
|
|
|
const transcript_page_exists_promises = [
|
|
`${data.page.filePathStem}`,
|
|
`../../../transcripts/${file_stem}`
|
|
].filter(file_path=>file_path).map((file_path)=>
|
|
fs.access(path.resolve(data.page.inputPath, `${file_path}.srt`))
|
|
.then(()=>path.resolve(data.page.filePathStem, file_path))
|
|
.catch((e)=>{
|
|
return null
|
|
})
|
|
)
|
|
|
|
transcript_file_exists_promise = new Promise ((resolve, reject) => {
|
|
if (data.podcast.transcriptUrl) {
|
|
resolve(data.podcast.transcriptUrl)
|
|
} else {
|
|
const url = `${data.site.cdn}/${file_stem}.srt`
|
|
fetch(url, { method: "HEAD" })
|
|
.then((res)=>{
|
|
if (res.ok) {
|
|
resolve(`${url}`)
|
|
} else {
|
|
reject(new Error(`No file at ${url}`));
|
|
}
|
|
})
|
|
}
|
|
}).catch((e)=>{return null})
|
|
|
|
const result = {
|
|
"enclosureUrl": await mp3_exists_promise,
|
|
"transcriptUrl": await transcript_file_exists_promise,
|
|
"transcriptPage": (await Promise.all(transcript_page_exists_promises)).find(i=>i),
|
|
"title": data.podcast.title || `${seasonEpisodeFormat(null, data)}: ${data.title || "Episode " + data.episode}`,
|
|
"image" : data.podcast.image || data.image
|
|
}
|
|
|
|
if (result.enclosureUrl != null) {
|
|
return result
|
|
} else {
|
|
return false
|
|
}
|
|
} |