-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (76 loc) · 2.97 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
const axios = require("axios")
const cheerio = require('cheerio');
const canvas = require('./downloadImage');
var fs = require('fs');
const path = require("path");
const main = async () =>{
try {
const folderName = path.join(__dirname+'/data.json')
const news = await axios.get('https://www.ndtv.com/world-news#pfrom=home-ndtv_mainnavgation');
const node = String(news.data)
const $ = cheerio.load(node)
const worldNewsLinksSet = new Set();
$('a').each( (index, value) => {
let link = $(value).attr('href');
if(String(link).includes('https://www.ndtv.com/world-news/') && !(String(link).includes('page-'))){
worldNewsLinksSet.add(link)
}
});
const worldNewsLinks = [...worldNewsLinksSet];
const data = worldNewsLinks.map(async v => {
return await axios.get(v)
})
let newstitle = []
await Promise.all(data).then((values) => {
let data = values.map(v=> {
return {data: v.data, url: v.config.url}
});
data = data.map(c=>{
const dom = cheerio.load(c.data);
let tag = dom("meta[name='keywords']").attr("content")
tag = tag.split(',')
dom('script,i, .externalVideo, .jiosaavn-widget, meta, #jiosaavn-widget').remove()
let title = dom('.sp-ttl-wrp h1').contents().first().text()
title = String(title).trim().replace(/['\"]+/g, '');
const summry = dom('.sp-descp').contents().first().text()
const description = dom('#ins_storybody').contents().text();
const image = dom('#story_image_main').attr('src');
const checkduplicate = newstitle.filter(v=> v.title == title);
if(!checkduplicate.length){
newstitle.push({
url:String(c.url).trim(),
title,
summry,
description,
image,
tag
})
}
})
})
const imageprocess = newstitle.map(async (v,i)=>{
const d = await canvas.downLoadImage(v, i);
return d
})
const foldersData = []
Promise.all(imageprocess).then(src=>{
foldersData.push(...src);
}).then(()=>{
const data = {
newstitle,
foldersData
}
var jsonContent = JSON.stringify(data);
fs.writeFile('data.json', jsonContent, 'utf8', (err) => {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
console.log("JSON file has been saved.");
})
})
} catch (error) {
console.log('error', error)
}
}
main()