You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

3 years ago
import got from 'got'
import url from 'url'
import path from 'path'
import { createWriteStream } from 'fs'
import Action from './action.js'
/**
*
*
* @extends Action
*
*/
class DownloadPodcast extends Action
{
async run()
{
// count of files
let files = 0
// errors
let errors = []
for (let item of this.data.rss.channel.item)
{
// gettin
const pubDate = new Date(item.pubDate)
// check for new entries
if (pubDate >= new Date(this.source.last_run_at))
{
const parsedUrl = url.parse(item.link)
const filename = decodeURIComponent(path.basename(parsedUrl.pathname))
const downloadStream = got.stream(item.link)
const fileWriterStream = createWriteStream(this.options.destination + '/' + filename)
downloadStream
.on('error', (error) => {
console.error(`Download failed: ${error.message}`)
})
fileWriterStream
.on('error', (error) => {
console.error(`Could not write file to system: ${error.message}`)
})
.on('finish', () => {
files++
})
await downloadStream.pipe(fileWriterStream)
}
}
}
}
export default DownloadPodcast