Valhalla Legends Forums Archive | Java Programming | StealthBot News Retriever

AuthorMessageTime
JoeTheOdd
When StealthBot starts up, it displays some news about whats been going on latley on the SB world. I wrote a small Java program to retreive that information, and display it to the console. Stealth uses \n in his PHP file to make a new line, and I tried replacing that with \r\n to make a new line, but it doesn't seem to be working. Any suggestions? Heres my code.

[code]import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

// Created on Mar 25, 2005
// @author JoeTheOdd
// Retreives the SB news and prints it to the console

public class Main {
  public static void main(String args[]) {
      Socket sckSBNews;
      String line;
      try {
          sckSBNews = new Socket("www.stealthbot.net", 80);
          BufferedReader in = new BufferedReader(new InputStreamReader(sckSBNews.getInputStream()));
          BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sckSBNews.getOutputStream()));
          out.write("GET /getver.php HTTP/1.0" + "\r\n" + "Host: www.stealthbot.net" + "\r\n" + "\r\n");
          out.flush();
          while ((line = in.readLine()) != null)
              parseNews(in.readLine());
          sckSBNews.close();
      }
      catch (UnknownHostException e) { System.out.println("Unable to connect to stealthbot.net:80"); }
      catch (IOException e) { System.out.println("Caught IOException"); }
  }
 
  private static void parseNews(String s) {
      try
      {
          String data[];
          String data2;
          data = s.split(","); data2 = data[3]; // remove first two numbers
          data2 = data2.replaceAll("˙cb", "");  // remove any bold flags
          data = data2.split("\n");            // split at new lines
          for(int i = 0; i < data.length; i++) { System.out.println(data[i]); }
      }
      catch (ArrayIndexOutOfBoundsException e) {}
  }
}[/code]
March 26, 2005, 8:20 PM
iago
If you need to split at \n or \r\n, use BufferedReader.readLine().  It looks after that for you.
March 26, 2005, 8:31 PM
JoeTheOdd
Thanks.
March 26, 2005, 10:02 PM
Kp
Also, try not to throw away useful information.  You're only processing even numbered lines.  Finally, consider not doing concatenation of several adjacent string constants; just write them as one.  It works the same, but concatenating them like that looks a bit silly.
March 26, 2005, 11:20 PM
iago
[quote author=Kp link=topic=11049.msg105684#msg105684 date=1111879224]
Also, try not to throw away useful information.  You're only processing even numbered lines.
[/quote]
Good point

[quote]Finally, consider not doing concatenation of several adjacent string constants; just write them as one.  It works the same, but concatenating them like that looks a bit silly.
[/quote]
Haha, I saw that but opted to ignore it. 
March 26, 2005, 11:34 PM
JoeTheOdd
[quote]Finally, consider not doing concatenation of several adjacent string constants; just write them as one.  It works the same, but concatenating them like that looks a bit silly.[/quote]

Woah, huh?
March 28, 2005, 1:58 PM
JoeTheOdd
After some random changes I put to the test, I've managed to pump out this.

[code]StealthBot News:
> Normal News:
>> Date: Mon, 28 Mar 2005 13:59:46 GMT
>> X-Powered-By: PHP/4.3.10
>> Content-Type: text/html
>> 584171,241,x,If you need help or have questions feel free to join our forums at http://www.stealthbot.net. They're back up!\n\nWant to trade Diablo II items? Check out the new http://www.d2central.com ! D2Central holds tournaments raffles and more - all realms are welcome!
> Beta News:
>> Date: Mon, 28 Mar 2005 13:59:46 GMT
>> X-Powered-By: PHP/4.3.10
>> Content-Type: text/html
>> 22194|260|˙cbStealthBot v2.6 is now available!\nVisit http://www.stealthbot.net to download a copy today.\n\n˙cbForge Hosting is the new home of StealthBot.net\nForge Hosting's lightning-fast servers can handle all of your website needs.\nPlans start at just $4.99 monthly for 1GB space and 20GB transfer with 24 hour activation guaranteed. Check us out at http://www.forgehosting.com!|Beta version 2.620 is now available. Snag it in Betaland.
[/code]

I'm going to keep working on it, and parsing the \n's still seems impossible. Also, BufferReader.readLine() doesn't seem to fix that.

Should I just input the 4th line of each thing the server sends back, or is there a more professional way to do it?
March 28, 2005, 2:02 PM
kamakazie
Here is something I put together real quick. It uses the built in classes so one doesn't actually have to parse through the http headers and such. I didn't do any parsing of the actual data (news) since I figured you should be able to do that. This is pretty easy to follow if you have the SDK in front of you.

[code]
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class StealthBotNews {
public static void main(String args[]) {
try {
URL stealthbot = new URL("http", "www.stealthbot.net", 80, "/getver.php");
HttpURLConnection conn = (HttpURLConnection)stealthbot.openConnection();

conn.connect();

if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

System.out.println(in.readLine());
} else {
System.out.println("Bad response code: " + conn.getResponseCode());
}

conn.disconnect();

} catch (Exception e) {
e.printStackTrace();
}

}
}
[/code]
March 28, 2005, 4:25 PM
JoeTheOdd
Mind linking me to the SDK? Looks good at a quick glance. I'll test it later today after I've gone to bed. Erm, had a nap?
March 30, 2005, 9:57 AM
Myndfyr
[quote author=Joey link=topic=11049.msg106304#msg106304 date=1112176639]
Mind linking me to the SDK? Looks good at a quick glance. I'll test it later today after I've gone to bed. Erm, had a nap?
[/quote]

http://java.sun.com/j2se/1.4.2/docs/api/
March 30, 2005, 2:42 PM
JoeTheOdd
Thanks MyndFyre.

I'm going to work on this right now waiting for Blizzy to get their effing servers back up.
March 31, 2005, 12:45 PM
JoeTheOdd
Excellent. You men happen to be my heroes of the day!

[code]Initiating SBNews...

StealthBot News:
> Version News:
>> 22194|260|˙cbStealthBot v2.6 is now available!\nVisit http://www.stealthbot.net to download a copy today.\n\n˙cbForge Hosting is the new home of StealthBot.net\nForge Hosting's lightning-fast servers can handle all of your website needs.\nPlans start at just $4.99 monthly for 1GB space and 20GB transfer with 24 hour activation guaranteed. Check us out at http://www.forgehosting.com!|Beta version 2.620 is now available. Snag it in Betaland.
> Normal News:
>> 584171,241,x,If you need help or have questions feel free to join our forums at http://www.stealthbot.net. They're back up!\n\nWant to trade Diablo II items? Check out the new http://www.d2central.com ! D2Central holds tournaments raffles and more - all realms are welcome!
> Beta News:
>> 22194|260|˙cbStealthBot v2.6 is now available!\nVisit http://www.stealthbot.net to download a copy today.\n\n˙cbForge Hosting is the new home of StealthBot.net\nForge Hosting's lightning-fast servers can handle all of your website needs.\nPlans start at just $4.99 monthly for 1GB space and 20GB transfer with 24 hour activation guaranteed. Check us out at http://www.forgehosting.com!|Beta version 2.620 is now available. Snag it in Betaland.[/code]

EDIT: My splitting method doesn't seem to be working exactly correct. It works for the normal news, but the beta and version news, from getver2.php, don't seem to come out right. Anybody wanna take a look? A VB version of what I'm trying to do is available at joe.astriks.com under the name prjSBNews-src.

[code]// Created 3-25-05
// Re-Created 2-26-05
// Author: JoeTheOdd
// Thanks to dxoigmn for a better connection method

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String args[]) {
        System.out.println("Initiating SBNews...");
        System.out.println();
       
        String sVerNews; String sNormNews; String sBetaNews;
        sVerNews = getVerNews();
        sNormNews = getNormNews();
        sBetaNews = getBetaNews();
       
        System.out.println("StealthBot News:");
        System.out.println("> Version News:");
        System.out.println(">> " + sVerNews);
        System.out.println("> Normal News:");
        System.out.println(">> " + sNormNews);
        System.out.println("> Beta News:");
        System.out.println(">> " + sBetaNews);
    }

    // @return - SB Version News
    private static String getVerNews() {
        String s = null; String sSplt[];
        try {
            URL stealthbot = new URL("http", "www.stealthbot.net", 80, "/getver2.php");
            HttpURLConnection conn = (HttpURLConnection) stealthbot.openConnection();
            conn.connect();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                s = in.readLine();
            }else{
                System.out.println("Bad response code: " + conn.getResponseCode());
            }

            conn.disconnect();
        }catch(Exception e){
            e.printStackTrace();
        }
        sSplt = s.split("|");
        return sSplt[2].toString();
    }

    // @return - SB Normal News
    public static String getNormNews() {
        String s = null; String sSplt[];
        try {
            URL stealthbot = new URL("http", "www.stealthbot.net", 80, "/getver.php");
            HttpURLConnection conn = (HttpURLConnection) stealthbot.openConnection();
            conn.connect();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                s = in.readLine();
            }else{
                System.out.println("Bad response code: " + conn.getResponseCode());
            }

            conn.disconnect();
        }catch(Exception e){
            e.printStackTrace();
        }
        sSplt = s.split(",");
        return sSplt[3].toString();
    }
   
    // @return - SB Beta News
    private static String getBetaNews() {
        String s = null; String sSplt[];
        try {
            URL stealthbot = new URL("http", "www.stealthbot.net", 80, "/getver2.php");
            HttpURLConnection conn = (HttpURLConnection) stealthbot.openConnection();
            conn.connect();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                s = in.readLine();
            }else{
                System.out.println("Bad response code: " + conn.getResponseCode());
            }

            conn.disconnect();
        }catch(Exception e){
            e.printStackTrace();
        }
        sSplt = s.split("|");
        return sSplt[3].toString();
    }
}[/code]
March 31, 2005, 12:58 PM
kamakazie
[quote author=Joey link=topic=11049.msg106516#msg106516 date=1112273931]
EDIT: My splitting method doesn't seem to be working exactly correct. It works for the normal news, but the beta and version news, from getver2.php, don't seem to come out right. Anybody wanna take a look? A VB version of what I'm trying to do is available at joe.astriks.com under the name prjSBNews-src.
[/quote]

In java, for a string's split method, it takes a regular expression, NOT a character(s) to split on like in Visual Basic. So what you probably need to do is escape the character (in this case the pipe, |) since the pipe is a special character in regular expressions.
March 31, 2005, 3:33 PM
Myndfyr
[quote author=dxoigmn link=topic=11049.msg106542#msg106542 date=1112283221]
In java, for a string's split method, it takes a regular expression, NOT a character(s) to split on like in Visual Basic. So what you probably need to do is escape the character (in this case the pipe, |) since the pipe is a special character in regular expressions.
[/quote]

Alternatively, you could use Java's StringTokenizer class, which can take one or more characters as delimiters.  You then run through it in the following manner:
[code]
while (myTokenizer.hasMoreTokens())
{
  System.out.println(new String("Next token: ").concat(myTokenizer.nextToken()));
}
[/code]
April 1, 2005, 1:36 AM
JoeTheOdd
W
o
w

I'll try some more on this after school.
April 4, 2005, 11:44 AM

Search