-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikiloc.js
More file actions
222 lines (178 loc) · 5.81 KB
/
wikiloc.js
File metadata and controls
222 lines (178 loc) · 5.81 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const puppeteer = require("puppeteer");
const cheerio = require("cheerio");
const sample = {
guests: 1,
bedrooms: 1,
beds: 1,
baths: 1,
pesosPerNight: 350,
};
let browser;
async function openSpecificTrail(url, page) {
await page.goto(url, { waitUntil: "networkidle2" });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
//console.log("url:", url);
const trailDetails = await $("#trail-data > div > h4").text();
const trailDetails2 = await $(
"#trail-data > div.data-items.clearfix > div > a > span"
).text();
const trailDetail3 = await $(
"#primary > div.description.dont-break-out > p"
).text();
const trailDetail4 = await $(
"#primary > div.description.dont-break-out"
).text();
const wayPoints = await $("[class='simplecard__breadcrumb__title']").text();
const map = await $("#map-canvas");
const elevation = await $("#elevation-profile-svg > polyline:nth-child(2)");
const data = {
info: trailDetails,
stats: trailDetails2,
more: trailDetail3,
description: trailDetail4,
wayPoints: wayPoints,
map: map,
elevation: elevation,
};
return data;
}
async function openCityAndScrape(url, page) {
await page.goto(url, { waitUntil: "networkidle2" });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
}
async function openCountryAndScrape(url, page) {
await page.goto(url, { waitUntil: "networkidle2" });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const cityList = await $("[href]")
.map((i, element) => {
const hrefs = $(element).attr("href") + "'";
const hrefsMatch = hrefs.match(/trails\/outdoor(.*?)\'/);
arr = hrefsMatch;
return hrefsMatch;
})
.get();
var clists = cityList.filter(function (element, index, array) {
return index % 2 === 0;
});
var cities = clists.map((place) => place.slice(0, -1));
cities.reverse();
cities.shift();
var citiesFinal = cities.map((x) => "https://www.wikiloc.com/" + x);
return citiesFinal;
}
async function openActivityAndScrape(url, page) {
await page.goto(url, { waitUntil: "networkidle2" });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const trailsOfActivity = await $("#trails > ul > li > div > div > h3 > a")
.map((i, element) => {
const hrefs = $(element).attr("href");
return hrefs;
})
.get();
return trailsOfActivity;
}
async function getListOfTrails(
url,
page,
activityPage,
specificTrailPage,
countryPage,
cityPage,
cityTrailPage
) {
try {
await page.goto(url, { waitUntil: "networkidle2" });
const html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
const allList = await $("[href]")
.map((i, element) => {
const hrefs = $(element).attr("href") + "'";
const hrefsMatch = hrefs.match(/trails\/(.*?)\'/);
arr = hrefsMatch;
return hrefsMatch;
})
.get();
const countryList = await $("[href]")
.map((i, element) => {
const hrefs = $(element).attr("href") + "'";
const hrefsMatch = hrefs.match(/trails\/outdoor(.*?)\'/);
arr = hrefsMatch;
return hrefsMatch;
})
.get();
var lists = allList.filter(function (element, index, array) {
return index % 2 === 0;
});
var all = lists.map((place) => place.slice(0, -1));
var countrylists = countryList.filter(function (element, index, array) {
return index % 2 === 0;
});
var countries = countrylists.map((place) => place.slice(0, -1));
//All list - country list = activity list
var activities = all.filter((x) => !countries.includes(x));
var activitiesFinal = activities.map((x) => "https://www.wikiloc.com/" + x);
var countriesFinal = countries.map((x) => "https://www.wikiloc.com/" + x);
//console.log(activitiesFinal);
//Browse by activities.
for (var i = 0; i < activitiesFinal.length; i++) {
const trails = await openActivityAndScrape(
activitiesFinal[i],
activityPage
);
for (var j = 0; j < trails.length; j++) {
const specificTrail = await openSpecificTrail(
trails[j],
specificTrailPage
);
const trailData = {
activity: activitiesFinal[i],
trail: trails[j],
Data: specificTrail,
};
await console.log(trailData);
}
}
//Browse by countries.
for (var x = 0; x < countriesFinal.length; x++) {
const cities = await openCountryAndScrape(countriesFinal[x], countryPage);
//console.log("cities:", cities);
for (var y = 0; y < cities.length; y++) {
const citytrails = await openActivityAndScrape(cities[y], cityPage);
//console.log("city", cities[y], "city trails", citytrails);
for (var z = 0; z < citytrails.length; z++) {
const specificCityTrail = await openSpecificTrail(
citytrails[z],
cityTrailPage
);
console.log("specific city trail: ", specificCityTrail);
}
}
}
} catch (error) {
console.error(error);
}
}
async function main() {
browser = await puppeteer.launch({ headless: false });
const trailsUrl = "https://www.wikiloc.com/trails";
const trailsPage = await browser.newPage();
const activitiesPage = await browser.newPage();
const specificTrailPage = await browser.newPage();
const countryPage = await browser.newPage();
const cityPage = await browser.newPage();
const specificCityTrailPage = await browser.newPage();
await getListOfTrails(
trailsUrl,
trailsPage,
activitiesPage,
specificTrailPage,
countryPage,
cityPage,
specificCityTrailPage
);
}
main();