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