-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (61 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const cheerio = require('cheerio');
const Article = require('./lib/article');
const ArticleSnippet = require('./lib/article-snippet');
/**
* Extracts news article from page html
* @param {String} html Article page html
* @return {Object} Article object
*/
function parseArticle(html) {
_validateInput(html);
const article = new Article(html);
return {
title: article.getTitle(),
date: article.getDate(),
imageLink: article.getImageLink(),
author: article.getAuthor(),
text: article.getText(),
photos: article.getPhotos(),
attachments: article.getAttachments()
};
}
/**
* Extracts article snippets
* @param {String} html News list page html
* @return {Object[]} Array of article objects
*/
function parseArticleSnippets(html) {
_validateInput(html);
const $ = cheerio.load(html);
const elements = $('#dle-content').find('.well');
return elements.map((index, element) => {
const articleSnippet = new ArticleSnippet($(element).html());
return {
title: articleSnippet.getTitle(),
date: articleSnippet.getDate(),
topic: articleSnippet.getTopic(),
imageLink: articleSnippet.getImageLink(),
description: articleSnippet.getDescription(),
readMoreLink: articleSnippet.getReadMoreLink()
};
}).get();
}
/**
* Validate input html to contain main content section
* @param {String} html Input html
* @throws {Error} Error for empty or invalid html
* @private
*/
function _validateInput(html) {
if (!html || !html.length) {
throw new Error('Empty html supplied');
}
const $ = cheerio.load(html);
if ($('#dle-content').length === 0) {
throw new Error('Invalid html supplied. Cannot find #dle-content');
}
}
module.exports = {
parseArticle,
parseArticleSnippets
};