Source: site.view [edit]
Function name: getMovieStruct
Arguments: city
Description:
Page type: webl
Render function:  
Module: sandbox

Page source:

// File: GoogleMovies.webl
// 
// Script to extract weather info from www.weather.com
//
// (c) 1998-2005 Copyright SRI International.



//**************************************************************************
// Print flags
//**************************************************************************
var printInfoLevel = 15;

//**************************************************************************
// Global variables
//**************************************************************************
var googleMovieSearchUrl = "http://www.google.com/movies";

// ***************** Utility functions *********************
//**************************************************************************
// Name : PrintInfo
//**************************************************************************
var PrintInfo = fun(message,level)
  if (level <= printInfoLevel) then 
    PrintLn("Google Movies Info : " + message); 
  end;
end;

//************************************************************************
// Name : LoadPage
// Input : A filename
// Output : A page
// Purpose : Load a page from a file
//************************************************************************
var LoadPage = fun(inFileName)
   return WubCall("testGoogleMovies", []);
   //return GetURL(googleMovieSearchUrl
end;

//************************************************************************
// Name : getTableByKeyWord
// Purpose : Returns the first table of a page that
//           contains a given keyword
// Input : The keyword (string)
// Input : The page (Page)
// Output : A table
// Courtesy : H. Marais
//************************************************************************

var getTableByKeyWord = fun (inPage, inWord)
  var table = Elem(inPage, "table") contain Pat(inPage, inWord);
  if (Size(table)>0) then
    table = (table !contain table)[0];
  end;
end;

//************************************************************************
// Name : PickCell
// Purpose : Function to extract the contents of a particular (row, col) of a table.
// Input : The page containing the table
// Input : The table
// Input : row, col the coordinates of the cell
// Output : An object
// Courtesy : H. Marais
//************************************************************************
// 
var PickCell = fun(page, table, row, col)
    var rows = Elem(table, "tr");
    var cols = (Elem(page, "th") + Elem(page, "td")) inside rows[row];
    cols[col]
end;

//************************************************************************
// Name : PrintTable
// Purpose : Debug: print contents of a table, by row and column
// Input : The table
//************************************************************************
var PrintTable = fun(table)
   var r = 0;
   every row in Elem(table, "tr") do
      var c = 0;
      every col in Elem(row, "td") do
         //Print Ln(r, " ", c, " ", Text(col));
         c = c + 1
      end;
      r = r + 1
   end
end;

//************************************************************************
// Name : trimString
// Input : A string to be trimmed
// Output : The trimmed string
// Purpose : Removes all white spaces (keeps only words)
//************************************************************************
var trimString = fun (inString) 
  var listOfWords = Str_Search(inString, `\S+`);
  var result = "";
  every word in listOfWords do
    result = result+word[0]+" ";
  end;
  return result;
end;

//var getWebServiceEngine = fun()
//  return Java Class("com.sri.active.service.webl.WebLWrapper");
//end;

//**************************************************************************
// Name : Main functions
//**************************************************************************

var getMoviesFromPiece = fun (inMoviesPiece)

  var result = [. .];

  var links = Select (
     Elem(inMoviesPiece,"a"),
     fun(a)  
       try 
         return Str_IndexOf("mid", a.href)>0
       catch E
         on true do
	   return false;
       end;
     end
  );

  var localTitle = Str_Trim(Text(links[0]));
  result.title:=localTitle;

  var mytext = Text(inMoviesPiece);
  //Print Ln("Source=", mytext); 
  var timesAndRating = Str_Search(mytext, `(\d)hr&nbsp;(\d+?)min - Rated&nbsp;(.+?) - (.*?) -.*$`);

  //Print Ln("Duration=", timesAndRating[0][1]+":"+timesAndRating[0][2]); 
  //Print Ln("Genres=", timesAndRating[0][4]); 
  //Print Ln("Ratings=", timesAndRating[0][3]); 
  result.genres:=timesAndRating[0][4];

  var movieTimes = Str_Search(mytext, `(\d+?:\d\d)`);

  var localTimes = "";
  every movieTime in movieTimes do
    //Print Ln("Movie time=", movieTime[1]); 
    localTimes = localTimes + movieTime[1] + "X";
  end;
  result.movieTimes:=localTimes;
  return result;
end;

var getMovieEvents = fun (inCity) 

  var result = [];

  //PrintInfo("Getting MovieEvents for city " + inCity,3);
  var page = nil;
  
  if (true) then 
    //page = GetURL(googleMovieSearchUrl, [. "hl" = "en", "q" = "movie:"+inCity, "btnG" = "Google Search" .]);
    page = GetURL(googleMovieSearchUrl, [. "sc" = "1", "near" = inCity, "rl" = "1" .]);
    //Files SaveToFile("google_movies.html", Pretty(page));
  else
    page = LoadPage("google_movies.html");
  end;

  var links = Select (
     Elem(page,"a"),
     fun(a)  
       try 
         return ((Str_IndexOf("tid", a.href)>0) or (Str_IndexOf("mid", a.href)>0))
       catch E
         on true do
	   return false;
       end;
     end
  );

  var currentTheaterName=nil;
  var currentTheaterAddress=nil;
  every link in links do
    try 
      if ((Str_IndexOf("tid", link.href)>0) and (Str_IndexOf("date", link.href)<0))   then
	var localPiece = (Elem(page, "td") directlycontain link)[0];

	var localaddress = Elem(page, "font") directlyafter link;
	currentTheaterAddress = Text(localaddress[0]);

        currentTheaterName= Str_Trim(Text(link));
	//PrintInfo("Processing movie theater:"+currentTheaterName, 3);
      end;
      if ((Str_IndexOf("mid", link.href)>0) and (Str_IndexOf("date", link.href)<0)) then
	var localMoviePiece = (Elem(page, "td") directlycontain link)[0];
	var localResult = getMoviesFromPiece(localMoviePiece);
	localResult.theaterName:=currentTheaterName;
	localResult.theaterAddress:=currentTheaterAddress;
	result = result + [ localResult ];
      end;
    catch E
      on true do
        PrintLn("Could not process", E);
    end;
  end;
  //PrintInfo("Done getting MovieEvents for city " + inCity,3);
  return result;
end;


var main = fun(city)
  //PrintInfo("Hello from Movies Service",1);
  //Print Ln("Result: ",getMovieEvents("palo alto"));
  //getMovieEvents("Boston");
  //getWebServiceEngine().getInstance().registerCallback("getMovieEvents", getMovieEvents);
  getMovieEvents(city);
end;

main(city);