Skip to content

Instantly share code, notes, and snippets.

@ardacetinkaya
Last active January 21, 2016 21:19
Show Gist options
  • Save ardacetinkaya/2254557efb077799bf99 to your computer and use it in GitHub Desktop.
Save ardacetinkaya/2254557efb077799bf99 to your computer and use it in GitHub Desktop.
Blog'da yer verdiğim küçük bir yazıda anlattığım, node.js için ile yapılmış, basit bir web sitesi parse etmek ve sonra e-mail atmak gerekli olan script örneği... Ayrıntılarına http://www.minepla.net/2016/01/lego-azure-webjobs-nodejs/ yazısından bakabilirsiniz.
/* global $ */
/* global callback */
var http = require('http');
var cheerio = require('cheerio');
var sendgrid = require('sendgrid')([SEND_GRID_KEY]);
var azure = require('azure-storage');
function LegoChecker() {
this.Sender = '[SENDER_EMAIL]';
this.Receivers = '[RECEIVER_EMAIL]';
}
LegoChecker.prototype.checkCurrent = function (domain, page, callback) {
var options = {
host: domain,
path: '/' + page
};
var _productInfo = "";
http.request(options, function (response) {
var html = '';
response.on('data', function (chunk) {
html += chunk;
});
response.on('end', function () {
$ = cheerio.load(html);
$("span[class='rtIn NamedListTreeNodeHdr']").each(function () {
if ($(this).text().indexOf("LEGO") > -1) {
console.log("There are some new items for Lego ->" + $(this).text());
_productInfo = $(this).text();
}
});
if (callback && typeof (callback) === "function") {
callback(_productInfo);
}
});
}).end();
}
//********Start********//
var check = new LegoChecker();
check.checkCurrent("www.adoreoyuncak.com", "NewProducts.aspx", function (current) {
var tableService = azure.createTableService([ACCOUNT], [KEY]);
tableService.createTableIfNotExists('Nody', function (error, result, response) {
if (!error) {
tableService.retrieveEntity('Nody', 'lego', '1', function (error, result, response) {
if (!error) {
console.log("Stored value -->" + result.description._);
console.log("Current value-->" + current);
if (result.description._ != current) {
//This means the number of current lego is changed
//So update the storage and then send an e-mail
var entGen = azure.TableUtilities.entityGenerator;
var legoInfo = {
PartitionKey: entGen.String('lego'),
RowKey: entGen.String('1'),
description: entGen.String(current)
};
tableService.insertOrReplaceEntity('Nody', legoInfo, function (error, result, response) {
if (!error) {
console.log("Lego info is updated.");
} else {
console.log("[ERROR] - An error occured in Inserting Entity");
console.log(error);
}
});
var email = new sendgrid.Email({
to: check.Sender,
from: check.Receivers,
subject: 'New Lego',
text: 'Hello;\r\n It seems that there are new Lego.('+ current+') \r\nCheck them out! - http://www.adoreoyuncak.com/NewProducts.aspx '
});
sendgrid.send(email, function (err, json) {
if (err) {
console.error(err);
}
console.log(json);
});
} else {
console.log("No new lego.");
}
} else {
console.log("[ERROR] - An error occured in Retriving Entity");
console.log(error);
}
});
} else {
console.log("[ERROR] - An error occured in Table Service");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment