Example usage for org.jsoup.nodes Element attr

List of usage examples for org.jsoup.nodes Element attr

Introduction

In this page you can find the example usage for org.jsoup.nodes Element attr.

Prototype

public String attr(String attributeKey) 

Source Link

Document

Get an attribute's value by its key.

Usage

From source file:com.lloydtorres.stately.telegrams.TelegramComposeActivity.java

/**
 * Gets the check value needed to send a telegram.
 * @param recipients List of verified recipients
 *//*from w  w w.  j a  v  a2  s.  com*/
private void getTelegramCheckValue(final List<String> recipients) {
    if (isInProgress) {
        SparkleHelper.makeSnackbar(mView, getString(R.string.multiple_request_error));
        return;
    }
    isInProgress = true;

    NSStringRequest stringRequest = new NSStringRequest(getApplicationContext(), Request.Method.GET,
            Telegram.SEND_TELEGRAM, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Document d = Jsoup.parse(response, SparkleHelper.BASE_URI);
                    Element input = d.select("input[name=chk]").first();

                    if (input == null) {
                        mSwipeRefreshLayout.setRefreshing(false);
                        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_parsing));
                        return;
                    }

                    String chk = input.attr("value");
                    sendTelegram(recipients, chk);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    SparkleHelper.logError(error.toString());
                    mSwipeRefreshLayout.setRefreshing(false);
                    isInProgress = false;
                    if (error instanceof TimeoutError || error instanceof NoConnectionError
                            || error instanceof NetworkError) {
                        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_no_internet));
                    } else {
                        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_generic));
                    }
                }
            });

    if (!DashHelper.getInstance(this).addRequest(stringRequest)) {
        mSwipeRefreshLayout.setRefreshing(false);
        isInProgress = false;
        SparkleHelper.makeSnackbar(mView, getString(R.string.rate_limit_error));
    }
}

From source file:com.lloydtorres.stately.telegrams.TelegramsFragment.java

/**
 * Scrape and parse telegrams from NS site.
 *///from w w w.j a va  2  s.c o  m
private void queryTelegrams(final int offset, final int direction, final boolean firstRun) {
    TelegramFolder activeFolder = null;

    if (selectedFolder < folders.size()) {
        activeFolder = folders.get(selectedFolder);
    } else {
        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_generic));
        return;
    }

    String targetURL = String.format(Locale.US, Telegram.GET_TELEGRAM, activeFolder.value, offset);

    NSStringRequest stringRequest = new NSStringRequest(getContext(), Request.Method.GET, targetURL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (getActivity() == null || !isAdded()) {
                        return;
                    }
                    Document d = Jsoup.parse(response, SparkleHelper.BASE_URI);
                    Element chkHolder = d.select("input[name=chk]").first();
                    if (chkHolder != null) {
                        chkValue = chkHolder.attr("value");
                    }
                    processRawTelegrams(d, direction, firstRun);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (getActivity() == null || !isAdded()) {
                        return;
                    }
                    SparkleHelper.logError(error.toString());
                    mSwipeRefreshLayout.setRefreshing(false);
                    if (error instanceof TimeoutError || error instanceof NoConnectionError
                            || error instanceof NetworkError) {
                        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_no_internet));
                    } else {
                        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_generic));
                    }
                }
            });

    if (!DashHelper.getInstance(getContext()).addRequest(stringRequest)) {
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(mView, getString(R.string.rate_limit_error));
    }
}

From source file:com.lloydtorres.stately.telegrams.TelegramsFragment.java

/**
 * Actually parse through the response sent by NS and build telegram objects.
 * @param d Document containing parsed response.
 * @param direction Direction the user is loading telegrams
 * @param firstRun if first time running this process
 *//* w ww .ja  v  a 2s .  c  o  m*/
private void processRawTelegrams(Document d, int direction, boolean firstRun) {
    Element telegramsContainer = d.select("div#tglist").first();
    Element foldersContainer = d.select("select#tgfolder").first();

    if (telegramsContainer == null || foldersContainer == null) {
        // safety check
        mSwipeRefreshLayout.setRefreshing(false);
        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_parsing));
        return;
    }

    // Build list of folders
    folders = new ArrayList<TelegramFolder>();
    Elements rawFolders = foldersContainer.select("option[value]");
    for (Element rf : rawFolders) {
        TelegramFolder telFolder = new TelegramFolder();
        String rfValue = rf.attr("value");
        if (!rfValue.equals("_new")) {
            String rfName = rf.text();
            telFolder.name = rfName;
            telFolder.value = rfValue;
            folders.add(telFolder);
        }
    }

    // Build telegram objects from raw telegrams
    ArrayList<Telegram> scannedTelegrams = MuffinsHelper.processRawTelegrams(telegramsContainer,
            PinkaHelper.getActiveUser(getContext()).nationId);
    switch (direction) {
    case SCAN_FORWARD:
        processTelegramsForward(scannedTelegrams, firstRun);
        break;
    default:
        processTelegramsBackward(scannedTelegrams);
        break;
    }

    mSwipeRefreshLayout.setRefreshing(false);
}

From source file:com.lloydtorres.stately.wa.ResolutionActivity.java

/**
 * Gets the required localid to post vote.
 * @param url Target URL to scrape./*from w  ww .  j  av a  2 s .co m*/
 * @param p Vote
 */
private void getLocalId(final String url, final int p) {
    if (isInProgress) {
        SparkleHelper.makeSnackbar(mView, getString(R.string.multiple_request_error));
        return;
    }
    isInProgress = true;

    NSStringRequest stringRequest = new NSStringRequest(getApplicationContext(), Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Document d = Jsoup.parse(response, SparkleHelper.BASE_URI);
                    Element input = d.select("input[name=localid]").first();

                    if (input == null) {
                        mSwipeRefreshLayout.setRefreshing(false);
                        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_parsing));
                        return;
                    }

                    String localid = input.attr("value");
                    postVote(url, localid, p);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    SparkleHelper.logError(error.toString());
                    mSwipeRefreshLayout.setRefreshing(false);
                    isInProgress = false;
                    if (error instanceof TimeoutError || error instanceof NoConnectionError
                            || error instanceof NetworkError) {
                        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_no_internet));
                    } else {
                        SparkleHelper.makeSnackbar(mView, getString(R.string.login_error_generic));
                    }
                }
            });

    if (!DashHelper.getInstance(this).addRequest(stringRequest)) {
        mSwipeRefreshLayout.setRefreshing(false);
        isInProgress = false;
        SparkleHelper.makeSnackbar(mView, getString(R.string.rate_limit_error));
    }
}

From source file:com.near.chimerarevo.fragments.PostFragment.java

private void parseParagraphs(Elements ps) {
    for (Element p : ps) {
        if (!p.html().startsWith("&") && !p.html().startsWith("<iframe") && !p.html().startsWith("<!")
                && !p.html().contains("<h") && !p.html().contains("<ol") && !p.html().contains("<ul")
                && !p.html().contains("<pre") && !p.html().contains("<tr")) {
            parseNormalImages(p.select("img"));
            p.select("img").remove();

            Elements lnks = p.getElementsByTag("a");
            for (Element lnk : lnks) {
                if (lnk.attr("href").startsWith("#"))
                    lnk.removeAttr("href");
            }/*from w w  w . j av  a  2  s  .com*/

            String txt = p.html().replace("<br />", "").replace("\n", "").trim();
            if (txt.length() > 0)
                addText(txt, true, Typeface.DEFAULT);
        }
    }
}

From source file:com.near.chimerarevo.fragments.PostFragment.java

private void parseNormalImages(Elements ims) {
    if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("load_imgs_pref", true))
        for (Element im : ims) {
            if (im.attr("src").startsWith("http")) {
                if (!im.attr("src").contains("www.gstatic.com"))
                    addImage(im.attr("src"));
                else if (im.hasAttr("pagespeed_lazy_src"))
                    addImage(im.attr("pagespeed_lazy_src"));
            }/*from   w w  w  .jav  a2 s  . c o m*/
        }
}

From source file:com.near.chimerarevo.fragments.PostFragment.java

private void parseLinkedImages(Elements lnk) {
    if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("load_imgs_pref", true)) {
        String[] urls = new String[lnk.size()];
        for (int i = 0; i < lnk.size(); i++) {
            Elements ims = lnk.get(i).getElementsByTag("img");
            for (Element im : ims) {
                if (im.hasAttr("data-original"))
                    urls[i] = im.attr("data-original");
                else if (im.hasAttr("ng-src"))
                    urls[i] = im.attr("ng-src");
                else
                    urls[i] = "#";
            }//from   ww  w .j av a  2s.  c o m
        }
        if (urls.length == 1)
            addImage(urls[0]);
        else
            addGallery(urls);
    }
}

From source file:com.near.chimerarevo.fragments.PostFragment.java

private void parseYoutubeVideos(Elements vids) {
    for (Element vid : vids) {
        if (vid.hasAttr("src")) {
            if (vid.attr("src").contains("youtube")) {
                if (PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("load_vid_pref",
                        true))/*w  ww.j av a  2s .c  o m*/
                    addYoutubeVideo(vid.attr("src"));
            } else
                addWebObject(vid.attr("width"), vid.attr("height"), vid.attr("src"));
        }
    }
}

From source file:com.near.chimerarevo.fragments.PostFragment.java

private void parseBulletedLists(Elements itms) {
    String bld = "";
    for (Element itm : itms) {
        Elements str = itm.getElementsByTag("li");
        for (Element itm2 : str) {
            if (itm2.children().size() >= 1) {
                Elements ch = itm2.getElementsByTag("a");
                for (Element c : ch) {
                    if (c.attr("href").contains("#"))
                        c.removeAttr("href");
                }// ww  w.j a  va 2  s  .  co m
            }
            bld += ("\u2022 " + itm2.outerHtml() + "<br />");
        }
    }
    addText(bld, true, Typeface.DEFAULT);
}

From source file:com.normalexception.app.rx8club.fragment.category.CategoryFragment.java

/**
 * Grab contents from the forum that the user clicked on
 * @param doc      The document parsed from the link
 * @param id      The id number of the link
 * @param isMarket    True if the link is from a marketplace category
 *///from   www . j a  va  2s  .co  m
public void getCategoryContents(Document doc, String id, boolean isMarket) {

    // Update pagination
    try {
        Elements pageNumbers = doc.select("div[class=pagenav]");
        Elements pageLinks = pageNumbers.first().select("td[class^=vbmenu_control]");
        thisPage = pageLinks.text().split(" ")[1];
        finalPage = pageLinks.text().split(" ")[3];
    } catch (Exception e) {
    }

    // Make sure id contains only numbers
    if (!isNewTopicActivity)
        id = Utils.parseInts(id);

    // Grab each thread
    Elements threadListing = doc.select("table[id=threadslist] > tbody > tr");

    for (Element thread : threadListing) {
        try {
            boolean isSticky = false, isLocked = false, hasAttachment = false, isAnnounce = false,
                    isPoll = false;
            String formattedTitle = "", postCount = "0", views = "0", forum = "", threadUser = "",
                    lastUser = "", threadLink = "", lastPage = "", totalPosts = "0", threadDate = "";

            Elements announcementContainer = thread.select("td[colspan=5]");
            Elements threadTitleContainer = thread.select("a[id^=thread_title]");

            // We could have two different types of threads.  Announcement threads are 
            // completely different than the other types of threads (sticky, locked, etc)
            // so we need to play some games here
            if (announcementContainer != null && !announcementContainer.isEmpty()) {
                Log.d(TAG, "Announcement Thread Found");

                Elements annThread = announcementContainer.select("div > a");
                Elements annUser = announcementContainer.select("div > span[class=smallfont]");
                formattedTitle = "Announcement: " + annThread.first().text();
                threadUser = annUser.last().text();
                threadLink = annThread.attr("href");
                isAnnounce = true;
            } else if (threadTitleContainer != null && !threadTitleContainer.isEmpty()) {
                Element threadLinkEl = thread.select("a[id^=thread_title]").first();
                Element repliesText = thread.select("td[title^=Replies]").first();
                Element threaduser = thread.select("td[id^=td_threadtitle_] div.smallfont").first();
                Element threadicon = thread.select("img[id^=thread_statusicon_]").first();
                Element threadDiv = thread.select("td[id^=td_threadtitle_] > div").first();
                Element threadDateFull = thread.select("td[title^=Replies:] > div").first();

                try {
                    isSticky = threadDiv.text().contains("Sticky:");
                } catch (Exception e) {
                }

                try {
                    isPoll = threadDiv.text().contains("Poll:");
                } catch (Exception e) {
                }

                try {
                    String icSt = threadicon.attr("src");
                    isLocked = (icSt.contains("lock") && icSt.endsWith(".gif"));
                } catch (Exception e) {
                }

                String preString = "";
                try {
                    preString = threadDiv.select("span > b").text();
                } catch (Exception e) {
                }

                try {
                    hasAttachment = !threadDiv.select("a[onclick^=attachments]").isEmpty();
                } catch (Exception e) {
                }

                // Find the last page if it exists
                try {
                    lastPage = threadDiv.select("span").last().select("a").last().attr("href");
                } catch (Exception e) {
                }

                threadDate = threadDateFull.text();
                int findAMPM = threadDate.indexOf("M") + 1;
                threadDate = threadDate.substring(0, findAMPM);

                String totalPostsInThreadTitle = threadicon.attr("alt");

                if (totalPostsInThreadTitle != null && totalPostsInThreadTitle.length() > 0)
                    totalPosts = totalPostsInThreadTitle.split(" ")[2];

                // Remove page from the link
                String realLink = Utils.removePageFromLink(link);

                if (threadLinkEl.attr("href").contains(realLink) || (isNewTopicActivity || isMarket)) {

                    String txt = repliesText.getElementsByClass("alt2").attr("title");
                    String splitter[] = txt.split(" ", 4);

                    postCount = splitter[1].substring(0, splitter[1].length() - 1);
                    views = splitter[3];

                    try {
                        if (this.isNewTopicActivity)
                            forum = thread.select("td[class=alt1]").last().text();
                    } catch (Exception e) {
                    }

                    formattedTitle = String.format("%s%s%s", isSticky ? "Sticky: " : isPoll ? "Poll: " : "",
                            preString.length() == 0 ? "" : preString + " ", threadLinkEl.text());
                }

                threadUser = threaduser.text();
                lastUser = repliesText.select("a[href*=members]").text();
                threadLink = threadLinkEl.attr("href");
            }

            // Add our thread to our list as long as the thread
            // contains a title
            if (!formattedTitle.equals("")) {
                ThreadModel tv = new ThreadModel();
                tv.setTitle(formattedTitle);
                tv.setStartUser(threadUser);
                tv.setLastUser(lastUser);
                tv.setLink(threadLink);
                tv.setLastLink(lastPage);
                tv.setPostCount(postCount);
                tv.setMyPosts(totalPosts);
                tv.setViewCount(views);
                tv.setLocked(isLocked);
                tv.setSticky(isSticky);
                tv.setAnnouncement(isAnnounce);
                tv.setPoll(isPoll);
                tv.setHasAttachment(hasAttachment);
                tv.setForum(forum);
                tv.setLastPostTime(threadDate);
                threadlist.add(tv);
            } else if (thread.text()
                    .contains(MainApplication.getAppContext().getString(R.string.constantNoUpdate))) {
                Log.d(TAG, String.format("Found End of New Threads after %d threads...", threadlist.size()));
                if (threadlist.size() > 0) {
                    ThreadModel ltv = threadlist.get(threadlist.size() - 1);
                    Log.d(TAG, String.format("Last New Thread '%s'", ltv.getTitle()));
                }

                if (!PreferenceHelper.hideOldPosts(MainApplication.getAppContext()))
                    threadlist.add(new ThreadModel(true));
                else {
                    Log.d(TAG, "User Chose To Hide Old Threads");
                    break;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Error Parsing That Thread...", e);
            Log.d(TAG, "Thread may have moved");
        }
    }
}