-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (59 loc) · 2.03 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
67
68
69
70
71
72
const http = require('http')
const path = require('path')
const phantom = require('phantomjs-prebuilt')
const { spawn } = require('child_process')
const winston = require('winston')
const port = 3000
var fs = require('fs')
var request = require('request')
var cron = require('node-cron')
const requestHandler = (request, response) => {
winston.log('info', 'node-app', { log: request.url })
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return winston.log('error', 'node-app', {error: `${err}`})
}
return winston.log('info', 'node-app', {info: `server is listening on ${port}`})
})
function parse_events() {
var contents = fs.readFileSync('./event-list.json', function(err) {
winston.log('error', 'node-app', {stderr: `${err}`})
})
var event_list = JSON.parse(contents)
var image_dir = "./client/public/assets/img/"
var download = function(uri, filename, callback) {
request.head(uri, function(err, res, body) {
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback)
})
}
if (!fs.existsSync(image_dir)) {
fs.mkdirSync(image_dir)
}
for (var i = 0; i < event_list.events.length; i++) {
download(event_list.events[i].img_url, image_dir + i + '.jpg', function() {})
}
}
function phantom_web_scrape(callback) {
var p = spawn(phantom.path, ['./phantom-job.js']);
winston.log('info', 'node-app', {info: `${phantom.path}` })
p.stdout.setEncoding('utf8')
p.stderr.setEncoding('utf8')
p.stdout.on('data', (data) => {
winston.log('info', 'phantom-js', { stdout: `${data}` })
})
p.stderr.on('data', (data) => {
winston.log('error', 'phantom-js', { stderr: `${data}` })
})
p.on('error', (data) => {
winston.log('error', 'spawn', { child_process_error: `${data}` })
})
p.on('close', (message) => {
winston.log('close', 'spawn', { close_message: `${message}` })
callback()
})
}
phantom_web_scrape(parse_events)
// Currently set to scrape once every minute
//cron.schedule('* * * * *', phantom_web_scrape(parse_events))