ZackBCFC
08-04-2007, 04:12 AM
Hi,
My mate has made something using Java that means that it can go to a website, read through the page until a certain point and from that point get the link. The contents of that link (again from a certain point) are output as WML files. All this requires Java on the server which I don't have and can't get setup with my hosting.
Is it possible to do a similar thing with php? Here is the script with some URLs removed.
//=============================================================================
// Project: New
//=============================================================================
// $Id: Zack.java 2089 2006-10-06 13:05:25Z apf $
//=============================================================================
package package;
/**
* Class for fetching updates.
*
*/
public class Zack
{
private static final String ZACK_FILE_NAME = "zack.wml";
private static final String ZACK_URL = "http://the main page.com/?account=x";
private static final String AMP = "&";
private static final String MATCHES_TABLE_START = "<p class=\"text\">The following matches have been started</p>";
private static final String TABLE_END = "</table>";
private static final String LINK_START = "<a href";
private static final String TAG_END = ">";
private static final String MATCH_START = "match=";
private static final String MATCH_END = "\"";
private static final String MATCH_URL = ZACK_URL + AMP + MATCH_START;
private static final String H1_START = "<h1>";
private static final String H1_END = "</h1>";
private static final String LINEUP_TABLE_START = ">Football</th></tr>";
private static final String UPDATES_TABLE_START = ">Updates</td></tr>";
private static final String ROW_END = "</tr>";
private static final String ROW_START = "<tr>";
private static final String TD_START = "<td";
private static final String TD_END = "</td>";
private static final String HEADER_START = "<th";
private static final String HEADER_END = "</th>";
private final Reader reader;
private Reader matchReader = null;
private String currentMatch = "";
private String firstH1 = "";
private String updates = "";
private String lineup = "";
private String subs = "";
/**
* Constructor for the Zack class.
*/
public Zack ()
{
reader = new Reader( ZACK_URL );
}
/**
* Write out Zack WML file.
*/
private void writeFile ()
{
final Writer file = new Writer( ZACK_FILE_NAME );
file.writeOneLine( firstH1 );
file.writeOneLine( "Updates" );
file.writeLines( updates );
if (lineup.length() > 0)
{
file.writeOneLine( "Line Up" );
file.writeLines( lineup );
if (subs.length() > 0)
{
file.writeOneLine( "Substitutes" );
file.writeLines( subs );
}
}
file.closeFile();
}
/**
* Refresh Zack's updates.
*/
public void fetch ()
{
reader.fetch();
final String page = reader.getString();
final String table = Reader.extract( page, MATCHES_TABLE_START, TABLE_END );
final String firstLink = Reader.extract( table, LINK_START, TAG_END );
final String match = Reader.extract( firstLink, MATCH_START, MATCH_END );
if (!currentMatch.equalsIgnoreCase( match ))
{
currentMatch = match;
matchReader = new Reader( MATCH_URL + currentMatch );
}
if (matchReader == null)
{
return;
}
matchReader.fetch();
final String matchPage = matchReader.getString();
firstH1 = Reader.extract( matchPage, H1_START, H1_END );
final String updateTable = Reader.extract( matchPage, UPDATES_TABLE_START, TABLE_END );
String remaining = updateTable;
updates = "";
while (remaining.length() > 0)
{
final String row = Reader.extract( remaining, ROW_START, ROW_END );
if (row.length() > 0)
{
final String firstTD = Reader.extract( row, TD_START, TD_END );
final String firstTDtext = Reader.extract( firstTD + TD_END, TAG_END, TD_END );
final String firstTDremain = Reader.remove( row, TD_END );
final String secondTD = Reader.extract( firstTDremain, TD_START, TD_END );
final String secondTDtext = Reader.extract( secondTD + TD_END, TAG_END, TD_END );
updates = updates + Writer.makePara( secondTDtext + " (" + firstTDtext + ")" );
remaining = Reader.remove( remaining, ROW_START + row + ROW_END );
}
else
{
remaining = "";
}
}
final String lineupTable = Reader.extract( matchPage, LINEUP_TABLE_START, TABLE_END );
remaining = lineupTable;
boolean sub = false;
lineup = "";
subs = "";
while (remaining.length() > 0)
{
final String row = Reader.extract( remaining, ROW_START, ROW_END );
if (row.length() > 0)
{
final String firstTD = Reader.extract( row, TD_START, TD_END );
final String firstTDtext = Reader.extract( firstTD + TD_END, TAG_END, TD_END );
final String firstTDremain = Reader.remove( row, TD_END );
final String secondTD = Reader.extract( firstTDremain, TD_START, TD_END );
final String secondTDtext = Reader.extract( secondTD + TD_END, TAG_END, TD_END );
final String header = Reader.extract( row, HEADER_START, HEADER_END );
if (secondTDtext.length() > 0)
{
if (sub)
{
subs = subs + Writer.makePara( firstTDtext + " " + secondTDtext );
}
else
{
lineup = lineup + Writer.makePara( firstTDtext + " " + secondTDtext );
}
}
else if (header.length() > 0)
{
final String substring = Reader.remove( header, TAG_END );
if (substring.equalsIgnoreCase( "Substitutes" ))
{
sub = true;
}
}
remaining = Reader.remove( remaining, ROW_START + row + ROW_END );
}
else
{
remaining = "";
}
}
writeFile();
}
}
I am not too sure how this is all done because I am new to Java and am still learning, but I was hopeing maybe someone else would understand what is happening by me posting that code, and then advice me if there was anyway that this could be done using just a PHP script?
Thanks for your time
Zack
My mate has made something using Java that means that it can go to a website, read through the page until a certain point and from that point get the link. The contents of that link (again from a certain point) are output as WML files. All this requires Java on the server which I don't have and can't get setup with my hosting.
Is it possible to do a similar thing with php? Here is the script with some URLs removed.
//=============================================================================
// Project: New
//=============================================================================
// $Id: Zack.java 2089 2006-10-06 13:05:25Z apf $
//=============================================================================
package package;
/**
* Class for fetching updates.
*
*/
public class Zack
{
private static final String ZACK_FILE_NAME = "zack.wml";
private static final String ZACK_URL = "http://the main page.com/?account=x";
private static final String AMP = "&";
private static final String MATCHES_TABLE_START = "<p class=\"text\">The following matches have been started</p>";
private static final String TABLE_END = "</table>";
private static final String LINK_START = "<a href";
private static final String TAG_END = ">";
private static final String MATCH_START = "match=";
private static final String MATCH_END = "\"";
private static final String MATCH_URL = ZACK_URL + AMP + MATCH_START;
private static final String H1_START = "<h1>";
private static final String H1_END = "</h1>";
private static final String LINEUP_TABLE_START = ">Football</th></tr>";
private static final String UPDATES_TABLE_START = ">Updates</td></tr>";
private static final String ROW_END = "</tr>";
private static final String ROW_START = "<tr>";
private static final String TD_START = "<td";
private static final String TD_END = "</td>";
private static final String HEADER_START = "<th";
private static final String HEADER_END = "</th>";
private final Reader reader;
private Reader matchReader = null;
private String currentMatch = "";
private String firstH1 = "";
private String updates = "";
private String lineup = "";
private String subs = "";
/**
* Constructor for the Zack class.
*/
public Zack ()
{
reader = new Reader( ZACK_URL );
}
/**
* Write out Zack WML file.
*/
private void writeFile ()
{
final Writer file = new Writer( ZACK_FILE_NAME );
file.writeOneLine( firstH1 );
file.writeOneLine( "Updates" );
file.writeLines( updates );
if (lineup.length() > 0)
{
file.writeOneLine( "Line Up" );
file.writeLines( lineup );
if (subs.length() > 0)
{
file.writeOneLine( "Substitutes" );
file.writeLines( subs );
}
}
file.closeFile();
}
/**
* Refresh Zack's updates.
*/
public void fetch ()
{
reader.fetch();
final String page = reader.getString();
final String table = Reader.extract( page, MATCHES_TABLE_START, TABLE_END );
final String firstLink = Reader.extract( table, LINK_START, TAG_END );
final String match = Reader.extract( firstLink, MATCH_START, MATCH_END );
if (!currentMatch.equalsIgnoreCase( match ))
{
currentMatch = match;
matchReader = new Reader( MATCH_URL + currentMatch );
}
if (matchReader == null)
{
return;
}
matchReader.fetch();
final String matchPage = matchReader.getString();
firstH1 = Reader.extract( matchPage, H1_START, H1_END );
final String updateTable = Reader.extract( matchPage, UPDATES_TABLE_START, TABLE_END );
String remaining = updateTable;
updates = "";
while (remaining.length() > 0)
{
final String row = Reader.extract( remaining, ROW_START, ROW_END );
if (row.length() > 0)
{
final String firstTD = Reader.extract( row, TD_START, TD_END );
final String firstTDtext = Reader.extract( firstTD + TD_END, TAG_END, TD_END );
final String firstTDremain = Reader.remove( row, TD_END );
final String secondTD = Reader.extract( firstTDremain, TD_START, TD_END );
final String secondTDtext = Reader.extract( secondTD + TD_END, TAG_END, TD_END );
updates = updates + Writer.makePara( secondTDtext + " (" + firstTDtext + ")" );
remaining = Reader.remove( remaining, ROW_START + row + ROW_END );
}
else
{
remaining = "";
}
}
final String lineupTable = Reader.extract( matchPage, LINEUP_TABLE_START, TABLE_END );
remaining = lineupTable;
boolean sub = false;
lineup = "";
subs = "";
while (remaining.length() > 0)
{
final String row = Reader.extract( remaining, ROW_START, ROW_END );
if (row.length() > 0)
{
final String firstTD = Reader.extract( row, TD_START, TD_END );
final String firstTDtext = Reader.extract( firstTD + TD_END, TAG_END, TD_END );
final String firstTDremain = Reader.remove( row, TD_END );
final String secondTD = Reader.extract( firstTDremain, TD_START, TD_END );
final String secondTDtext = Reader.extract( secondTD + TD_END, TAG_END, TD_END );
final String header = Reader.extract( row, HEADER_START, HEADER_END );
if (secondTDtext.length() > 0)
{
if (sub)
{
subs = subs + Writer.makePara( firstTDtext + " " + secondTDtext );
}
else
{
lineup = lineup + Writer.makePara( firstTDtext + " " + secondTDtext );
}
}
else if (header.length() > 0)
{
final String substring = Reader.remove( header, TAG_END );
if (substring.equalsIgnoreCase( "Substitutes" ))
{
sub = true;
}
}
remaining = Reader.remove( remaining, ROW_START + row + ROW_END );
}
else
{
remaining = "";
}
}
writeFile();
}
}
I am not too sure how this is all done because I am new to Java and am still learning, but I was hopeing maybe someone else would understand what is happening by me posting that code, and then advice me if there was anyway that this could be done using just a PHP script?
Thanks for your time
Zack