NoNameCracksInc

JSoup web parser - P4rse

Aug 24th, 2016
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.00 KB | None | 0 0
  1. /* Hey folks. I decided to share with you this java website parser I made to make my web developing easier. If you need to generate SQL queries dynamically based on webpage indexing, or output website's source code into textfile, or even choose certain node based on class / id and dump it's content into text file, you might find this tool useful. You will need JSoup library to run this and if you forge it into executable JAR file, then all text files are generated in directory of JAR. */
  2.  
  3. import org.jsoup.*;
  4. import java.io.*;
  5. import java.net.MalformedURLException;
  6. import java.net.UnknownHostException;
  7. import java.util.Random;
  8. import org.jsoup.nodes.Document;
  9. import org.jsoup.select.Elements;
  10.  
  11. public class P4rse
  12. {
  13.     private static Document target;
  14.     private static boolean connected = false;
  15.     private static String[] command_collection = new String[]{"connect","disconnect","sql","author","srcout","nodetext","exit"};
  16.    
  17.     public static void main(String args[]) throws IOException
  18.     {
  19.         System.out.println("P4rse - visit websites not...\n");
  20.         P4rse.cycle();
  21.     }
  22.    
  23.     public static void printwrite(String name, String[] values) throws IOException
  24.     {
  25.         PrintWriter writer;
  26.         name = name.replace("www","").replace(".","").replace("http://","");
  27.        
  28.         System.out.println("Generating file...");
  29.        
  30.         try
  31.         {
  32.             writer = new PrintWriter(name+".txt", "UTF-8");
  33.         }
  34.         catch(FileNotFoundException e)
  35.         {
  36.             Random rn = new Random();
  37.             writer = new PrintWriter((10000 + rn.nextInt(100000 - 10000 + 1))+".txt", "UTF-8");
  38.         }
  39.          
  40.            System.out.println("Writing data...");
  41.            for(int i=0;i<=values.length-1;i++)
  42.            {
  43.                if(values[i] != null)
  44.                 writer.println(values[i]);
  45.                else
  46.                 continue;
  47.            }
  48.          
  49.           writer.close();  
  50.     }
  51.    
  52.     public static boolean isNumeric(String str)
  53.     {
  54.         for (char c : str.toCharArray())
  55.         {
  56.             if (!Character.isDigit(c)) return false;
  57.         }
  58.         return true;
  59.     }
  60.    
  61.     static int getTrailingInteger(String str)
  62.     {
  63.         int positionOfLastDigit = getPositionOfLastDigit(str);
  64.         if (positionOfLastDigit == str.length())
  65.         {
  66.             // string does not end in digits
  67.             return -1;
  68.         }
  69.         return Integer.parseInt(str.substring(positionOfLastDigit));
  70.     }
  71.  
  72.     static int getPositionOfLastDigit(String str)
  73.     {
  74.         int pos;
  75.         for (pos=str.length()-1; pos>=0; --pos)
  76.         {
  77.             char c = str.charAt(pos);
  78.             if (!Character.isDigit(c)) break;
  79.         }
  80.         return pos + 1;
  81.     }
  82.    
  83.     private static boolean connect(String url) throws IOException
  84.     {
  85.         System.out.println("Connecting...");
  86.        
  87.         try
  88.         {
  89.             try
  90.             {
  91.                 target = Jsoup.connect(url).get();
  92.             }catch(UnknownHostException ex){System.out.println("No internet connection.");return false;}
  93.         }catch(IllegalArgumentException|MalformedURLException e){System.out.println("Wrong URL format.");return false;}
  94.        
  95.          return true;
  96.     }
  97.    
  98.     private static void cycle() throws IOException
  99.     {
  100.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  101.        
  102.         outerloop:
  103.         while(true)
  104.         {  
  105.             System.out.print("P4rse > ");
  106.             String command = reader.readLine();
  107.            
  108.             switch(command)
  109.             {
  110.            
  111.             case "":
  112.                 continue;
  113.                
  114.              case "exit":
  115.                 System.out.println("P4rse - visit websites not...");
  116.                 break outerloop;
  117.                
  118.              case "connect":
  119.                  
  120.                  if(!P4rse.connected)
  121.                  {
  122.                      System.out.print("Target: ");
  123.                        
  124.                        String tgt = reader.readLine();
  125.                  
  126.                        if(P4rse.connect(tgt))
  127.                        {
  128.                            System.out.println("Connected to ("+tgt+").");
  129.                            P4rse.connected = true;
  130.                        }
  131.                    
  132.                        else
  133.                        {
  134.                            break;
  135.                        }
  136.                   }
  137.                  
  138.                  else
  139.                  {
  140.                      System.out.println("You have to disconnect first.");
  141.                  }
  142.              break;
  143.                  
  144.              case "srcout":
  145.                    if(P4rse.connected)
  146.                    {
  147.                        printwrite(target.location()+" source",new String[]{target.html()});
  148.                        System.out.println("OK.");
  149.                    }
  150.                  
  151.                    else
  152.                    {
  153.                        System.out.println("You need to connect to target first.");
  154.                    }
  155.              break;
  156.              
  157.              case "nodetext":
  158.                  
  159.                    if(P4rse.connected)
  160.                    {
  161.                        System.out.print("Insert nodecode: ");
  162.                        String nodecode = reader.readLine();
  163.                        Elements node = target.select(nodecode);
  164.                        printwrite(target.location()+" node text",new String[]{node.text()});
  165.                        System.out.println("OK.");
  166.                    }
  167.                    
  168.                    else
  169.                    {
  170.                        System.out.println("You need to connect to target first.");
  171.                    }
  172.              break;
  173.              
  174.              case "sql":
  175.                  
  176.                    if(P4rse.connected)
  177.                    {
  178.                        System.out.print("Choose type: ");
  179.                        String type = reader.readLine();
  180.                        
  181.                        switch(type)
  182.                        {
  183.                         case "insert":
  184.                             String lastchar = target.location().substring(target.location().length()-1, target.location().length());
  185.                            
  186.                                 System.out.println("Possible url increment: "+isNumeric(lastchar)+".");
  187.                                
  188.                                 System.out.print("Table name: ");
  189.                                 String table = reader.readLine();
  190.                                 System.out.print("Records amount: ");
  191.                                 int records = Integer.parseInt(reader.readLine());
  192.                                
  193.                                  String[] nodecode_set = new String[10];
  194.                                
  195.                                  for(int i=0;i<=records-1;i++)
  196.                                  {
  197.                                      System.out.print("Record "+i+" nodecode: ");
  198.                                      nodecode_set[i] = reader.readLine();
  199.                                  }
  200.                                
  201.                                
  202.                                  if(isNumeric(lastchar))
  203.                                  {
  204.                                       int urlinc = getTrailingInteger(target.location());
  205.                                      
  206.                                       System.out.print("Delimiter: "+urlinc+" to ");
  207.                                       int delimiter = Integer.parseInt(reader.readLine());
  208.                                      
  209.                                       String location_model = target.location().replace(Integer.toString(urlinc),"");
  210.                                       System.out.println("Starting simulation model...");
  211.                                      
  212.                                       Thread t1 = new Thread(new Runnable()
  213.                                       {
  214.                                        public void run()
  215.                                        {
  216.                                            System.out.println("Simulation model started.");
  217.                                            
  218.                                            Document document_model;
  219.                                            String[] query_storage = new String[((urlinc)-delimiter)+1];
  220.                                            String query = "";
  221.                                            int inc = -1;
  222.                                            
  223.                                            for(int i=urlinc;i>=delimiter;i--)
  224.                                            {
  225.                                                
  226.                                               try
  227.                                               {
  228.                                                  document_model = Jsoup.connect(location_model+i).get();
  229.                                                  
  230.                                                   System.out.println("Processing dummy target...");
  231.                                                  
  232.                                                      inc++;
  233.                                                  
  234.                                                  for(int q=0;q<=records-1;q++)
  235.                                                  {
  236.                                                      String text = document_model.select(nodecode_set[q]).text();
  237.                                                      query = query + "'" + text + "',";
  238.                                                  }
  239.                                                  
  240.                                                  
  241.                                                  query = query.replaceAll(",$","");
  242.                                                  String query_final = "INSERT INTO "+table+" VALUES("+query+");";
  243.                                                  query_storage[inc] = query_final;
  244.                                                  
  245.                                                   query = "";
  246.                                                
  247.                                               }catch(IOException e){continue;}
  248.                                            }
  249.                                              try
  250.                                              {
  251.                                                 printwrite(target.location()+" sql set",query_storage);
  252.                                              }catch(IOException e){e.printStackTrace();}
  253.                                              
  254.                                              System.out.println("OK.");
  255.                                              System.out.print("Press enter to continue...");
  256.                                        }});
  257.                                      
  258.                                       t1.start();
  259.                                  }
  260.                                  
  261.                                  else
  262.                                  {
  263.                                      String query = "";
  264.                                      
  265.                                      for(int i=0;i<=records-1;i++)
  266.                                      {
  267.                                          query = query + "'" + target.select(nodecode_set[i]).text() + "',";
  268.                                      }
  269.                                      
  270.                                      query = query.replaceAll(",$","");
  271.                                      String query_final = "INSERT INTO "+table+" VALUES("+query+");";
  272.                                      printwrite(target.location()+" sql set",new String[]{query_final});
  273.                                      System.out.println("OK.");
  274.                                  }
  275.                            
  276.                         break;
  277.                        }
  278.                    }
  279.                    
  280.                    else
  281.                    {
  282.                        System.out.println("You need to connect to target first.");
  283.                    }
  284.              break;
  285.              
  286.              case "disconnect":
  287.                    
  288.                    if(P4rse.connected)
  289.                    {
  290.                        P4rse.connected = false;
  291.                        P4rse.target = null;
  292.                        System.out.println("Disconnected.");
  293.                    }
  294.                    
  295.                    else
  296.                    {
  297.                        System.out.println("You are not conneced yet.");
  298.                    }
  299.                    
  300.              break;
  301.              
  302.              case "author":
  303.                    System.out.println("Created by Andrej Noskaj 06.07 2016.");
  304.              break;
  305.              
  306.              case "commands":
  307.                    for(int i=0;i<=command_collection.length-1;i++)
  308.                    {
  309.                        System.out.println(command_collection[i]);
  310.                    }
  311.              break;
  312.              
  313.              default:
  314.                    System.out.println("No such command. Try to type 'commands' if you struggle.");
  315.              break;
  316.              
  317.             }
  318.         }
  319.     }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment