[JAVA] Site Search Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SKG_Scintill
    Spun a twirly fruitcake,
    FFR Simfile Author
    • Feb 2009
    • 3875

    #1

    [JAVA] Site Search Help

    I was checking if I could make a program that searches for a certain word on a site. It that word was on it, it would do a system.out.println(); if it wasn't, it would simulate pressing f5 and loop the class.

    Here's my code:
    N.B: There's two classes, so you can't just copy paste.

    Code:
    [b]//Tabswitch class is so it doesn't loop alt+tab later on[/b]
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class Tabswitch {
    	public static void main(String[] args) throws Exception{
    		Robot r = new Robot();
    		r.keyPress(KeyEvent.VK_ALT);
    		r.keyPress(KeyEvent.VK_TAB);
    		r.delay(100);
    		r.keyRelease(KeyEvent.VK_ALT);
    		r.keyRelease(KeyEvent.VK_TAB);
    		URLReader a = new URLReader();
    	}
    }
    
    [b]//URLReader class is where my question lies[/b]
    
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class URLReader{
    	public URLReader() throws Exception{
    		URL site = new URL("http://kaction.com/badfanfiction/");
    		BufferedReader in = new BufferedReader(
    				new InputStreamReader(site.openStream()));
    		
    		String inputLine;
    		while((inputLine = in.readLine()) != null)
    			if(inputLine.contains("the"))
    				System.out.println(inputLine);
    			else{
    				Robot s = new Robot();
    				s.delay(2000);
    				s.keyPress(KeyEvent.VK_F5);
    				s.keyRelease(KeyEvent.VK_F5);
    				s.delay(2000);
    				URLReader a = new URLReader();
    			}
    	}
    }
    My issue lies in the last "if"-statement. It doesn't print inputLine, it always goes to the "else"-statement.
    I was trying to make a .containsnot() of some sorts so it could be a "while"-loop, but to no avail...

    Any ideas how to fix this?


    P.S: I used badfanfiction so my printline wouldn't be massive.
    Last edited by SKG_Scintill; 05-1-2012, 03:06 AM.





    Originally posted by bluguerilla
    So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
    ___
    . RHYTHMS PR LAYERING
    . ZOMG I HAD TO QUIT OUT TERRIBLE
    .
  • UserNameGoesHere
    FFR Veteran
    • May 2008
    • 1114

    #2
    Re: [JAVA] Site Search Help

    The problem is you have an infinite loop of sorts. If the very first line has input but doesn't contain "the" it immediately loads the page again. The recursively-spawned URLReader then does the same. Ad-infinitum.

    Do you just want it to read the entire page, catch all lines which have "the", print out only those lines, and only AFTER that reload the page if there weren't any? Then rewrite your while like this
    Code:
    boolean found = false;
    while((inputLine = in.readLine()) != null){
    	if(inputLine.contains("the")){
    		found = true;
    		System.out.println(inputLine);
    	}
    }
    if(!found){
    	Robot s = new Robot();
    	s.delay(2000);
    	s.keyPress(KeyEvent.VK_F5);
    	s.keyRelease(KeyEvent.VK_F5);
    	s.delay(2000);
    	URLReader a = new URLReader();
    }
    Is there a reason you're using a recursive solution, by the way? A nonrecursive one would be less resource-hungry. Would you prefer a nonrecursive one?

    Also, other than serving up delays when relevant (and those are relevant), what is your Robot doing? I don't think it's doing what you may think it's doing. In particular your Robot is in no way connected with your URL stream and if it is (as I am guessing/assuming) meant for scripting Firefox or another browser in the background, its scripting is independent of everything else this program is trying to do and is unrelated.

    To me, the Robot looks useless and I'd remove it unless you can integrate it better with what you're actually trying to do. If it's not needed for anything else, replace the Robot delay calls with Thread.sleep calls instead.
    Last edited by UserNameGoesHere; 05-1-2012, 04:15 AM.
    Originally posted by Crashfan3
    Man, what would we do without bored rednecks?

    Comment

    • SKG_Scintill
      Spun a twirly fruitcake,
      FFR Simfile Author
      • Feb 2009
      • 3875

      #3
      Re: [JAVA] Site Search Help

      I think it's got to do with the fact that I've only had 3 months of programming class so far ;)





      Originally posted by bluguerilla
      So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
      ___
      . RHYTHMS PR LAYERING
      . ZOMG I HAD TO QUIT OUT TERRIBLE
      .

      Comment

      • SKG_Scintill
        Spun a twirly fruitcake,
        FFR Simfile Author
        • Feb 2009
        • 3875

        #4
        Re: [JAVA] Site Search Help

        It works when the word is on the first opened page, but when I tried to look for "Final Fantasy" it didn't println() and continued looping.

        The robot is to simulate button presses, I couldn't find a refresh action in the java.io.* or java.net.* so this was my way round.
        The delays are mostly so I can follow its progress, when it works I make it quicker
        Last edited by SKG_Scintill; 05-1-2012, 04:27 AM.





        Originally posted by bluguerilla
        So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
        ___
        . RHYTHMS PR LAYERING
        . ZOMG I HAD TO QUIT OUT TERRIBLE
        .

        Comment

        • UserNameGoesHere
          FFR Veteran
          • May 2008
          • 1114

          #5
          Re: [JAVA] Site Search Help

          Leave the delays in there. The delays are useful so you don't hammer the website. (At least that's why I assumed you had them).

          Well as-is the Robot is scripting some button presses but those have nothing whatsoever to do with the loading, reloading, or reading of that particular website. It really has nothing else to do with what you're trying to do (that I can see) and your program doesn't need it to automate either. I'd remove it. (replace the delay calls with Thread.sleep calls)
          Originally posted by Crashfan3
          Man, what would we do without bored rednecks?

          Comment

          • SKG_Scintill
            Spun a twirly fruitcake,
            FFR Simfile Author
            • Feb 2009
            • 3875

            #6
            Re: [JAVA] Site Search Help

            I don't know if you have looked at the site itself, but it randomly generates a couple of words. It's to keep refreshing and looking if it randomly generated a given word, such as "Final Fantasy".
            I know it's really long-winded to do it this way, but it's what I can understand with my current knowledge.





            Originally posted by bluguerilla
            So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
            ___
            . RHYTHMS PR LAYERING
            . ZOMG I HAD TO QUIT OUT TERRIBLE
            .

            Comment

            • UserNameGoesHere
              FFR Veteran
              • May 2008
              • 1114

              #7
              Re: [JAVA] Site Search Help

              Well yes I assume you wanted to stop once one page had your chosen words on it. That's why it stops after one page has the words. Did you want it to continue refreshing/printing indefinitely even after it found a page that had the words on it???
              Originally posted by Crashfan3
              Man, what would we do without bored rednecks?

              Comment

              • SKG_Scintill
                Spun a twirly fruitcake,
                FFR Simfile Author
                • Feb 2009
                • 3875

                #8
                Re: [JAVA] Site Search Help

                No, but it's doing that right now, after your adjustments :P

                If it's there on the first try, it prints. If it comes up on later tries, it continues looping. I don't want that xD

                (Looking for things I may have overlooked in your code)
                Last edited by SKG_Scintill; 05-1-2012, 04:44 AM.





                Originally posted by bluguerilla
                So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                ___
                . RHYTHMS PR LAYERING
                . ZOMG I HAD TO QUIT OUT TERRIBLE
                .

                Comment

                • UserNameGoesHere
                  FFR Veteran
                  • May 2008
                  • 1114

                  #9
                  Re: [JAVA] Site Search Help

                  Okay so state specifically, and clearly, in as precise of detail as you can, exactly what your program is supposed to do? That's the first step.

                  I can read the code and see what it actually does. I can try to infer or guess what you wanted it to do. But if my guess as to what you wanted is wrong, well my solution will implement my guess lol.

                  The clearer you explain it the better I can help you. :)
                  Originally posted by Crashfan3
                  Man, what would we do without bored rednecks?

                  Comment

                  • SKG_Scintill
                    Spun a twirly fruitcake,
                    FFR Simfile Author
                    • Feb 2009
                    • 3875

                    #10
                    Re: [JAVA] Site Search Help

                    The program is supposed to do this:

                    1. Switch from my programming software Eclipse to the site itself by simulating alt+tab.
                    2. Read the text on the given site.
                    3. Determine if a given word occurs in the text. (or given words occur)
                    4. If it occurs in the text, it's supposed to stop searching so I can read it. (The println() isn't necessary). The program ends here if this happens.
                    5. If it doesn't occur in the text, it's supposed to refresh the page. (Which I do by simulating the f5-button)
                    6. Go back to point 2.





                    Originally posted by bluguerilla
                    So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                    ___
                    . RHYTHMS PR LAYERING
                    . ZOMG I HAD TO QUIT OUT TERRIBLE
                    .

                    Comment

                    • UserNameGoesHere
                      FFR Veteran
                      • May 2008
                      • 1114

                      #11
                      Re: [JAVA] Site Search Help

                      So you want it to stop the very FIRST line it finds? If it finds ANY lines with desired text, print only the first of such then terminate program?
                      Originally posted by Crashfan3
                      Man, what would we do without bored rednecks?

                      Comment

                      • SKG_Scintill
                        Spun a twirly fruitcake,
                        FFR Simfile Author
                        • Feb 2009
                        • 3875

                        #12
                        Re: [JAVA] Site Search Help

                        yes





                        Originally posted by bluguerilla
                        So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                        ___
                        . RHYTHMS PR LAYERING
                        . ZOMG I HAD TO QUIT OUT TERRIBLE
                        .

                        Comment

                        • UserNameGoesHere
                          FFR Veteran
                          • May 2008
                          • 1114

                          #13
                          Re: [JAVA] Site Search Help

                          put a break after the System.out.println and you get that functionality then.
                          Originally posted by Crashfan3
                          Man, what would we do without bored rednecks?

                          Comment

                          • SKG_Scintill
                            Spun a twirly fruitcake,
                            FFR Simfile Author
                            • Feb 2009
                            • 3875

                            #14
                            Re: [JAVA] Site Search Help

                            Still continues searching after personally seeing "Final" come by O.o

                            Code:
                            public class URLReader{
                            	public URLReader() throws Exception{
                            		URL site = new URL("http://kaction.com/badfanfiction/");
                            		BufferedReader in = new BufferedReader(
                            				new InputStreamReader(site.openStream()));
                            		
                            		String inputLine;
                            		boolean found = false;
                            		while((inputLine = in.readLine()) != null){
                            			if(inputLine.contains("Final")){
                            				found = true;
                            				System.out.println(inputLine);
                            				break;
                            			}
                            		}
                            	if(!found){
                            			Robot s = new Robot();
                            			s.delay(2000);
                            			s.keyPress(KeyEvent.VK_F5);
                            			s.keyRelease(KeyEvent.VK_F5);
                            			s.delay(2000);
                            			URLReader a = new URLReader();
                            		}
                            	}
                            }





                            Originally posted by bluguerilla
                            So Sexy Robotnik (SKG_Scintill) {.0001/10} [--]
                            ___
                            . RHYTHMS PR LAYERING
                            . ZOMG I HAD TO QUIT OUT TERRIBLE
                            .

                            Comment

                            • UserNameGoesHere
                              FFR Veteran
                              • May 2008
                              • 1114

                              #15
                              Re: [JAVA] Site Search Help

                              That's because what you're seeing is what your Robot is doing (refreshing page in your browser) but what your program is searching is independent of that. That's what I was trying to get at earlier. ;-)

                              So it's really hitting up the website twice on each round. Once, to search it for what you want. Then a second time when your browser refreshes the page. The page you see and the page it searches are not the same or in any way connected.
                              Originally posted by Crashfan3
                              Man, what would we do without bored rednecks?

                              Comment

                              Working...