PostscodeLight Snow, 33°F
Code: podcastImporter.ts
import * as request from 'request-promise'; import * as moment from 'moment';
const twodads = 'http://www.unmung.com/mf2?url=http%3A%2F%2Fwww.unmung.com%2Ffeed%3Ffeed%3Dhttps%253A%252F%252Ffeeds.twodads.fm&html='; const myurlis = 'http://www.unmung.com/mf2?url=http%3A%2F%2Fwww.unmung.com%2Ffeed%3Ffeed%3Dhttp%253A%252F%252Ffeeds.myurlis.com%252Fpodcast&html='; const thirtycounting = 'http://www.unmung.com/mf2?url=http%3A%2F%2Fwww.unmung.com%2Ffeed%3Ffeed%3Dhttp%253A%252F%252Ffeeds.30andcounting.me%252Fpodcast&html=';
let podcastName, podcastCover; let podcasts = [twodads, myurlis, thirtycounting];
podcasts.forEach(podcastUrl => { request.get(podcastUrl, { json: true }).then(body => { if (body.items !== undefined) { let today = moment().hour(0).minute(0).second(0);
podcastName = body.items[0].properties.name[0];
podcastCover = body.items[0].properties.author[0].properties.photo[0];
let episodesToSend = body.items[0].children.filter(entry => today.diff(moment(entry.properties.updated[0]), "days") < 1 ).map(convertMf2ToMicropub);
if (episodesToSend.length > 0) {
sendEpisodesViaMicropub(episodesToSend);
} else {
console.log(`${podcastName} did not have any episodes today`);
}
}
});
});
function convertMf2ToMicropub(mfObject) { let micropubPost = mfObject;
if (micropubPost.properties.name[0].indexOf(podcastName) != 0) {
micropubPost.properties.name[0] = `${podcastName}, ${micropubPost.properties.name[0]}`;
}
micropubPost.properties.featured = [podcastCover];
micropubPost.properties.published = micropubPost.properties.updated;
micropubPost.properties.canonical = micropubPost.properties.url;
delete micropubPost.properties.summary;
delete micropubPost.properties.updated;
delete micropubPost.properties.url;
return micropubPost;
}
function sendEpisodesViaMicropub(episodesToSend) {
episodesToSend.forEach(postData => {
request.post(${micropubEndpoint}, {
'auth': {
'bearer': ${micropubToken}
},
body: postData,
json: true
}, (err, data) => {
if (err != undefined) {
console.log(ERROR: ${err});
}
if (data.statusCode !== 201 && data.statusCode !== 202) {
console.log("Podcast Episode Micropub error");
} else {
console.log("Successfully created Micropub request for " + postData.properties.name[0]);
}
});
});
}
Posted by Quill