-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
136 lines (119 loc) · 3.46 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict'
/**
* Url scraper which takes the text input and finds the links/urls, scraps them using cheerio and will
* returns an array of objects where each object contains scrapped webpage's information.
*
* @module url-scraper
* @typicalname urlScraper
* @example
* var urlScraper = require('url-scraper')
*/
var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
var parser = require('npm-text-parser');
exports.scrap = urlScrap;
/**
* @name urlScrap
* @typicalname urlScrap
* @param {string} text - Input text
* @return {object} Object
*
* @usage
* var urlScraper = require('url-scraper')
* var inputString = 'This is awesome it parses the url's dude and http://krishcdbry.com done !'
*
* urlScraper.scrap(inputString).then(function (response) {
* console.log(response);
* });
*
* //{
* // original_text: 'This is awesome it scraps the sites dude and http://heartynote.com done !',
* // parsed_text: 'This is awesome it scraps the sites dude and <a href="http://heartynote.com" target="_blank">http://heartynote.com</a> done !',
* // scraped_data:
* // [
* // { domain: 'heartynote.com',
* // title: 'heartynote welcomes u !!',
* // description: 'Bring your life',
* // thumb: '',
* // url: '',
* // isValid: true,
* // _links: [Object]
* // }
* // ]
* // }
*
*/
function urlScrap(data) {
var urls = parser.getUrls(data);
return new Promise(function(resolve, reject) {
if (urls && urls.length > 0) {
var finalRes = {};
var scrapRes = [];
var sendData = function() {
finalRes['original_text'] = data;
finalRes['parsed_text'] = parser.parseUrl(data);
finalRes['scraped_data'] = scrapRes;
resolve(finalRes);
};
var getUrlData = function(url, callback) {
url = (url.indexOf('://') > 0) ? url : 'http://' + url;
var domain = (url.indexOf("://") > 0) ? url.split("://")[1] : url;
domain = (domain.indexOf('/') > 0) ? domain.split('/')[0] : domain;
var _self = url;
request(url, function(err, res2, html) {
if (err) {
callback({'error': "Error getting url data"});
}
var $ = cheerio.load(html);
if ($) {
var title = $('title').html() ? $('title').html() : '';
var description = $('meta[name=description]') ? $('meta[name=description]').attr('content') : '';
var thumb = null;
if ($('meta[property="og:image"]')) {
thumb = $('meta[property="og:image"]').attr('content')
}
else {
if ($('link[rel="shortcut icon"]')) {
thumb = $('link[rel="shortcut icon"]').attr('href')
}
}
var canonical = $('link[rel=canonical]') ? $('link[rel=canonical]').attr('href') : '';
var scrapObj = {
"domain": domain
, "title": (title != undefined) ? title : ''
, "description": (description != undefined) ? description : ''
, "thumb": (thumb != undefined) ? thumb : ''
, "canonical": (canonical != undefined) ? canonical : ''
, "isValid": true
, "_links": {
"self": _self
}
};
callback(scrapObj);
}
else {
callback(null);
}
});
};
var gettingData = function(url) {
if (url) {
getUrlData(url, function(result) {
if (result) {
scrapRes.push(result);
}
return gettingData(urls.pop());
})
}
else {
return sendData();
}
};
gettingData(urls.pop());
}
else {
reject({});
}
})
};