Advertisement
Mihael

Untitled

Apr 3rd, 2013
52
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.             Elements priceListNodes = doc.getElementsByClass("price_list:eq(0)");
  13.             Element priceListNode = priceListNodes.first();
  14.             if(priceListNode!=null)
  15.             {
  16.                 Elements mode1Nodes = priceListNode.select(".mode_1");
  17.                 for(int i=0;i<mode1Nodes.size();i++)
  18.                 {
  19.                     Element priceTableNode = mode1Nodes.get(i);
  20.                     if(priceTableNode!=null)
  21.                     {
  22.                         Elements priceTableTRNodes = priceTableNode.select("tr");
  23.                         for(int n=0;n<priceTableTRNodes.size();n++)
  24.                         {
  25.                             Element priceTableTRNode = priceTableTRNodes.get(n);
  26.                             if(priceTableTRNode!=null)
  27.                             {
  28.                                 String trClass = priceTableTRNode.attr("class");
  29.                                 String trPreview = priceTableTRNode.attr("preview");
  30.                                 if(trClass!=null && trClass.equals("section"))
  31.                                 {
  32.                                     try {
  33.                                         JSONObject jsonObj = new JSONObject();
  34.                                         Element sectionNameNode = priceTableTRNode.getElementsByClass("t:eq(0)").first();
  35.                                         String sectionName = sectionNameNode.text();
  36.                                         jsonObj.put(Def.kItemHrefParent, hrefParent);
  37.                                         jsonObj.put(Def.kItemsSectionName, sectionName);
  38.                                         jsonObj.put(Def.kItemsSectionItems, new JSONArray());
  39.                                         items.put(jsonObj);
  40.                                     } catch (Exception e) {
  41.                                         Log.d("coccccccc", "exc sect: "+e.getLocalizedMessage());
  42.                                     }
  43.                                 }
  44.                                 else if(((trClass!=null && trClass.length()>0) && (trClass.equals("first") || trClass.equals("odd")))
  45.                                         || (trPreview!=null && trPreview.length()>0)
  46.                                         || (priceTableTRNode.select(".c").first()!=null
  47.                                             && !priceTableTRNode.select(".c").first().text().equals("Код")) && items.length()>0)
  48.                                 {
  49.                                     //Код
  50.                                     Element codeNode = priceTableTRNode.getElementsByClass("c:eq(0)").first();
  51.                                     String code = codeNode.text();
  52.                                     //Комментарии
  53.                                     Element commentsNode = priceTableTRNode.getElementsByClass("co:eq(0)").first();
  54.                                     String comments = commentsNode.text();
  55.                                     //Рейтинг
  56.                                     String rating = "0";
  57.                                     Element ratingNode = priceTableTRNode.getElementsByClass("r:eq(0)").first();
  58.                                     Element ratingANode = ratingNode.getElementsByTag("a:eq(0)").first();
  59.                                     if(ratingANode!=null)
  60.                                     {
  61.                                         String[] splitRating = ratingANode.attr("title").split("от");
  62.                                         if(splitRating.length>0)
  63.                                             rating = splitRating[0].trim().replace(",",".");
  64.                                     }
  65.                                     //Название
  66.                                     Element nameNode = priceTableTRNode.getElementsByClass("t:eq(0)").first();
  67.                                     String name = nameNode.text().replace("Помощь эксперта", "").trim();
  68.                                     //Картинка
  69.                                     String imageURL = (trPreview!=null)?trPreview:"";
  70.                                     //Цена
  71.                                     Element priceNode = priceTableTRNode.getElementsByClass("p:eq(0)").first();
  72.                                     String price = (priceNode!=null)?priceNode.text().replace(" ", "").trim():"";
  73.                                     for(Element priceDIVNode : priceNode.select("div"))
  74.                                         if(priceDIVNode.attr("class")==null
  75.                                         || priceDIVNode.attr("class").length()==0
  76.                                         || priceDIVNode.attr("class").equals("new"))
  77.                                             price = (priceDIVNode!=null)?priceDIVNode.text().replace(" ", "").trim():"";
  78.                                     try {
  79.                                         JSONArray sectionItems = ((JSONObject)items.get(items.length()-1)).getJSONArray(Def.kItemsSectionItems);
  80.                                         JSONObject jsonObj = new JSONObject();
  81.                                         jsonObj.put(Def.kItemHrefParent, hrefParent);
  82.                                         jsonObj.put(Def.kItemCode, code);
  83.                                         jsonObj.put(Def.kItemComments, comments);
  84.                                         jsonObj.put(Def.kItemGrade, rating);
  85.                                         jsonObj.put(Def.kItemName, name);
  86.                                         jsonObj.put(Def.kItemImageURL, imageURL);
  87.                                         jsonObj.put(Def.kItemPrice, price);
  88.                                         sectionItems.put(jsonObj);
  89.                                     } catch (Exception e) {
  90.                                         Log.d("coccccccc", "exc item: "+e.getLocalizedMessage());
  91.                                     }                              
  92.                                 }
  93.                             }
  94.                         }
  95.                     }
  96.                 }
  97.             }
  98.             //TODO: заполнить фильтры
  99.            
  100.             result.put(Def.kItemHrefParent, hrefParent);
  101.             result.put(Def.kItemsURL, URL);
  102.             result.put(Def.kItems, items);
  103.             result.put(Def.kItemsFilters, filters);
  104.             dl.downloadComplete(result);
  105.         }
  106.     }).start();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement