session-object in external scripts?

Can I get access to the session-object in external scripts, like in the scripts I use in workbench.

I'm trying to find the equivalent of session.scrapeFile(String scrapeableFileIdentifier) in my java program.

But RemoteScrapingSession don't have a method for doing that. Is there a simple way to do it?

Session object in external scripts

If I understand correctly, you are writing some sort of external library that you want to be able to use in your scrapes. In this case, you can get access to the session by including the screen-scraper.jar file and the lib/ directory of screen-scraper in your classpath when building your code. The session object is an instance of com.screenscraper.scraper.ScrapingSession, and you can use a script at the beginning of the scrape to set the session in your code.

For instance, if you are have a class named Helper, you could do something like this:

Your java code

import com.screenscraper.scraper.ScrapingSession;

public class Helper
{
  private ScrapingSession session;
  public Helper(ScrapingSession session)
  {
    this.session = session;
  }
 
  public void foo()
  {
    session.scrapeFile("My ScrapeableFile");
  }
  /* Your code here */
}

The init script that runs before anything else in the scrape

import my.package.Helper;

session.setVariable("_HELPER", new Helper(session));

Another script that uses the helper

import my.package.Helper;

Helper helper = session.getVariable("_HELPER");

// Based on the example Helper class definition, this would simply scrape the "My ScrapeableFile" file.
helper.foo();

You'll also need to make sure you put your jar file in the lib/ext/ directory of screen-scraper and then restart it so it will have access to your code.

I tried this:A simple script

I tried this:

A simple script named 'TestScript' that is run before scrape:

session.setVariable("_SESSION", session);

I tried the run the scrape in workbench just to confirm no errors (no files are actually scraped here, it is just the script that is run):

Starting scraper.
Running scraping session: TestScrapingSession
Processing scripts before scraping session begins.
Processing script: "TestScript"
Processing scripts after scraping session has ended.
Processing scripts always to be run at the end.
Scraping session "TestScrapingSession" finished.

A short program to to test getting the instance of session:

package tomkri.screenscraper;

import com.screenscraper.scraper.RemoteScrapingSession;
import com.screenscraper.scraper.ScrapingSession;

public class Test {
   
    public static void main(String[] args){
       try {
            RemoteScrapingSession remoteSession = new RemoteScrapingSession( "TestScrapingSession" );
            remoteSession.scrape();

            Object object = remoteSession.getVariable("_SESSION");
            System.out.println("Class of object: " + object.getClass());
                       
            ScrapingSession session = (ScrapingSession) object;
           
           
            remoteSession.disconnect();
        } catch (Exception e){
            System.err.println(e);
        }
    }
}

The output when running above code:

run:
Class of object: class java.lang.String
java.lang.ClassCastException: java.lang.String cannot be cast to com.screenscraper.scraper.ScrapingSession
BUILD SUCCESSFUL (total time: 1 second)

getVariable returns a String, so I can't cast it to ScrapingSession.
I have tried variants similiar to what you suggested (with helper class), but the problem remains that only Strings are returned from the getVariable method
in RemoteScrapingSession.

Session Object

In the RemoteScrapingSession you can only get session variables that are Strings, DataRecords, and DataSets. You can view Invoking screen-scraper from Java to view the methods available with the RemoteScrapingSession.

It looks like you want the code that starts the scrape to also have access to the actual session. There is no way to do this, and it wouldn't make sense to create one, since the session is running in a different JVM than screen-scraper. Because of this, there is no guarantee that the classes in the JVM you use to invoke the RemoteScrapingSession are the same version as the ones screen-scraper uses. It is also likely that the two JVMs don't have the same classes available to them, as their classpaths will most likely differ.

If you are simply writing a library for screen-scraper to invoke in your scrapes (you start the scrape from screen-scraper, rather than in your own code), I can post an example class and scraping session that uses it.

Thanks

It was the different JVM's issue I didn't see.

Thing is I wanted to control most of the scraping process from my external code. Solution was simply to interact with screen-scraper in server mode through setting session variables. Needed to write only 2 scripts with 5 lines of code in workbench mode.

Was just a newbie thing. Problem solved.