forked from loon3/pepechain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_pepe_directory.js
93 lines (68 loc) · 2.2 KB
/
scrape_pepe_directory.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
const http = require('http')
const fs = require('fs')
const url = require('url')
const path = require('path')
var options = {
host: 'chiguireitor.com',
path: '/pepelist.json'
};
if (!fs.existsSync("./pepe_directory")){
fs.mkdirSync("./pepe_directory");
}
if (!fs.existsSync("./pepe_images")){
fs.mkdirSync("./pepe_images");
}
callback = function(response) {
var str = ''
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk
})
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
fs.writeFileSync('./pepe_directory/pepelist.json', str)
var ob = JSON.parse(str)
for (x in ob) {
download(x, ob[x])
}
})
}
http.request(options, callback).end()
var downloadQueue = []
var reqsStarted = false
function download(name, url) {
downloadQueue.push({name, url})
if (!reqsStarted) {
fetchOne()
reqsStarted = true
}
}
function fetchOne() {
setTimeout(function() {
if (downloadQueue.length > 0) {
var curDl = downloadQueue.splice(0,1)[0]
var udl = url.parse(curDl.url)
var imagePath = "./pepe_images/"+(curDl.name + path.extname(udl.path))
fs.access(imagePath, fs.F_OK, function(err) {
if (!err) {
// Image exists
console.log("> " + curDl.name + path.extname(udl.path) + " exists!")
fetchOne()
} else {
// Image doesn't exist
console.log("> Downloading '" + curDl.name + path.extname(udl.path))
http.request(udl, function(response) {
var data = new Buffer(0)
response.on('data', function (chunk) {
data = Buffer.concat([data, chunk])
})
response.on('end', function () {
fs.writeFileSync('./pepe_images/' + curDl.name + path.extname(udl.path), data)
fetchOne()
})
}).end()
}
});
}
}, 100)
}