Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.MalformedURLException;
- import java.util.*;
- import com.gargoylesoftware.htmlunit.*;
- import com.gargoylesoftware.htmlunit.html.*;
- public class ChickenSmoothie {
- /* Each individual pet with constant ID in URL
- static final String <name> = VALUE;
- */
- //ONLINE: type of pet - w/ items: include ID - w/o items: add to count
- //OFFLINE: ignore
- static final String ONLINE = "your account to get HTML";
- static final String OFFLINE = "Don't have a pet yet";
- static final String STATICIMG = "<img src=\"https://static.chickensmoothie";
- static final String ITEMSIMG = "<img src=\"/pet/";
- static final String PETID = "Pet ID:";
- static final String PETIMG = "id=\"petimg\"";
- static final String DELETED = "Sorry, that pet could not be found. Maybe you can adopt a new pet instead :)";
- //Change this to determine where to start searching (1 = ID#1)
- static int pagenum = 37;
- //Change this to how many pages you want to look up (10 = 10 pages, RANGE: [1, 10])
- static final int numberOfPages = 15;
- static final int NUMPAGES = pagenum+numberOfPages;
- static final int start = pagenum;
- static final int end = (pagenum+numberOfPages-1);
- static int deletedpets = 0;
- static int count = 0;
- static int offlinepets = 0;
- static boolean hasItems = true;
- static ArrayList<Pet> static_images = new ArrayList<>();
- static ArrayList<Pet> item_images = new ArrayList<>();
- static ArrayList<String> deleted_pets = new ArrayList<>();
- static ArrayList<Pet> offline_pets = new ArrayList<>();
- static HashMap<String, String> directory = new HashMap<>();
- public static void hMap_create()
- {
- try
- {
- Scanner input = new Scanner(new File("CS_Code_Directory.txt"));
- input.useDelimiter("\n");
- while(input.hasNext())
- {
- String line = input.next();
- String value[];
- value = line.split(",");
- //System.out.printf("Key:%s Value:%s\n",value[1], value[0]);
- directory.put(value[1], value[0]);
- }
- input.close();
- }
- catch(IOException ioe)
- {
- ioe.printStackTrace();
- }
- }
- public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException
- {
- hMap_create();
- String image = "";
- try
- {
- while(pagenum<NUMPAGES)
- {
- Formatter output = new Formatter("CS_HTML_TXT.txt");
- String webpage = "https://www.chickensmoothie.com/viewpet.php?id="+Integer.toString(pagenum);
- WebClient webClient = new WebClient(BrowserVersion.CHROME);
- HtmlPage page = webClient.getPage(webpage);
- String pageContent = page.asXml();
- webClient.close();
- output.format("%s",pageContent);
- output.close();
- System.out.println("Looking up ID: "+Integer.toString(pagenum));
- Pet p1 = new Pet();
- Scanner input = new Scanner(new File("CS_HTML_TXT.txt"));
- while(input.hasNext())
- {
- String line = input.nextLine();
- String value[];
- p1.setID(Integer.toString(pagenum));
- if(line.contains(STATICIMG) && line.contains(PETIMG))
- {
- value = line.split("\"");
- image = value[1];
- hasItems = false;
- p1.setKey(image.substring(43,77));
- p1.setType(directory.get(p1.getKey()));
- }
- if(line.contains(ITEMSIMG) && line.contains(PETIMG))
- {
- value = line.split("\"");
- image = value[1];
- hasItems = true;
- p1.setKey(image);
- }
- if(!hasItems && line.contains(ONLINE))
- {
- static_images.add(p1);
- count++;
- }
- if(hasItems && line.contains(ONLINE))
- {
- item_images.add(p1);
- count++;
- }
- if(line.contains(DELETED))
- {
- deleted_pets.add(Integer.toString(pagenum));
- deletedpets++;
- }
- if(line.contains(OFFLINE))
- {
- // offline_pets.add(p1);
- offlinepets++;
- }
- }
- input.close();
- pagenum++;
- }
- System.out.println(static_images);
- System.out.println(item_images);
- System.out.println(deleted_pets);
- }
- catch(IOException ioe)
- {
- ioe.printStackTrace();
- System.out.println("File not found in the directory - check program file location");
- }
- output();
- }
- public static void output()
- {
- try
- {
- Formatter output1 = new Formatter("CS_PET_LIST.txt");
- output1.format("===================================== PETS W/O ITEMS =====================================%n%n%n");
- for(Pet p1:static_images)
- output1.format("ID: %s%nType: %s%nKey: %s%n%n",p1.getID(),p1.getType(),p1.getKey());
- output1.format("%n%n%n===================================== PETS W/ ITEMS =====================================%n%n%n");
- for(Pet p2:item_images)
- output1.format("ID: %s%nType: %s%nKey: %s%n%n",p2.getID(),p2.getType(),p2.getKey());
- //output1.format("%n%n%n===================================== OFF-ACCOUNT PETS =====================================%n%n%n");
- //for(Pet p3:offline_pets)
- // output1.format("ID: %s%nType: %s%nKey: %s%n%n",p3.getID(),p3.getType(),p3.getKey());
- output1.format("%n%n=====================================%n%n");
- 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));
- if(!(deleted_pets.isEmpty()))
- {
- output1.format("DELETED PETS: \t\t%d%n",deletedpets);
- // for(String s:deleted_pets)
- // output1.format("\t%s%n",s);
- }
- // else
- // output1.format("DELETED PETS: 0%n");
- output1.close();
- }
- catch(IOException ioe)
- {
- ioe.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement