Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Only generate audio for items with content. #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions bin/lib/autovoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const constructRSS = require('./constructRSS');
const formatContentForReading = require('./formatContentForReading');
const fetchContent = require('./fetchContent');
const individualUUIDs = require('./individualUUIDs');
const filterNullItems = require('./filter-null');

function processItemToMp3(item, voiceId){
debug(`processItemToMp3: index=${item.processingIndex}, item.keys=${JSON.stringify(Object.keys(item))}, voiceId=${voiceId}`);
Expand Down Expand Up @@ -88,10 +89,16 @@ function generatePodcast(rssUrl, voiceId=tts.defaultVoiceId){
debug(`generatePodcast: items.length=${items.length}`);
const promises = items.map( (item, i) => {
item.processingIndex = i;
return processItemToMp3(item, voiceId);
return processItemToMp3(item, voiceId)
.catch(err => {
debug(`An error occurred trying to generate speech for item ${item.uuid}. Err:`, err);
return null;
})
;
} );
return Promise.all(promises);
})
.then( items => filterNullItems(items) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not inline?
.then( items => { return items.filter( item => {return item != null; ) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to modularise these snippets. Saves us having to change code in multiple places if we want to filter by a different condition.

.then( items => {
const feed = constructRSS(rssUrl, items);
return feed;
Expand All @@ -109,10 +116,16 @@ function generateFirstFtBasedPodcast(maxResults, requestedUrl, includeFirstFtUui
debug(`generateFirstFtBasedPodcast: items.length=${items.length}`);
const promises = items.map( (item, i) => {
item.processingIndex = i;
return processItemToMp3(item, voiceId);
return processItemToMp3(item, voiceId)
.catch(err => {
debug(`An error occurred trying to generate speech for item ${item.uuid}. Err:`, err);
return null;
})
;
} );
return Promise.all(promises);
})
.then( items => filterNullItems(items) )
.then( items => constructRSS(requestedUrl, items) )
;
}
Expand Down
9 changes: 9 additions & 0 deletions bin/lib/filter-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const debug = require('debug')('bin:lib:filter-null');

module.exports = function(listWithNulls){

return listWithNulls.filter(item => {
return item !== null;
});

}
4 changes: 4 additions & 0 deletions bin/lib/formatContentForReading.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ function wrap(itemData) {

let texts = [];

if(itemData.content === undefined){
throw 'itemData.content is undefined. There is nothing to read aloud.';
}

texts.push(`This article is narrated by Experimental ${itemData.voiceId}.`);
texts.push( itemData.content + '.' );

Expand Down