Valhalla Legends Archive

Programming => General Programming => Java Programming => Topic started by: Joe[x86] on March 26, 2005, 02:20 PM

Title: StealthBot News Retriever
Post by: Joe[x86] on March 26, 2005, 02:20 PM
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.

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) {}
   }
}
Title: Re: StealthBot News Retriever
Post by: iago on March 26, 2005, 02:31 PM
If you need to split at \n or \r\n, use BufferedReader.readLine().  It looks after that for you.
Title: Re: StealthBot News Retriever
Post by: Joe[x86] on March 26, 2005, 04:02 PM
Thanks.
Title: Re: StealthBot News Retriever
Post by: Kp on March 26, 2005, 05:20 PM
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.
Title: Re: StealthBot News Retriever
Post by: iago on March 26, 2005, 05:34 PM
Quote from: Kp on March 26, 2005, 05:20 PM
Also, try not to throw away useful information.  You're only processing even numbered lines.
Good point

QuoteFinally, 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.
Haha, I saw that but opted to ignore it. 
Title: Re: StealthBot News Retriever
Post by: Joe[x86] on March 28, 2005, 07:58 AM
QuoteFinally, 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.

Woah, huh?
Title: Re: StealthBot News Retriever
Post by: Joe[x86] on March 28, 2005, 08:02 AM
After some random changes I put to the test, I've managed to pump out this.

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.


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?
Title: Re: StealthBot News Retriever
Post by: dxoigmn on March 28, 2005, 10:25 AM
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.


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();
}

}
}
Title: Re: StealthBot News Retriever
Post by: Joe[x86] on March 30, 2005, 03:57 AM
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?
Title: Re: StealthBot News Retriever
Post by: MyndFyre on March 30, 2005, 08:42 AM
Quote from: Joey on March 30, 2005, 03:57 AM
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?

http://java.sun.com/j2se/1.4.2/docs/api/
Title: Re: StealthBot News Retriever
Post by: Joe[x86] on March 31, 2005, 06:45 AM
Thanks MyndFyre.

I'm going to work on this right now waiting for Blizzy to get their effing servers back up.
Title: Re: StealthBot News Retriever
Post by: Joe[x86] on March 31, 2005, 06:58 AM
Excellent. You men happen to be my heroes of the day!

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.


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.

// 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();
    }
}
Title: Re: StealthBot News Retriever
Post by: dxoigmn on March 31, 2005, 09:33 AM
Quote from: Joey on March 31, 2005, 06:58 AM
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.

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.
Title: Re: StealthBot News Retriever
Post by: MyndFyre on March 31, 2005, 07:36 PM
Quote from: dxoigmn on March 31, 2005, 09:33 AM
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.

Alternatively, you could use Java's StringTokenizer (http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html) class, which can take one or more characters as delimiters.  You then run through it in the following manner:

while (myTokenizer.hasMoreTokens())
{
  System.out.println(new String("Next token: ").concat(myTokenizer.nextToken()));
}
Title: Re: StealthBot News Retriever
Post by: Joe[x86] on April 04, 2005, 06:44 AM
W
o
w

I'll try some more on this after school.