View difference between Paste ID: JMJZ5sS8 and PBB5yrz2
SHOW: | | - or go back to the newest paste.
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();
12+
			Elements priceListNodes = doc.getElementsByClass("price_list:eq(0)"); 
13
			Element priceListNode = priceListNodes.first();
14-
				for(Element priceTableNode : priceListNode.select(".mode_1"))
14+
15-
					for(Element priceTableTRNode : priceTableNode.select("tr"))
15+
			{
16
				Elements mode1Nodes = priceListNode.select(".mode_1");
17-
						String trClass = priceTableTRNode.attr("class");
17+
				for(int i=0;i<mode1Nodes.size();i++)
18-
						String trPreview = priceTableTRNode.attr("preview");
18+
				{
19-
						if(trClass!=null && trClass.equals("section"))
19+
					Element priceTableNode = mode1Nodes.get(i);
20
					if(priceTableNode!=null)
21-
							try {
21+
22-
								JSONObject jsonObj = new JSONObject();
22+
						Elements priceTableTRNodes = priceTableNode.select("tr");
23-
								Element sectionNameNode = priceTableTRNode.select(".t").first();
23+
						for(int n=0;n<priceTableTRNodes.size();n++)
24-
								String sectionName = sectionNameNode.text();
24+
25-
								jsonObj.put(Def.kItemHrefParent, hrefParent);
25+
							Element priceTableTRNode = priceTableTRNodes.get(n);
26-
								jsonObj.put(Def.kItemsSectionName, sectionName);
26+
							if(priceTableTRNode!=null)
27-
								jsonObj.put(Def.kItemsSectionItems, new JSONArray());
27+
							{
28-
								items.put(jsonObj);
28+
								String trClass = priceTableTRNode.attr("class");
29-
							} catch (Exception e) {
29+
								String trPreview = priceTableTRNode.attr("preview");
30-
								Log.d("coccccccc", "exc sect: "+e.getLocalizedMessage());
30+
								if(trClass!=null && trClass.equals("section"))
31
								{
32
									try {
33-
						else if(((trClass!=null && trClass.length()>0) && (trClass.equals("first") || trClass.equals("odd")))
33+
										JSONObject jsonObj = new JSONObject();
34-
								|| (trPreview!=null && trPreview.length()>0)
34+
										Element sectionNameNode = priceTableTRNode.getElementsByClass("t:eq(0)").first();
35-
								|| (priceTableTRNode.select(".c").first()!=null
35+
										String sectionName = sectionNameNode.text();
36-
									&& !priceTableTRNode.select(".c").first().text().equals("Код")) && items.length()>0)
36+
										jsonObj.put(Def.kItemHrefParent, hrefParent);
37-
		                {
37+
										jsonObj.put(Def.kItemsSectionName, sectionName);
38-
							//Код
38+
										jsonObj.put(Def.kItemsSectionItems, new JSONArray());
39-
		                    Element codeNode = priceTableTRNode.select(".c").first();
39+
										items.put(jsonObj);
40-
		                    String code = codeNode.text();
40+
									} catch (Exception e) {
41-
		                    //Комментарии
41+
										Log.d("coccccccc", "exc sect: "+e.getLocalizedMessage());
42-
		                    Element commentsNode = priceTableTRNode.select(".co").first();
42+
									}
43-
		                    String comments = commentsNode.text();
43+
								}
44-
		                    //Рейтинг
44+
								else if(((trClass!=null && trClass.length()>0) && (trClass.equals("first") || trClass.equals("odd")))
45-
		                    String rating = "0";
45+
										|| (trPreview!=null && trPreview.length()>0)
46-
		                    Element ratingNode = priceTableTRNode.select(".r").first();
46+
										|| (priceTableTRNode.select(".c").first()!=null
47-
		                    Element ratingANode = ratingNode.select("a").first();
47+
											&& !priceTableTRNode.select(".c").first().text().equals("Код")) && items.length()>0)
48-
		                    if(ratingANode!=null)
48+
				                {
49-
		                    {
49+
									//Код
50-
		                    	String[] splitRating = ratingANode.attr("title").split("от");
50+
				                    Element codeNode = priceTableTRNode.getElementsByClass("c:eq(0)").first();
51-
		                    	if(splitRating.length>0)
51+
				                    String code = codeNode.text();
52-
		                    		rating = splitRating[0].trim().replace(",",".");
52+
				                    //Комментарии
53-
		                    }
53+
				                    Element commentsNode = priceTableTRNode.getElementsByClass("co:eq(0)").first();
54-
		                    //Название
54+
				                    String comments = commentsNode.text();
55-
		                    Element nameNode = priceTableTRNode.select(".t").first();
55+
				                    //Рейтинг
56-
		                    String name = nameNode.text().replace("Помощь эксперта", "").trim();
56+
				                    String rating = "0";
57-
		                    //Картинка
57+
				                    Element ratingNode = priceTableTRNode.getElementsByClass("r:eq(0)").first();
58-
		                    String imageURL = (trPreview!=null)?trPreview:"";
58+
				                    Element ratingANode = ratingNode.getElementsByTag("a:eq(0)").first();
59-
		                    //Цена
59+
				                    if(ratingANode!=null)
60-
		                    Element priceNode = priceTableTRNode.select(".p").first();
60+
				                    {
61-
		                    String price = (priceNode!=null)?priceNode.text().replace(" ", "").trim():"";
61+
				                    	String[] splitRating = ratingANode.attr("title").split("от");
62-
		                    for(Element priceDIVNode : priceNode.select("div"))
62+
				                    	if(splitRating.length>0)
63-
		                        if(priceDIVNode.attr("class")==null
63+
				                    		rating = splitRating[0].trim().replace(",",".");
64-
		                        || priceDIVNode.attr("class").length()==0
64+
				                    }
65-
		                        || priceDIVNode.attr("class").equals("new"))
65+
				                    //Название
66-
		                            price = (priceDIVNode!=null)?priceDIVNode.text().replace(" ", "").trim():"";
66+
				                    Element nameNode = priceTableTRNode.getElementsByClass("t:eq(0)").first();
67-
	                           try {
67+
				                    String name = nameNode.text().replace("Помощь эксперта", "").trim();
68-
	                           	JSONArray sectionItems = ((JSONObject)items.get(items.length()-1)).getJSONArray(Def.kItemsSectionItems);
68+
				                    //Картинка
69-
			                    JSONObject jsonObj = new JSONObject();
69+
				                    String imageURL = (trPreview!=null)?trPreview:"";
70-
			                    jsonObj.put(Def.kItemHrefParent, hrefParent);
70+
				                    //Цена
71-
		                    	jsonObj.put(Def.kItemCode, code);
71+
				                    Element priceNode = priceTableTRNode.getElementsByClass("p:eq(0)").first();
72-
		                    	jsonObj.put(Def.kItemComments, comments);
72+
				                    String price = (priceNode!=null)?priceNode.text().replace(" ", "").trim():"";
73-
		                    	jsonObj.put(Def.kItemGrade, rating);
73+
				                    for(Element priceDIVNode : priceNode.select("div"))
74-
		                    	jsonObj.put(Def.kItemName, name);
74+
				                        if(priceDIVNode.attr("class")==null
75-
		                    	jsonObj.put(Def.kItemImageURL, imageURL);
75+
				                        || priceDIVNode.attr("class").length()==0
76-
		                    	jsonObj.put(Def.kItemPrice, price);
76+
				                        || priceDIVNode.attr("class").equals("new"))
77-
		                    	sectionItems.put(jsonObj);
77+
				                            price = (priceDIVNode!=null)?priceDIVNode.text().replace(" ", "").trim():"";
78-
							} catch (Exception e) {
78+
		                            try {
79-
								Log.d("coccccccc", "exc item: "+e.getLocalizedMessage());
79+
		                            	JSONArray sectionItems = ((JSONObject)items.get(items.length()-1)).getJSONArray(Def.kItemsSectionItems);
80-
							}			                    
80+
					                    JSONObject jsonObj = new JSONObject();
81-
						} 
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
}