Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. public class Main {
  2.  
  3.     static Map<String, Integer> list = new HashMap();
  4.     static Map<String, Integer> results = new HashMap();
  5.  
  6.     static BasicCookieStore cookieStore = new org.apache.http.impl.client.BasicCookieStore();
  7.  
  8.     public static void main(String[] args) throws UnirestException, IOException {
  9.  
  10.         HttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
  11.         Unirest.setHttpClient(client);
  12.  
  13.         addItem("000.891.63",1);
  14.         addItem("001.301.29",1);
  15.         addItem("001.301.67",1);
  16.  
  17.         int itemsCount = 0;
  18.  
  19.         for (Map.Entry<String, Integer> item : list.entrySet()) {
  20.             HttpResponse response = send(item.getKey(), item.getValue());
  21.             results.put(item.getKey(), response.getStatus());
  22.             System.out.println(item + " \t " + response.getStatus());
  23.             itemsCount += item.getValue();
  24.         }
  25.  
  26.         System.out.println("Items count: " + itemsCount);
  27.         System.out.println("List size: " + list.size());
  28.  
  29.     }
  30.  
  31.     public static void addItem(String number, Integer count) {
  32.         if(count == 0) return;
  33.         if (list.containsKey(number)) {
  34.             list.put(number, list.get(number) + count);
  35.             return;
  36.         }
  37.         list.put(number, count);
  38.     }
  39.  
  40.     public static String parse(String number, int count) throws IOException {
  41.         String location = "http://www.ikea.com/ru/ru/catalog/products/" + number.replace(".", "") + "/";
  42.         Connection connection = Jsoup.connect(location);
  43.         connection.request().ignoreHttpErrors(true);
  44.         Document doc = connection.get();
  45.         if (doc.location().equals(location) && doc.getElementsByClass("errorHead").isEmpty()) {
  46.             String name = doc.getElementById("name").text();
  47.             String type = doc.getElementById("type").text();
  48.             String price = doc.getElementById("price1").text().replace(".–", "");
  49.             Element ifPrice = doc.getElementById("price5");
  50.             if(ifPrice != null && ifPrice.hasText()) price = ifPrice.text().replace(".–", "");
  51.             System.out.println(number + "\t" + name + " " + type + "\t" + price.replace(" ", "").replace(" ", "") + "\t" + count);
  52.         } else if (!number.contains("S")) {
  53.             parse("S" + number, count);
  54.         } else {
  55.             System.out.println(number + "\tНе найден");
  56.         }
  57.         return "";
  58.     }
  59.  
  60.     public static HttpResponse send(String number, Integer count) throws UnirestException {
  61.         HttpResponse response = Unirest.post("http://www.ikea.com/webapp/wcs/stores/servlet/IrwInterestItemAddByPartNumber")
  62.                 .header("content-type", "application/x-www-form-urlencoded")
  63.                 .field("quantity", count, "application/x-www-form-urlencoded")
  64.                 .field("partNumber", number, "application/x-www-form-urlencoded")
  65.                 .field("listId", "", "application/x-www-form-urlencoded")
  66.                 .asString();
  67.         return response;
  68.     }
  69.  
  70.     public static void login() throws UnirestException {
  71.         String user = "";
  72.         String password = "";
  73.         Unirest.post("https://secure.ikea.com/webapp/wcs/stores/servlet/Logon")
  74.                 .header("content-type", "application/x-www-form-urlencoded")
  75.                 .field("logonId", user)
  76.                 .field("logonPassword", password)
  77.                 .field("storeId", 23)
  78.                 .asString();
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement