Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void downloadItems(final String hrefParent, final String URL, final DownloadListener dl)
- {
- new Thread(new Runnable() {
- @Override
- public void run() {
- Map<String, Object> result = new HashMap<String, Object>();
- JSONArray items = new JSONArray();
- JSONArray filters = new JSONArray();
- dl.downloadStart();
- String responseHTML = new Request(URL).execute();
- Document doc = Jsoup.parse(responseHTML);
- Elements priceListNodes = doc.getElementsByClass("price_list:eq(0)");
- Element priceListNode = priceListNodes.first();
- if(priceListNode!=null)
- {
- Elements mode1Nodes = priceListNode.select(".mode_1");
- for(int i=0;i<mode1Nodes.size();i++)
- {
- Element priceTableNode = mode1Nodes.get(i);
- if(priceTableNode!=null)
- {
- Elements priceTableTRNodes = priceTableNode.select("tr");
- for(int n=0;n<priceTableTRNodes.size();n++)
- {
- Element priceTableTRNode = priceTableTRNodes.get(n);
- if(priceTableTRNode!=null)
- {
- String trClass = priceTableTRNode.attr("class");
- String trPreview = priceTableTRNode.attr("preview");
- if(trClass!=null && trClass.equals("section"))
- {
- try {
- JSONObject jsonObj = new JSONObject();
- Element sectionNameNode = priceTableTRNode.getElementsByClass("t:eq(0)").first();
- String sectionName = sectionNameNode.text();
- jsonObj.put(Def.kItemHrefParent, hrefParent);
- jsonObj.put(Def.kItemsSectionName, sectionName);
- jsonObj.put(Def.kItemsSectionItems, new JSONArray());
- items.put(jsonObj);
- } catch (Exception e) {
- Log.d("coccccccc", "exc sect: "+e.getLocalizedMessage());
- }
- }
- else if(((trClass!=null && trClass.length()>0) && (trClass.equals("first") || trClass.equals("odd")))
- || (trPreview!=null && trPreview.length()>0)
- || (priceTableTRNode.select(".c").first()!=null
- && !priceTableTRNode.select(".c").first().text().equals("Код")) && items.length()>0)
- {
- //Код
- Element codeNode = priceTableTRNode.getElementsByClass("c:eq(0)").first();
- String code = codeNode.text();
- //Комментарии
- Element commentsNode = priceTableTRNode.getElementsByClass("co:eq(0)").first();
- String comments = commentsNode.text();
- //Рейтинг
- String rating = "0";
- Element ratingNode = priceTableTRNode.getElementsByClass("r:eq(0)").first();
- Element ratingANode = ratingNode.getElementsByTag("a:eq(0)").first();
- if(ratingANode!=null)
- {
- String[] splitRating = ratingANode.attr("title").split("от");
- if(splitRating.length>0)
- rating = splitRating[0].trim().replace(",",".");
- }
- //Название
- Element nameNode = priceTableTRNode.getElementsByClass("t:eq(0)").first();
- String name = nameNode.text().replace("Помощь эксперта", "").trim();
- //Картинка
- String imageURL = (trPreview!=null)?trPreview:"";
- //Цена
- Element priceNode = priceTableTRNode.getElementsByClass("p:eq(0)").first();
- String price = (priceNode!=null)?priceNode.text().replace(" ", "").trim():"";
- for(Element priceDIVNode : priceNode.select("div"))
- if(priceDIVNode.attr("class")==null
- || priceDIVNode.attr("class").length()==0
- || priceDIVNode.attr("class").equals("new"))
- price = (priceDIVNode!=null)?priceDIVNode.text().replace(" ", "").trim():"";
- try {
- JSONArray sectionItems = ((JSONObject)items.get(items.length()-1)).getJSONArray(Def.kItemsSectionItems);
- JSONObject jsonObj = new JSONObject();
- jsonObj.put(Def.kItemHrefParent, hrefParent);
- jsonObj.put(Def.kItemCode, code);
- jsonObj.put(Def.kItemComments, comments);
- jsonObj.put(Def.kItemGrade, rating);
- jsonObj.put(Def.kItemName, name);
- jsonObj.put(Def.kItemImageURL, imageURL);
- jsonObj.put(Def.kItemPrice, price);
- sectionItems.put(jsonObj);
- } catch (Exception e) {
- Log.d("coccccccc", "exc item: "+e.getLocalizedMessage());
- }
- }
- }
- }
- }
- }
- }
- //TODO: заполнить фильтры
- result.put(Def.kItemHrefParent, hrefParent);
- result.put(Def.kItemsURL, URL);
- result.put(Def.kItems, items);
- result.put(Def.kItemsFilters, filters);
- dl.downloadComplete(result);
- }
- }).start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement