Skip to content

Instantly share code, notes, and snippets.

@mootzville
Created November 25, 2014 12:29
Show Gist options
  • Save mootzville/15af584e626b365d2664 to your computer and use it in GitHub Desktop.
Save mootzville/15af584e626b365d2664 to your computer and use it in GitHub Desktop.
NYSDOS Real Estate License Search (phantomjs)
/*
* Description: New York Dept. of State Real Estate License Search
*
* Usage: phantomjs searchDOS.js 1234567890
*
* Returns a JSON object with the following keys:
* licName - Full Name
* licNum - License Number (aka the number you passed in)
* licType - E.G. Salesperson or Broker
* licStat - Current/Expired
* licExp - License Expiration Date
*/
// Equivalent to creating a tab in a browser
var page = require('webpage').create(),
// This module is necessary to read the command line argument(s)
system = require('system'),
// The page with the search by number form
site = 'https://appext20.dos.ny.gov/nydos/searchByLicNumber.do',
// Get the license number passed in from the command line
licenseNumber = system.args[1];
// The data (body) of the form
formData = 'licNumber=' + licenseNumber + '&search=Search';
// Since we are just mining data we can filter out and abort
// unnecessary requests to speed things up, so here we add
// the event listener to do so.
page.onResourceRequested = function (requestData, request) {
if (
/\.css/.test(requestData.url) === true ||
/\.js/.test(requestData.url) === true ||
/\.jpg/.test(requestData.url) === true ||
/\.png/.test(requestData.url) === true ||
/\.gif/.test(requestData.url) === true
) {
request.abort();
}
};
// Load the page and run the automation. If things go ok, you get the object
// described above back. Otherwise, you get the same object with null values.
page.open(site, 'post', formData, function (status) {
var licItems;
if (status !== 'success') {
licItems = {licName: null, licNum: null, licType: null, licStat: null, licExp: null}
console.log(JSON.stringify(licItems));
phantom.exit();
} else {
var licItems = page.evaluate(function () {
if (document.querySelectorAll('span.item').length === 5) {
var items = document.querySelectorAll('span.item');
var licName = items[0].textContent.trim();
var licNum = items[1].textContent.trim();
var licType = items[2].textContent.trim();
var licStat = items[3].textContent.trim();
var licExp = items[4].textContent.trim();
return {licName: licName, licNum: licNum, licType: licType, licStat: licStat, licExp: licExp};
} else {
return {licName: null, licNum: null, licType: null, licStat: null, licExp: null};
}
});
console.log(JSON.stringify(licItems));
phantom.exit();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment