-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (81 loc) · 3.31 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
const express = require('express');
const path = require('path');
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');
const PORT = process.env.PORT || 5000
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/display', (req, res) => {
console.log("----------------------------START---------------------------------------");
res.locals.type = req.query.type;
var urll = req.query.enteredUrl;
axios.get(urll)
.then(response => {
let getData = html => {
const $ = cheerio.load(html);
//image stuff
imageSrc = [];
pageName = [];
$('img.ProfileAvatar-image').each((i, elem) => {//$('p.TweetTextSize').each((i, elem) => {
imageSrc.push({
picture: $(elem).attr('src')
});
pageName.push({
name: $(elem).attr('alt')
});
});
var picStr = JSON.stringify(imageSrc);
var jsonPicStr = "{\"picture\":" + picStr + "}"; //format the string
var picObj = JSON.parse(jsonPicStr); //turn it to json obj
var nameStr = JSON.stringify(pageName);
var jsonNameStr = "{\"name\":" + nameStr + "}"; //format the string
var nameObj = JSON.parse(jsonNameStr); //turn it to json obj
//DATE STUFF
dateData = [];
$('a.tweet-timestamp').each((i, elem) => {//$('p.TweetTextSize').each((i, elem) => {
dateData.push({
date: $(elem).text()
});
});
var dateStr = JSON.stringify(dateData);
var jsonDateStr = "{\"date\":" + dateStr + "}"; //format the string
var dateObj = JSON.parse(jsonDateStr); //turn it to json obj
// tweet data
data = [];
$('div.js-tweet-text-container').each((i, elem) => {//$('p.TweetTextSize').each((i, elem) => {
data.push({
text: $(elem).text()
});
});
fs.writeFile('tweetData.json',
JSON.stringify(data),
(err)=> console.log('File successfully written!'))
var str = JSON.stringify(data);
var jsonStr = "{\"tweet\":" + str + "}"; //format the string
var obj = JSON.parse(jsonStr); //turn it to json obj
var tweets = []
for(var i in obj.tweet) {
//console.log(obj.tweet[i].text); //print out the tweets
tweets.push(dateObj.date[i].date);
tweets.push(obj.tweet[i].text);
}
var params = {urll: urll, html: tweets, picture: picObj.picture[0].picture, name: nameObj.name[0].name};
res.render('pages/display', params);
}
getData(response.data);
})
.catch(error => {
console.log(error);
var params = {urll: urll, html: 'Error'};
res.render('pages/display', params);
})
//https://blog.bitsrc.io/https-blog-bitsrc-io-how-to-perform-web-scraping-using-node-js-5a96203cb7cba
//https://dev.to/aurelkurtula/introduction-to-web-scraping-with-nodejs-9h2
})
.get('/', (req, res) => {
res.render('pages/home')
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`))