/* appjet:version 0.1 */

import("storage");

/* appjet:server */
page.setMode('plain');

if (!storage.count) {
  storage.count = 0;
}
storage.count++;

var LEAGUE_TEXT_A = '<td class="title" colspan="4" height="18">&nbsp;';
var LEAGUE_TEXT_B = '</td>';

function _parseHiddenSid(t) {
  var res = t.match(/name="sid" value="([0-9a-zA-Z]*)"/);
  if (res) {
    return res[1];
  }
  else {
    return '';
  }
}

function _readBody() {
  var t = wget("http://livescore.com/default.dll?page=home");
  var sid = _parseHiddenSid(t);
  return wget("http://livescore.com/default.dll?page=home&sid=" + sid);
}

function _readLeagueList(t) {
  var league = new Array();
  var cont = true;
  while (cont) {
    var pos = t.indexOf(LEAGUE_TEXT_A);
    if (pos <= 0) {
      break;
    }
    t = t.substring(pos + LEAGUE_TEXT_A.length);
    pos = t.indexOf("</td>");
    var s = t.substring(0, pos);
    league[league.length] = s;
  }
  return league;
}

/**
@param t HTML content
@param n zero-based if of league selected
@param league The league name
*/
function _readMatch(t, n, league) {
  var matches = new Array();
  var pos = t.indexOf(LEAGUE_TEXT_A + league);
  var pos2 = t.indexOf('<tr bgcolor="#111111">', pos + 1);
  if (pos <= 0 || pos2 <= 0) {
    return matches;
  }

  //t = t.substring(pos + (LEAGUE_TEXT_A + league + LEAGUE_TEXT_B).length, pos2);
  t = t.substring(pos, pos2);

  // remove width definition
  t = t.replace(/ width="\d*"/gm, '');
  // remove popup window
  t = t.replace(/target="[^"]*"/gm, '');
  t = t.replace(/onclick="[^"]*"/gm, '');
  // replace game score link
  t = t.replace(/default.dll\/Game\?/gm, 'g?n=' + n + '&');

  print(raw("<table cellpadding='1' border='0'><tr>" + t + "</table>"));
}

function doMainFlow(showGame) {
  var t = _readBody();
  var league = _readLeagueList(t);

  if (showGame && request.params.comp && request.params.game) {
    var game = wget("http://livescore.com/default.dll/Game?comp=" + request.params.comp + "&game=" + request.params.game);
    var pos = game.indexOf("<table");
    var pos2 = game.indexOf('<tr class="cr"', pos + 1);
    if (pos > 0 && pos2 > 0) {
      game = game.substring(pos, pos2) + "</table>";
      game = game.replace(/bgcolor="[^"]*"/gm, '');
      game = game.replace(/width="[^"]*"/gm, '');
      game = game.replace(/<font [^>]*>/gm, '');
      game = game.replace(/<\/font>/gm, '');
      print(raw(game));
    }
  }

  var n = 0;
  if (request.params.n) {
    n = request.params.n
  }
  if (n > league.length) {
    n = 0;
  }
  _readMatch(t, n, league[n]);

  print(HR());
  // Print league list
  for (var i=0;i<league.length;i++) {
    print(raw("<a href='/?n=" + i + "'>" + league[i] + "</a><br/>"));
  }
}

function get_main() {
  doMainFlow(false);
}

function get_g() {
  doMainFlow(true);
}

// Main Template
print(raw("""
<head>
<meta name="viewport" content="width=320">
<title>Mobile Livescore</title>
<style>
body, td { font-family: arial; }
a:visited { color:blue; }
.match-light { background-color:#fff; }
.dark, #livescore .light { background-color:#ccc; text-align:right; }
</style>
</head>
<body>
<h3>Mobile Livescore</h3>
"""));

dispatch();

print(raw("""
<hr/>
<span style="font-style:italic;">Mobile Livescore version 1.1 (20080318) |
""" +
storage.count +
"""
 hits since 11-Jan-2008</span>
"""));

print(raw("""
</body>
</html>
"""));



© Copyright 2007-2008 AppJet Inc.