Advertisement
Mihael

Untitled

Apr 3rd, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static void downloadItems(final String hrefParent, final String URL, final DownloadListener dl)
  2. {
  3.     new Thread(new Runnable() {
  4.         @Override
  5.         public void run() {
  6.             Map<String, Object> result = new HashMap<String, Object>();
  7.             JSONArray items = new JSONArray();
  8.             JSONArray filters = new JSONArray();
  9.             dl.downloadStart();
  10.             String responseHTML = new Request(URL).execute();
  11.             Document doc = Jsoup.parse(responseHTML);
  12.             Element priceListNode = doc.getElementsByClass("price_list").first();
  13.             if(priceListNode!=null)
  14.                 for(Element priceTableNode : priceListNode.select(".mode_1"))
  15.                     for(Element priceTableTRNode : priceTableNode.select("tr"))
  16.                     {
  17.                         String trClass = priceTableTRNode.attr("class");
  18.                         String trPreview = priceTableTRNode.attr("preview");
  19.                         if(trClass!=null && trClass.equals("section"))
  20.                         {
  21.                             try {
  22.                                 JSONObject jsonObj = new JSONObject();
  23.                                 Element sectionNameNode = priceTableTRNode.select(".t").first();
  24.                                 String sectionName = sectionNameNode.text();
  25.                                 jsonObj.put(Def.kItemHrefParent, hrefParent);
  26.                                 jsonObj.put(Def.kItemsSectionName, sectionName);
  27.                                 jsonObj.put(Def.kItemsSectionItems, new JSONArray());
  28.                                 items.put(jsonObj);
  29.                             } catch (Exception e) {
  30.                                 Log.d("coccccccc", "exc sect: "+e.getLocalizedMessage());
  31.                             }
  32.                         }
  33.                         else if(((trClass!=null && trClass.length()>0) && (trClass.equals("first") || trClass.equals("odd")))
  34.                                 || (trPreview!=null && trPreview.length()>0)
  35.                                 || (priceTableTRNode.select(".c").first()!=null
  36.                                     && !priceTableTRNode.select(".c").first().text().equals("Код")) && items.length()>0)
  37.                         {
  38.                             //Код
  39.                             Element codeNode = priceTableTRNode.select(".c").first();
  40.                             String code = codeNode.text();
  41.                             //Комментарии
  42.                             Element commentsNode = priceTableTRNode.select(".co").first();
  43.                             String comments = commentsNode.text();
  44.                             //Рейтинг
  45.                             String rating = "0";
  46.                             Element ratingNode = priceTableTRNode.select(".r").first();
  47.                             Element ratingANode = ratingNode.select("a").first();
  48.                             if(ratingANode!=null)
  49.                             {
  50.                                 String[] splitRating = ratingANode.attr("title").split("от");
  51.                                 if(splitRating.length>0)
  52.                                     rating = splitRating[0].trim().replace(",",".");
  53.                             }
  54.                             //Название
  55.                             Element nameNode = priceTableTRNode.select(".t").first();
  56.                             String name = nameNode.text().replace("Помощь эксперта", "").trim();
  57.                             //Картинка
  58.                             String imageURL = (trPreview!=null)?trPreview:"";
  59.                             //Цена
  60.                             Element priceNode = priceTableTRNode.select(".p").first();
  61.                             String price = (priceNode!=null)?priceNode.text().replace(" ", "").trim():"";
  62.                             for(Element priceDIVNode : priceNode.select("div"))
  63.                                 if(priceDIVNode.attr("class")==null
  64.                                 || priceDIVNode.attr("class").length()==0
  65.                                 || priceDIVNode.attr("class").equals("new"))
  66.                                     price = (priceDIVNode!=null)?priceDIVNode.text().replace(" ", "").trim():"";
  67.                                try {
  68.                                 JSONArray sectionItems = ((JSONObject)items.get(items.length()-1)).getJSONArray(Def.kItemsSectionItems);
  69.                                 JSONObject jsonObj = new JSONObject();
  70.                                 jsonObj.put(Def.kItemHrefParent, hrefParent);
  71.                                 jsonObj.put(Def.kItemCode, code);
  72.                                 jsonObj.put(Def.kItemComments, comments);
  73.                                 jsonObj.put(Def.kItemGrade, rating);
  74.                                 jsonObj.put(Def.kItemName, name);
  75.                                 jsonObj.put(Def.kItemImageURL, imageURL);
  76.                                 jsonObj.put(Def.kItemPrice, price);
  77.                                 sectionItems.put(jsonObj);
  78.                             } catch (Exception e) {
  79.                                 Log.d("coccccccc", "exc item: "+e.getLocalizedMessage());
  80.                             }                              
  81.                         }
  82.                     }
  83.             //TODO: заполнить фильтры
  84.            
  85.             result.put(Def.kItemHrefParent, hrefParent);
  86.             result.put(Def.kItemsURL, URL);
  87.             result.put(Def.kItems, items);
  88.             result.put(Def.kItemsFilters, filters);
  89.             dl.downloadComplete(result);
  90.         }
  91.     }).start();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement