Source: site.view [edit]
Function name: go
Arguments:
Description:
Page type: webl
Render function:  
Module: geneticFinance

Page source:

// Many of the company names have things like "-CLASS A SHARES" in the name, prune those.
var CleanField = fun(s)

   var i = Str_IndexOf("-", s);
   if (i > 0) then
      s = Select(s, 0, i)
   end;
   return s;
end;
  

// We need it in this format: 20050101:20050201  
var FormatDate = fun(dt)
   var bf = dt;
   var sdf = Wub_SimpleDateFormat("dd-MMM-yy");
   var d = sdf.parse(dt) ? nil;
   if d != nil then
      sdf = Wub_SimpleDateFormat("YYYYMMdd");
      dt = sdf.format(d);
      var c = Wub_GetCalendarClass().getInstance();
      c.setTime(d);
      var CalendarDATE = 5;
      c.add(CalendarDATE, -1);  // number of days to add
      bf = sdf.format(c.getTime());
      bf = bf;
   end;
   return [. day=dt, dayBefore=bf .];
end;

var readCSV = fun(cmd, idCol)

   var res = [. .];
   var headers = nil;
   every line in Str_Split(WubCall(cmd, []), "\n") do
      var cols = Str_Split(line, ",");
      if headers == nil then
         headers = cols
      else
         var obj = [. .];
         var i = 0;
         every h in headers do
            obj[h] := CleanField(cols[i]);
            i = i + 1
         end;
         res[Str_ToLowerCase(cols[idCol])] := obj
      end;
   end;
   return res
end;


var errs = [];

var main = fun()
   var stockInfo = readCSV("stockInfo", 2);

   var names=[. .];

   every line in Str_Split(WubCall("stockChange", []), "\n") do
      var cols = Str_Split(line, ",");
      var symbol = Str_ToLowerCase(cols[1]);
      var dt = FormatDate(cols[0]);
      var change = ToReal(cols[2]);
      (names[symbol] := [. fullName=stockInfo[symbol].company, change=change, dt=dt .] ) ? (errs = errs + [symbol]);
   end;
   
   var i = 0;
   var res = [];
   
   var ok = true;

   var writer = Wub_NewLuceneIndex("resumes", true);  // create is true, recreate index
   var doc = Wub_NewLuceneDocument();

   var j = 0;
   every symbol in names do
      if i < 3000 then
          var news = WubCall("searchDates", [ names[symbol].fullName + " (" + symbol + ")", 
                                       names[symbol].dt.day + ":" + names[symbol].dt.day]);
          every n in news do
                j = j + 1;
                // Add fields to doc
                ok = ok and Wub_AddLuceneField(doc, "num", ToString(j), true, true);
                ok = ok and Wub_AddLuceneField(doc, "symbol", symbol, true, true);
                ok = ok and Wub_AddLuceneField(doc, "fullname", names[symbol].fullName, true, true);
                ok = ok and Wub_AddLuceneField(doc, "day", names[symbol].dt.day, true, true);
                ok = ok and Wub_AddLuceneField(doc, "title", n.title, true, true);
                ok = ok and Wub_AddLuceneField(doc, "sitename", n.sitename, true, true);
                ok = ok and Wub_AddLuceneField(doc, "link", n.link, true, true);
                ok = ok and Wub_AddLuceneField(doc, "snippet", n.snippet, true, true);
                ok = ok and Wub_AddLuceneField(doc, "cacheid", n.cacheId, true, true);
                ok = ok and Wub_AddLuceneDocument(writer, doc);
          end
      end;
      i = i + 1
   end;
   ok = ok and Wub_OptimizeLucene(writer);
   ok = ok and Wub_CloseLuceneIndex(writer);   
   j;
end;
  
main();