Eddie Hinkle

Login

podcastImporter.ts

  1. import * as request from 'request-promise';
  2. import * as moment from 'moment';
  3. const twodads = 'http://www.unmung.com/mf2?url=http%3A%2F%2Fwww.unmung.com%2Ffeed%3Ffeed%3Dhttps%253A%252F%252Ffeeds.twodads.fm&html=';
  4. const myurlis = 'http://www.unmung.com/mf2?url=http%3A%2F%2Fwww.unmung.com%2Ffeed%3Ffeed%3Dhttp%253A%252F%252Ffeeds.myurlis.com%252Fpodcast&html=';
  5. const thirtycounting = 'http://www.unmung.com/mf2?url=http%3A%2F%2Fwww.unmung.com%2Ffeed%3Ffeed%3Dhttp%253A%252F%252Ffeeds.30andcounting.me%252Fpodcast&html=';
  6. let podcastName, podcastCover;
  7. let podcasts = [twodads, myurlis, thirtycounting];
  8. podcasts.forEach(podcastUrl => {
  9. request.get(podcastUrl, {
  10. json: true
  11. }).then(body => {
  12. if (body.items !== undefined) {
  13. let today = moment().hour(0).minute(0).second(0);
  14. podcastName = body.items[0].properties.name[0];
  15. podcastCover = body.items[0].properties.author[0].properties.photo[0];
  16. let episodesToSend = body.items[0].children.filter(entry => today.diff(moment(entry.properties.updated[0]), "days") < 1 ).map(convertMf2ToMicropub);
  17. if (episodesToSend.length > 0) {
  18. sendEpisodesViaMicropub(episodesToSend);
  19. } else {
  20. console.log(`${podcastName} did not have any episodes today`);
  21. }
  22. }
  23. });
  24. });
  25. function convertMf2ToMicropub(mfObject) {
  26. let micropubPost = mfObject;
  27. if (micropubPost.properties.name[0].indexOf(podcastName) != 0) {
  28. micropubPost.properties.name[0] = `${podcastName}, ${micropubPost.properties.name[0]}`;
  29. }
  30. micropubPost.properties.featured = [podcastCover];
  31. micropubPost.properties.published = micropubPost.properties.updated;
  32. micropubPost.properties.canonical = micropubPost.properties.url;
  33. delete micropubPost.properties.summary;
  34. delete micropubPost.properties.updated;
  35. delete micropubPost.properties.url;
  36. return micropubPost;
  37. }
  38. function sendEpisodesViaMicropub(episodesToSend) {
  39. episodesToSend.forEach(postData => {
  40. request.post(`${micropubEndpoint}`, {
  41. 'auth': {
  42. 'bearer': `${micropubToken}`
  43. },
  44. body: postData,
  45. json: true
  46. }, (err, data) => {
  47. if (err != undefined) {
  48. console.log(`ERROR: ${err}`);
  49. }
  50. if (data.statusCode !== 201 && data.statusCode !== 202) {
  51. console.log("Podcast Episode Micropub error");
  52. } else {
  53. console.log("Successfully created Micropub request for " + postData.properties.name[0]);
  54. }
  55. });
  56. });
  57. }
27.96 ℉❄️
posted using quill.p3k.io
Please note: This site is in an active redesign. Some things might be a little off 🧐