Skip to content

Instantly share code, notes, and snippets.

@etoxin
Created October 21, 2014 10:33
Show Gist options
  • Save etoxin/34b8e49470fe7277554e to your computer and use it in GitHub Desktop.
Save etoxin/34b8e49470fe7277554e to your computer and use it in GitHub Desktop.
ArdunioTempSetter CouchDB
var express = require('express');
var http = require('http');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var nodeCouchDB = require("node-couchdb");
var couch = new nodeCouchDB("192.168.0.12", 5984);
var app = express();
var url = 'http://192.168.0.111/';
console.log('---------------------\n| TempHum Started. |\n---------------------');
function getTempHum() {
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var guid = (function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return function() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
})();
var json = {
temperature: "",
humidity: ""
}
var humidity = $('h1:first-child').text();
var hum = humidity.replace(/Humidity: /,"").replace(/%/,"").replace(/\s/,"");
var temp = $('h1 + h1').text();
var temperature = temp.replace(/Temperature: /,"").replace(/°C/,"");
var uid = guid();
var now = new Date();
console.log('---');
console.log('Temperature: ', temperature);
console.log('Humidity: ', hum);
console.log('Date: ', now);
console.log('Guid: ', uid);
couch.insert("weather", {
_id: uid,
date: now,
temperature: temperature,
humidity: hum
}, function (err, resData) {
if (err)
return console.error(err);
console.dir(resData)
});
}
});
}
// 10 mins
setInterval(getTempHum, 3600000)
//getTempHum();
exports = module.exports = app;
{
"name": "node-web-scrape",
"version": "0.0.1",
"description": "Scrape le web.",
"main": "server.js",
"author": "Scotch",
"dependencies": {
"express": "latest",
"request": "latest",
"cheerio": "latest",
"node-couchdb": "~0.3.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment