Homemade Automatic Domain Tester with PhantomJS Part 1

Reason Why

I had an idea to write a program that automatically checked a list of websites to make sure they were still running. The idea is that I could later modify this program to contact me if a site was down.

Tools within My Reach

A Little Background

I started this adventure off using Selenium Webdriver written in Ruby and running on the Raspberry Pi. This combonation worked, however it was a little sluggish on the Pi. I needed something a little lighter and faster!

PhantomJS

If you haven't played around with PhantonJS yet, I recommend you do. They have a really great 'quick start' section to show you the power of PhantomJS.

Installing PhantomJS

If you don't have PhantomJS installed go ahead and get the latest version.

The First Version

Below I've included the first version of my PhantomJS Domain Tester. This version visits each site that you put inside the 'arrayProdsites' array, observes redirects and reports back the status of the site. (success or failed)

var webPage = require('webpage');
var array_position = 0;
var successes = 0;
var failures = 0;
var scheme = 'http://www.';
var arrayProdsites = [
    "bitpi.co",
    "reddit.com/r/raspberry_pi",
    "news.ycombinator.com",
    "yetichute.com"
    ];

function open_website(url){
    
    // Open website
    var action = '\x1b[33mRedirected\x1b[0m: ';
    if (!url) {
        url = scheme + arrayProdsites[array_position] + '/';
        action = 'Opening: ';
    }
    
    var page = webPage.create();
    console.log(action + url);
    page.onError = function (msg, trace) {
      console.log(msg);
    };
    page.onNavigationRequested = function(newurl, type, willNavigate, main) {
      if (    
            main && 
        url != newurl &&
            (type=="Other" || type=="Undefined") //  type = not by click/submit etc
        ) {
            page.close();
            open_website(newurl); // reload on new page
        }    
    }
    page.open(url, function(status){
        // Render
        if(status === "success") {
            console.log(" Status: \x1b[32m" + status + '\x1b[0m');
            console.log('');
            successes++;
            //page.render(array_position + '_example.png');
        } else {
            console.log(' Status: \x1b[31mFailed\x1b[0m');
            console.log('');
            failures++;
        }

        // Increment
        array_position++;
        page.close();

        // Go to the next or exit
        if(arrayProdsites.length !== array_position){
            open_website();
        } else {
            console.log(successes + ' successes, ' + failures + ' failures');
            phantom.exit();
        }
    });
}

open_website();

How to Run

After you copy this code to a .js file on your system, go ahead and run the following command...

phantomjs yourprogramname.js

Shortly after you should see the status of each domain test.

Possible next steps

I might want to hook this up to a cron job so that it will fire off automatically based on a schedule. That being said, I may also want to create some way for it to report its suspected failures.

Try It Yourself!

Feel free to modify and improve this code to your desire. The code can also be found here.

Check out Part 2 of the Domain Tester!