protected void fetchURLAndProcess( Utils.Notification notification, Utils.Function<Void, ArrayList<JuickMessage>> cont) { if (page == 0) { urlParser.getArgsMap().remove("page"); } else { urlParser.getArgsMap().put("page", "" + page); } final String jsonStr = httpClientService.getJSON(urlParser.getFullURL(), notification).getResult(); ArrayList<JuickMessage> messages = parseJSONpure(jsonStr); if (messages.size() > 0) { ArrayList<JuickMessage> reverse = new ArrayList<JuickMessage>(); for (Iterator<JuickMessage> iterator = messages.iterator(); iterator.hasNext(); ) { JuickMessage message = iterator.next(); if (!loadedMessages.add(message.getMID().toString())) { iterator.remove(); } else { reverse.add(0, message); } } if (loadedMessages.size() == 0) { page++; fetchURLAndProcess(notification, cont); return; } else { messages = reverse; } } cont.apply(messages); }
public ArrayList<JuickMessage> parseJSONpure(String jsonStr, boolean storeSource) { ArrayList<JuickMessage> messages = new ArrayList<JuickMessage>(); if (jsonStr != null) { try { JSONObject objMessages = new JSONObject(jsonStr); JSONArray json = objMessages.getJSONArray("messages"); int cnt = json.length(); for (int i = 0; i < cnt; i++) { JSONObject jsonObject = json.getJSONObject(i); JuickMessage msg = initFromJSON(jsonObject); messages.add(msg); if (!storeSource) msg.source = null; } } catch (Exception e) { e.printStackTrace(); // Log.e("initOpinionsAdapter", e.toString()); } } return messages; }
public ArrayList<JuickMessage> parseWebMessageListPure(String htmlStr) { ArrayList<JuickMessage> retval = new ArrayList<JuickMessage>(); Document parsed = Jsoup.parse(htmlStr); Elements posts = parsed.select("div"); ISimpleDateFormat sdf; ISimpleDateFormat sdf2; sdf = DevJuickComMessages.sdftz.createSDF("yyyy dd MMM HH:mm", "en", "US", "UTC"); sdf2 = DevJuickComMessages.sdftz.createSDF("yyyy dd MMM HH:mm", "ru", "RU", "UTC"); Calendar cal = Calendar.getInstance(); int currentYear = cal.get(Calendar.YEAR); for (Element post : posts) { String postClass = post.attr("class"); if (postClass.equals("post") || postClass.startsWith("post ")) { PointMessage message = new PointMessage(); message.User = new JuickUser(); message.User.UName = post.select("div[class=info] > a > img").attr("alt"); if (message.User.UName.length() == 0) { message.User.UName = post.select("div[class=author] > a").text(); } String dataId = post.attr("data-id"); String dataCommentId = post.attr("data-comment-id"); String dataToCommentId = post.attr("data-to-comment-id"); message.setMID(new PointMessageID(message.User.UName, dataId, 0)); if (dataCommentId.length() > 0) { message.setRID(Integer.parseInt(dataCommentId)); } if (dataToCommentId.length() > 0) { message.setReplyTo(Integer.parseInt(dataToCommentId)); } message.tags = new Vector<String>(); for (Element el : post.select("a[class=tag]")) { message.tags.add(el.text()); } message.microBlogCode = PointMessageID.CODE; StringBuilder dt = new StringBuilder(); for (Element el : post.select("div[class=created]")) { dt.append(" "); dt.append(el.text()); } try { message.Timestamp = new Date(sdf.parse(currentYear + " " + dt.toString().trim())); } catch (IllegalArgumentException e) { try { message.Timestamp = new Date(sdf2.parse(currentYear + " " + dt.toString().trim())); } catch (IllegalArgumentException e1) { continue; } } Date mt = message.Timestamp; if (mt.getTime() > System.currentTimeMillis() + 50 * 24 * 60 * 60 * 1000L) { Calendar cal2 = Calendar.getInstance(); cal2.setTime(mt); cal2.set(Calendar.YEAR, cal2.get(Calendar.YEAR) - 1); message.Timestamp = cal2.getTime(); } Elements postEls = post.select("div[class=text-content]"); if (postEls.size() < 1) { postEls = post.select("div[class=text]"); } String referencedImages = ""; Elements postimg = post.select("a[class=postimg]"); for (Element as : postimg) { Elements imgs = as.select("img"); for (Element img : imgs) { String src = img.attr("src"); referencedImages += " " + src; } } message.csrf_token = post.select("input[name=csrf_token]").attr("value"); // last part if (postEls.size() < 1) { message.Text = "Error parsing text ;-("; } else { Element elem = cleanupElement(postEls.get(0)); postEls.get(0).appendChild(elem); // add to document Document.OutputSettings os = elem.ownerDocument().outputSettings(); os.prettyPrint(false); String text = Utils.replace(elem.toString(), "\n", " "); text = Utils.replace(text, "&", "&"); // this was improperly done in cleanupElement while (true) { long olds = text.length(); text = Utils.replace(text, " ", " "); long news = text.length(); if (news == olds) break; } try { message.replies = Integer.parseInt(post.select("span[class=cn]").text()); } catch (Exception ex) { } text += referencedImages; message.Text = unwebMessageTextPoint(text); } retval.add(message); } } for (JuickMessage juickMessage : retval) { if (juickMessage.getRID() != 0 && juickMessage.getReplyTo() != 0 && !juickMessage.Text.startsWith("@")) { String uzur = null; for (JuickMessage scan : retval) { if (scan.getRID() == juickMessage.getReplyTo()) { uzur = scan.User.UName; break; } } if (uzur != null) { juickMessage.Text = "@" + uzur + " " + juickMessage.Text; } } } return retval; }