Event Handler

The EventCallback method of the session provides many ways for you to attach listeners to various parts of your scrape enabling you to have even greater control as to what happens and when it comes to your scrapes.

Listed below are some examples on how to make use of this powerful class in addition you can check out the Session Profiler in the debugging section of the script repository to find more examples of using the Event Handler.

See Also

Example Resource Closer

// One use of the events is to setup closing resources from the same script
// in which they were initialized

import com.screenscraper.events.*;
import com.screenscraper.events.session.*;

CsvWriter writer = new CsvWriter("output/my_csv_file.csv");
writer.setHeader(new String[]{"NAME", "ADDRESS"});

session.setVariable("_WRITER", writer);

// Setup a call to close the writer when the scrape ends (called regardless
// of whether the scrape was stopped mid run or completed normally)
EventHandler handler = new EventHandler()
{
    public String getHandlerName()
    {
        return "Close resources";
    }

    public Object handleEvent(EventFireTime fireTime, SessionEventData data)
    {
        // Note that in the interpreter, directly referencing variables that
        // were set external to the script (ie session, scrapeableFile, etc...)
        // will cause an error.  If they are needed, get them from the data object
        data.getSession().logInfo("Closing resources...");
        try
        {
            writer.close();
        }
        catch(Exception e)
        {
            // Do nothing
        }
        return data.getLastReturnValue();
    }
};

// Set the event to be fired at a specific time
session.addEventCallback(SessionEventFireTime.AfterEndScripts, handler);

General Use

// Import the general event handler classes
import com.screenscraper.events.*;

// Import the classes dealing with the events times you want to use
import com.screenscraper.events.session.*;
import com.screenscraper.events.scrapeablefile.*;
import com.screenscraper.events.script.*;
import com.screenscraper.events.extractor.*;
// Misc is random stuff that can be called from multiple locations
// and therefore didn't fit elsewhere
import com.screenscraper.events.misc.*;

// Create an EventHandler object which will be called when the event triggers
EventHandler handler = new EventHandler()
{
    /**
     * Returns the name of the handler.  This method doens't need to be implemented
     * but helps with debugging (on error executing the callback it will output this)
     */

    public String getHandlerName()
    {
        return "A test event handler";
    }

    /**
     * Processes the event, and potentially returns a useful value modifying something
     * in the internal code
     *
     * @param fireTime The fire time of the event. This helps when using the same handler
     * for multiple event times, to determine which was called
     * @param data The actual data from the event. Based on the event time this
     * will be a different type. It could be SessionEventData, ScrapeableFileEventData,
     * ScriptEventData, StringEventData, etc...  It will match the fire time class name
     *
     * @return A value indicating how to proceed (or sometimes the value is ignored)
     */

    public Object handleEvent(EventFireTime fireTime, SessionEventData data)
    {
        // While you can specifically grab any data from the data object,
        // if this is a method that has a return value that matters,
        // it's best to get it as the last return value, so that multiple
        // events can be chained together.  The input data object
        // will always have the original values for all the other getters
        Object returnValue = data.getLastReturnValue();

        // Do stuff...

        // The EventFireTime values describe in the documentation what the return
        // value will do, or says nothing about it if the value is ignored
        // If you don't intend to modify the return, always return data.getLastReturnValue();
        return returnValue;
    }
};

// Set the event to be fired at a specific time
session.addEventCallback(SessionEventFireTime.AfterEndScripts, handler);

Modify Last Response

    // With events, we can modify the Last Response before extractors are run
    // on it.  Perhaps we care about line breaks

    import com.screenscraper.events.*;
    import com.screenscraper.events.scrapeablefile.*;
    import com.screenscraper.scraper.*;

    // Setup a call on scrapeable files named "Details" to insert a
    // <br /> tag everywhere there is a line break character
    EventHandler handler = new EventHandler(){
        public String getHandlerName()
        {
            return "Update last response with line breaks";
        }

        public Object handleEvent(EventFireTime fireTime, ScrapeableFileEventData data)
        {
            ScrapingSession session = data.getSession();
            session.logInfo("Swapping out line breaks in the response for <br /> tags");

            // We know for the event time this will trigger that this is a String
            // and is the response data (post tidy) from the server
            String response = data.getLastReturnValue();

            response = response.replaceAll("(?s)\r?\n", "<br />\n");

            // Return the new value, which will be used as the last response
            return response;
        }
    };

    // Set the event to be fired at a specific time.  The documentation
    // (as seen in the completion popup) tells us for this fire time
    // the return value will be used as the response data
    session.addEventCallback(ScrapeableFileEventFireTime.AfterHttpRequest, handler);

    session.scrapeFile("Manually Scraped");