-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (55 loc) · 1.94 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
// For fetching map info pages
import { RequestQueue, CheerioCrawler } from 'crawlee';
// for zip file downloads
import axios from 'axios';
import * as path from 'path';
import * as fs from 'fs'
// download function credit: https://gist.github.com/senthilmpro/04ea6c086bcb592beb8083214a630548
function downloadFile(reqUrl, fileName){
axios({
method: "GET",
url: reqUrl,
responseType: "stream"
}).then(res => {
if (res.status == 200) {
fileName = fileName || reqUrl.split("/").pop();
const dir = path.resolve("downloads/" + fileName);
console.log(dir)
res.data.pipe(fs.createWriteStream(dir));
res.data.on("end", () => {
console.log("download completed");
});
} else {
console.log(`ERROR >> ${res.status}`);
}
}).catch(err => {
console.log("Error ",err);
});
}
let entry_url = `https://www.ubisoft.com/en-us/game/rainbow-six/siege/game-info/maps`
const requestQueue = await RequestQueue.open();
await requestQueue.addRequest({ url: entry_url });
let crawl_root = true
const crawler = new CheerioCrawler({
requestQueue,
async requestHandler({ $, request }) {
if (crawl_root){
crawl_root=false
let maps = $('a').map(function(i,el){
if (el.attribs.href.includes(`/en-us/game/rainbow-six/siege/game-info/maps/`)){
let map_url = `https://www.ubisoft.com${el.attribs.href}`
requestQueue.addRequest({ url: map_url})
}
})
}
else{
let blueprint_zip = $('a').map(async function(i,el){
if (el.attribs.href.includes(`.zip`)){
let download_url = el.attribs.href
downloadFile(download_url, download_url.split("/").pop())
}
})
}
}
})
await crawler.run();