-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.js
183 lines (176 loc) · 6.5 KB
/
scrape.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const axios = require('axios')
const cheerio = require('cheerio')
const jsdom = require('jsdom')
const { JSDOM } = jsdom
const util = require('util')
let result
const url = 'https://fridaysforfuture.de/streiktermine/'
const regioUrl = 'https://fridaysforfuture.de/regionalgruppen/'
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function removeHyphen(str) {
return str.replace(/\u00AD/g,'');
}
module.exports = {
async crunchDate () {
await axios.get(url)
.then((response) => {
if (response.status === 200) {
const html = response.data
const $ = cheerio.load(html)
result = $('.wp-block-table').prevAll('h2').html()
}
}, (err) => console.log(err))
return result
},
async crunchListAll () {
await axios.get(url)
.then((response) => {
if (response.status === 200) {
const html = response.data
const $ = cheerio.load(html)
result = $('.wp-block-table > tbody > tr > td').map(function (i, el) {return $(this).text().replace(/\u00A0/g, ' ')}).get()
}
}, (err) => console.log(err))
let results = []
result.forEach((value) => {
let splitted = value.split(', ')
if (splitted[2] !== undefined && splitted[1] !== undefined && splitted[0] !== undefined && splitted && value) results.push({
city: removeHyphen(splitted[0]),
time: splitted[1],
place: removeHyphen(splitted[2].trim())
})
})
return results
},
async crunchList () {
await axios.get(url)
.then((response) => {
if (response.status === 200) {
const html = response.data
const $ = cheerio.load(html)
result = $('.wp-block-table').eq(0).children().children().map(function (i, el) {return $(this).text().replace(/\u00A0/g, ' ')}).get()
}
}, (err) => console.log(err))
let results = []
result.forEach(value => {
let toSplit = value.toString()
let splitted = toSplit.split(', ')
if (splitted[2] !== undefined && splitted[1] !== undefined && splitted[0] !== undefined && splitted && value) {
results.push({
city: removeHyphen(splitted[0]),
time: splitted[1],
place: removeHyphen(splitted[2].trim())
})
}
})
return results
},
async crunchListSecond () {
await axios.get(url)
.then(response => {
if (response.status === 200) {
const html = response.data
const $ = cheerio.load(html)
result = $('.wp-block-table').eq(1).children().children().map(function (i, el) {return $(this).text().replace(/\u00A0/g, ' ')}).get()
}
}, err => console.log(err))
let results = []
result.forEach(value => {
let splitted = value.split(', ')
if (splitted[2] !== undefined && splitted[1] !== undefined && splitted[0] !== undefined && splitted && value) results.push({
city: removeHyphen(splitted[0]),
time: splitted[1],
place: removeHyphen(splitted[2].trim())
})
})
return results
},
crunchRegioList: async function () {
await axios.get(regioUrl)
.then(response => {
if (response.status === 200) {
const html = response.data
const $ = cheerio.load(html)
result = $('div .su-accordion').eq(0).children('div .su-spoiler').children('div .su-spoiler-content').children('ul').children('li').map(function (i, el) {return $(this).html()}).get()
}
}, err => console.log(err))
let results = []
result.forEach(value => {
let split = value.split(': ')
console.log(value)
let linksArray = []
const dom = new JSDOM('<!doctype html><body>' + split[0],
'text/html')
const groupName = dom.window.document.body.textContent
if (split[1]) {
let links = split[1].split(' | ')
links.forEach(value => {
const domLinks = new JSDOM(
'<!doctype html><body>' + value,
'text/html')
if (domLinks.window.document.body.querySelector('a') !== null) {
let chatLink = domLinks.window.document.body.querySelector('a').getAttribute('href')
const text = removeHyphen(domLinks.window.document.body.querySelector('a').textContent)
let chatType
switch (true) {
case text === 'WhatsApp':
chatType = 'whatsapp'
break
case text === 'Telegram':
chatType = 'telegram'
break
case validateEmail(removeHyphen(text)):
chatType = 'email'
break
case text === 'Twitter':
chatType = 'twitter'
break
case text === 'Website':
chatType = 'website'
break
case text === 'Facebook':
chatType = 'facebook'
break
case text === 'Instagram':
chatType = 'instagram'
break
default:
chatType = text
break
}
chatLink = chatLink.replace(/\r?\n|\r/, '')
if (chatType !== undefined) {
linksArray.push(JSON.parse(`{ "type": "${chatType}", "link": "${chatLink}" }`))
}
}
})
const jsonString = `{ "groupName": "${removeHyphen(groupName)}", "groupLinks": ${JSON.stringify(linksArray)} }`
const json = JSON.parse(jsonString)
if (split[0] !== undefined && split[0] !== 'Deutschland' && split[0] !== 'Diskussionen') results.push(json)
}
})
return results
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// async function crunchList () {
// await axios.get(regioUrl)
// .then(response => {
// if (response.status === 200) {
// const html = response.data
// const $ = cheerio.load(html)
// result = $('.su-accordion').eq(0).children().children().children().children().map(function (i, el) {return $(this).text()}).get()
// }
// }, err => console.log(err))
// let results = []
// result.forEach(value => {
// let splitted = value.split(': ')
// if (splitted[0] !== undefined) results.push(`{ group: ${splitted[0]} }`)
// })
// return results
// }
//
// crunchList().then(data => console.log(data))