Advertisement
Guest User

Untitled

a guest
Aug 11th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.52 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.MalformedURLException;
  3. import java.util.*;
  4. import com.gargoylesoftware.htmlunit.*;
  5. import com.gargoylesoftware.htmlunit.html.*;
  6.  
  7. public class ChickenSmoothie {
  8.     /*  Each individual pet with constant ID in URL
  9.     static final String <name> = VALUE;
  10.     */
  11.    
  12.     //ONLINE: type of pet - w/ items: include ID - w/o items: add to count
  13.     //OFFLINE: ignore
  14.     static final String ONLINE = "your account to get HTML";
  15.     static final String OFFLINE = "Don't have a pet yet";
  16.     static final String STATICIMG = "<img src=\"https://static.chickensmoothie";
  17.     static final String ITEMSIMG = "<img src=\"/pet/";
  18.     static final String PETID = "Pet ID:";
  19.     static final String PETIMG = "id=\"petimg\"";
  20.     static final String DELETED = "Sorry, that pet could not be found. Maybe you can adopt a new pet instead :)";
  21.    
  22.     //Change this to determine where to start searching (1 = ID#1)
  23.     static int pagenum = 37;
  24.    
  25.     //Change this to how many pages you want to look up (10 = 10 pages, RANGE: [1, 10])
  26.     static final int numberOfPages = 15;
  27.    
  28.     static final int NUMPAGES = pagenum+numberOfPages;
  29.     static final int start = pagenum;
  30.     static final int end = (pagenum+numberOfPages-1);
  31.     static int deletedpets = 0;
  32.     static int count = 0;
  33.     static int offlinepets = 0;
  34.     static boolean hasItems = true;
  35.     static ArrayList<Pet> static_images = new ArrayList<>();
  36.     static ArrayList<Pet> item_images = new ArrayList<>();
  37.     static ArrayList<String> deleted_pets = new ArrayList<>();
  38.     static ArrayList<Pet> offline_pets = new ArrayList<>();
  39.     static HashMap<String, String> directory = new HashMap<>();
  40.  
  41.     public static void hMap_create()
  42.     {
  43.         try
  44.         {
  45.             Scanner input = new Scanner(new File("CS_Code_Directory.txt"));
  46.             input.useDelimiter("\n");
  47.             while(input.hasNext())
  48.             {
  49.                 String line = input.next();
  50.                 String value[];
  51.                
  52.                 value = line.split(",");
  53.                 //System.out.printf("Key:%s Value:%s\n",value[1], value[0]);
  54.                 directory.put(value[1], value[0]);
  55.             }
  56.             input.close();
  57.         }
  58.         catch(IOException ioe)
  59.         {
  60.             ioe.printStackTrace();
  61.         }
  62.     }
  63.    
  64.    
  65.     public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException
  66.     {
  67.         hMap_create();
  68.        
  69.         String image = "";
  70.         try
  71.         {
  72.             while(pagenum<NUMPAGES)
  73.             {
  74.                 Formatter output = new Formatter("CS_HTML_TXT.txt");
  75.                 String webpage = "https://www.chickensmoothie.com/viewpet.php?id="+Integer.toString(pagenum);
  76.                 WebClient webClient = new WebClient(BrowserVersion.CHROME);
  77.                 HtmlPage page = webClient.getPage(webpage);
  78.                 String pageContent = page.asXml();
  79.                 webClient.close();
  80.                 output.format("%s",pageContent);
  81.                 output.close();
  82.                
  83.                
  84.                 System.out.println("Looking up ID: "+Integer.toString(pagenum));
  85.                 Pet p1 = new Pet();
  86.                 Scanner input = new Scanner(new File("CS_HTML_TXT.txt"));
  87.                 while(input.hasNext())
  88.                 {
  89.                     String line = input.nextLine();
  90.                     String value[];
  91.                     p1.setID(Integer.toString(pagenum));
  92.                    
  93.                     if(line.contains(STATICIMG) && line.contains(PETIMG))
  94.                     {
  95.                         value = line.split("\"");
  96.                         image = value[1];
  97.                         hasItems = false;
  98.                         p1.setKey(image.substring(43,77));
  99.                         p1.setType(directory.get(p1.getKey()));
  100.                     }
  101.                     if(line.contains(ITEMSIMG) && line.contains(PETIMG))
  102.                     {
  103.                         value = line.split("\"");
  104.                         image = value[1];
  105.                         hasItems = true;
  106.                         p1.setKey(image);
  107.                     }
  108.                     if(!hasItems && line.contains(ONLINE))
  109.                     {
  110.                         static_images.add(p1);
  111.                         count++;
  112.                     }
  113.                     if(hasItems && line.contains(ONLINE))
  114.                     {
  115.                         item_images.add(p1);
  116.                         count++;
  117.                     }
  118.                     if(line.contains(DELETED))
  119.                     {
  120.                         deleted_pets.add(Integer.toString(pagenum));
  121.                         deletedpets++;
  122.                     }
  123.                     if(line.contains(OFFLINE))
  124.                     {
  125. //                      offline_pets.add(p1);
  126.                         offlinepets++;
  127.                     }
  128.                 }
  129.                 input.close();
  130.                 pagenum++;
  131.             }
  132.            
  133.             System.out.println(static_images);
  134.             System.out.println(item_images);
  135.             System.out.println(deleted_pets);
  136.         }
  137.         catch(IOException ioe)
  138.         {
  139.             ioe.printStackTrace();
  140.             System.out.println("File not found in the directory - check program file location");
  141.         }
  142.        
  143.         output();
  144.     }
  145.    
  146.     public static void output()
  147.     {
  148.         try
  149.         {
  150.             Formatter output1 = new Formatter("CS_PET_LIST.txt");
  151.             output1.format("===================================== PETS W/O ITEMS =====================================%n%n%n");
  152.             for(Pet p1:static_images)
  153.                 output1.format("ID: %s%nType: %s%nKey: %s%n%n",p1.getID(),p1.getType(),p1.getKey());
  154.             output1.format("%n%n%n===================================== PETS W/ ITEMS =====================================%n%n%n");
  155.             for(Pet p2:item_images)
  156.                 output1.format("ID: %s%nType: %s%nKey: %s%n%n",p2.getID(),p2.getType(),p2.getKey());
  157.             //output1.format("%n%n%n===================================== OFF-ACCOUNT PETS =====================================%n%n%n");
  158.             //for(Pet p3:offline_pets)
  159.             //  output1.format("ID: %s%nType: %s%nKey: %s%n%n",p3.getID(),p3.getType(),p3.getKey());
  160.            
  161.             output1.format("%n%n=====================================%n%n");
  162.             output1.format("RANGE: \t[%d, %d]%nTOTAL: \t\t\t%d%n%nON-ACCOUNT PETS: \t%d%nOFF-ACCOUNT PETS: \t%d%n",start,end,numberOfPages,count,(numberOfPages-count-deletedpets));
  163.             if(!(deleted_pets.isEmpty()))
  164.             {
  165.                 output1.format("DELETED PETS: \t\t%d%n",deletedpets);
  166. //              for(String s:deleted_pets)
  167. //                  output1.format("\t%s%n",s);
  168.             }
  169. //          else
  170. //              output1.format("DELETED PETS: 0%n");
  171.             output1.close();
  172.         }
  173.         catch(IOException ioe)
  174.         {
  175.             ioe.printStackTrace();
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement