-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add youtube feed processing in backend
- Loading branch information
Showing
5 changed files
with
216 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const { logger } = require('../utils/logger'); | ||
const ArticleError = require('./article-error'); | ||
const { ensureDate } = require('../utils/date-validator'); | ||
const { addVideo } = require('../utils/storage'); | ||
const Feed = require('./feed'); | ||
const hash = require('./hash'); | ||
|
||
class Video { | ||
constructor(title, url, guid, datePublished, dateUpdated, feed) { | ||
this.id = hash(guid); | ||
this.title = title; | ||
this.url = url; | ||
this.guid = guid; | ||
this.published = ensureDate(datePublished); | ||
this.updated = ensureDate(dateUpdated, datePublished); | ||
|
||
if (!(feed instanceof Feed)) { | ||
throw new Error(`expected feed to be a Feed Object, got '${feed}'`); | ||
} | ||
this.feed = feed; | ||
} | ||
|
||
/** | ||
* Save the current Video to the database, using the | ||
* associated feed's id to link the Feed with the Video | ||
* | ||
* @returns an empty Promise | ||
*/ | ||
save() { | ||
return addVideo({ | ||
...this, | ||
feed: this.feed.id, | ||
}); | ||
} | ||
|
||
/** | ||
* Create a Video object from an article object. | ||
* If some data is missing, object creation will throw | ||
* an ArticleError exception. | ||
* | ||
* @param {*} article the source article for the video | ||
* @param {*} feed the feed associated to the video | ||
* @returns | ||
*/ | ||
static async createFromArticle(article, feed) { | ||
if (!article) { | ||
throw new Error('unable to read missing article'); | ||
} | ||
|
||
const missing = []; | ||
|
||
if (!article.link) missing.push('link'); | ||
|
||
if (!article.pubDate) missing.push('pubdate'); | ||
|
||
if (!article.id) missing.push('id'); | ||
|
||
if (!article.author) missing.push('author'); | ||
|
||
if (missing.length) { | ||
const message = `invalid youtube article: missing ${missing.join(', ')}`; | ||
logger.debug(message); | ||
throw new ArticleError(message); | ||
} | ||
|
||
// Allow for a missing title, but give it one | ||
if (!article.title) { | ||
logger.debug('article missing title, substituting with "Untitled"'); | ||
article.title = 'Untitled'; | ||
} | ||
|
||
if (!article.date) { | ||
logger.debug('article missing date of last update, substituting publication date'); | ||
article.date = article.pubDate; | ||
} | ||
|
||
const video = new Video( | ||
article.title, | ||
article.link, | ||
article.id, | ||
article.pubDate, | ||
article.date, | ||
feed | ||
); | ||
|
||
await Promise.all([video.save()]); | ||
return video.id; | ||
} | ||
} | ||
|
||
module.exports = Video; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = { | ||
/** | ||
* Makes sure that a given date can be constructed as a Date object | ||
* Returns a constructed Date object, if possible | ||
* Otherwise throws an Error | ||
* @param {Object} date an Object to construct as a Date object | ||
* @param {Date} [fallbackDate] an optional second Date to construct in case the first fails to do so | ||
*/ | ||
ensureDate: function (date, fallbackDate) { | ||
if ( | ||
date && | ||
(Object.prototype.toString.call(date) === '[object String]' || | ||
(Object.prototype.toString.call(date) === '[object Date]' && !Number.isNaN(date))) | ||
) { | ||
return new Date(date); | ||
} | ||
if (Object.prototype.toString.call(fallbackDate) === '[object Date]') { | ||
return new Date(fallbackDate); | ||
} | ||
|
||
throw new Error(`date is invalid: ${date}'`); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters