-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
86 lines (70 loc) · 2.29 KB
/
scraper.js
File metadata and controls
86 lines (70 loc) · 2.29 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
var config = require('./config.js')
, express = require('express')
, jsdom = require('jsdom')
, request = require('request')
, url = require('url')
, Tempo = require('./tempo.js');
// Instanciated module
module.exports = function() {
var app = express.createServer();
app.db = require('./db')();
app.on('close', app.db.close);
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.get('/', function(req, res) {
request({uri: config.scrap_url}, function(err, response, body) {
var self = this;
// Just a basic error check
if (err && response && response.statusCode !== 200) {
console.log('Request error.');
}
// Send the body param as the HTML code we will parse in jsdom
// also tell jsdom to attach jQuery in the scripts and loaded from jQuery.com
jsdom.env({
html: body,
scripts: ['./jquery-1.6.min.js']
}, function(err, window) {
// Use jQuery just as in regular HTML page
var $ = window.jQuery;
var $body = $('body');
// function to save the forecast.
var save = function(day) {
app.db.save(day, function(err, day, is_new) {
if (err) {
//console.log(err);
}
if (true === is_new) {
var twitter = require('./twitter.js')(day);
twitter.post(function(err, result) {
if (err) {
console.log(err);
throw err;
}
});
}
});
}
// create a new Tempo object and scrap brefore saving.
var today = new Tempo();
today.scrap($, $body, 0, save);
// create a new Tempo object and scrap brefore saving.
var tomorrow = new Tempo();
tomorrow.scrap($, $body, 1, save);
res.end('Done');
});
});
});
return app;
}
// Expose dependencies to avoid duplicate modules
exports.express = express;
// Start when main module
if (module.parent == null) module.exports().listen(3000);