Created
January 12, 2017 02:33
-
-
Save gmay46/f5aed58fed6e4122d6befe05202bb2dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var fs = require('fs'); | |
var pg= require('pg'); | |
request("https://www.google.com/finance?q=NASDAQ%3AGOOGL&ei=l5B1WKmWENDU2AaEsaSQAg", function(error, response, body) { | |
if(!error) { | |
var $ = cheerio.load(body); | |
$('span.pr').each(function(i, element){ | |
var a = $(this).text(); | |
//console.log(i); | |
//if(i = 1){ | |
var config = { | |
user: 'postgres', //env var: PGUSER | |
database: 'stocks', //env var: PGDATABASE | |
password: '', //env var: PGPASSWORD | |
host: 'localhost', // Server hosting the postgres database | |
port: 5432, //env var: PGPORT | |
max: 10, // max number of clients in the pool | |
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed | |
}; | |
var pool = new pg.Pool(config); | |
pool.connect(function(err, client, done) { | |
if(err) { | |
return console.error('error fetching client from pool', err); | |
} | |
client.query('insert into stock_main.prices values ((select max(price_id)+1 from stock_main.prices),'+a.trim()+',now(),1)', function(err, result) { | |
//call `done()` to release the client back to the pool | |
done(); | |
if(err) { | |
return console.error('error running query', err); | |
} | |
//output: 1 | |
}); | |
}); | |
pool.end(function(err){if (err) throw err;}); | |
}); | |
} | |
//console.log('price above') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment