-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathurl2screen.js
60 lines (51 loc) · 1.57 KB
/
url2screen.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
const fs = require('fs')
const readline = require('readline')
const stream = require('stream')
const screenie = require('node-server-screenshot')
const crypto = require('crypto')
const path = require('path')
const outDir = path.join(__dirname, '/screenshots')
const inFile = './urls.txt'
// parse the urls file and push each url into an array
function load (inFile, cb) {
const outStream = new stream()
const inStream = fs.createReadStream(inFile)
var rl = readline.createInterface(inStream, outStream)
var arr = []
rl.on('line', function (line) {
arr.push(line)
})
rl.on('close', function () {
console.log('done loading file: ', inFile)
cb(arr, outDir)
})
}
// take a screenshot of the urls in the array
function capture (urls, outDir) {
// create the outDir if it doesn't exist
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir)
}
for (var i = 0; i < urls.length; i++) {
const targetUrl = urls[i]
const proto = targetUrl.split(':', 1)[0]
const domainName = targetUrl.split('//', 3)[1]
const outFile = outDir + '/' + proto + '_' + domainName + '.jpg'
screenie.fromURL(targetUrl, outFile, function () {
console.log('saved screenshot of ' + domainName + ' at ' + outFile)
// get the hash of the saved file so we avoid dupes
var fd = fs.createReadStream(outFile)
const hash = crypto.createHash('sha1')
hash.setEncoding('hex')
fd.on('end', function () {
hash.end()
console.log('hash: ', hash.read())
})
fd.pipe(hash)
})
}
}
function main () {
load(inFile, capture)
}
main()