Java Code Examples for org.jsoup.select.Elements#isEmpty()

The following examples show how to use org.jsoup.select.Elements#isEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MypCardPricer.java    From MtgDesktopCompanion with GNU General Public License v3.0 6 votes vote down vote up
private void parsingOffers(String urlC, List<MagicPrice> list) throws IOException {
	Elements trs = URLTools.extractHtml(urlC).select("table.table tr[data-key]");
	for(Element tr : trs)
	{
		Elements tds = tr.select("td");
		if(tds.isEmpty())
		{
			logger.debug(getName() + " found no offer");
			return;
		}
		
		MagicPrice mp = new MagicPrice();
			mp.setCountry("Brazil");
			mp.setCurrency(Currency.getInstance("BRL"));
			mp.setSite(getName());
			mp.setSeller(tds.get(1).text());
			mp.setFoil(tds.get(2).html().equalsIgnoreCase("foil"));
			mp.setQuality(tds.get(3).html());
			mp.setValue(Double.parseDouble(tds.get(5).text().replaceAll("R\\$ ", "").replace(",", ".")));
			mp.setUrl(urlC);
			list.add(mp);
	}
	logger.debug(getName() + " found " + list.size() + " offers");
}
 
Example 2
Source File: RssLoader.java    From android-opensource-library-56 with Apache License 2.0 6 votes vote down vote up
private void parseDomTraverse(Document document) {
    Elements elements = document.getElementsByTag("item");
    for (Element element : elements) {
        Item item = new Item();
        Elements title = element.getElementsByTag("title");
        Elements link = element.getElementsByTag("link");
        if (!title.isEmpty()) {
            item.title = title.get(0).text();
        }
        if (!link.isEmpty()) {
            item.url = link.get(0).text();
        }
        if (mList == null) {
            mList = new RssList();
        }
        mList.addItem(item);
    }
}
 
Example 3
Source File: TwitchVideoRipper.java    From ripme with MIT License 6 votes vote down vote up
@Override
public void rip() throws IOException {
    LOGGER.info("Retrieving " + this.url);
    Document doc = Http.url(url).get();
    
    //Get user friendly filename from page title
    String title = doc.title();
    
    Elements script = doc.select("script");
    if (script.isEmpty()) {
        throw new IOException("Could not find script code at " + url);
    }
    //Regex assumes highest quality source is listed first
    Pattern p = Pattern.compile("\"source\":\"(.*?)\"");
    
    for (Element element : script) {
        Matcher m = p.matcher(element.data());
        if (m.find()){
            String vidUrl = m.group(1);
            addURLToDownload(new URL(vidUrl), HOST + "_" + title);
        }
    }
    waitForThreads();
}
 
Example 4
Source File: CukedoctorFilterExtension.java    From cukedoctor with Apache License 2.0 6 votes vote down vote up
@Override
public String process(Document document, String output) {
    if (document.basebackend("html") && System.getProperty(FILTER_DISABLE_EXT_KEY) == null) {
        org.jsoup.nodes.Document doc = Jsoup.parse(output, "UTF-8");

        Elements sect1 = doc.getElementsByClass("sect1");
        if (!sect1.isEmpty()) {
            Element contentElement = doc.getElementsByClass("sect1").get(0);
            contentElement.before("<span style=\"float:right\">\n" +
                    "\t<input value=\"Filter...\" onclick=\"this.value=''\" title=\"Filter features by title\" onblur=\"searchFeature(this.value);\"/>\n" +
                    "</span>");
        }
        return doc.html();
    } else {
        return output;
    }
}
 
Example 5
Source File: YoutubeAudioSourceManager.java    From kyoko with MIT License 6 votes vote down vote up
private String extractPlaylistTracks(Element videoContainer, Element loadMoreContainer, List<AudioTrack> tracks) {
    for (Element video : videoContainer.select(".pl-video")) {
        Elements lengthElements = video.select(".timestamp span");

        // If the timestamp element does not exist, it means the video is private
        if (!lengthElements.isEmpty()) {
            String videoId = video.attr("data-video-id").trim();
            String title = video.attr("data-title").trim();
            String author = video.select(".pl-video-owner a").text().trim();
            long duration = DataFormatTools.durationTextToMillis(lengthElements.first().text());

            tracks.add(buildTrackObject(videoId, title, author, false, duration));
        }
    }

    if (loadMoreContainer != null) {
        Elements more = loadMoreContainer.select(".load-more-button");
        if (!more.isEmpty()) {
            return more.first().attr("data-uix-load-more-href");
        }
    }

    return null;
}
 
Example 6
Source File: ImagevenueRipper.java    From ripme with MIT License 6 votes vote down vote up
private void fetchImage() {
    try {
        Document doc = Http.url(url)
                           .retries(3)
                           .get();
        // Find image
        Elements images = doc.select("a > img");
        if (images.isEmpty()) {
            LOGGER.warn("Image not found at " + this.url);
            return;
        }
        Element image = images.first();
        String imgsrc = image.attr("src");
        imgsrc = "http://" + this.url.getHost() + "/" + imgsrc;
        // Provide prefix and let the AbstractRipper "guess" the filename
        String prefix = "";
        if (Utils.getConfigBoolean("download.save_order", true)) {
            prefix = String.format("%03d_", index);
        }
        addURLToDownload(new URL(imgsrc), prefix);
    } catch (IOException e) {
        LOGGER.error("[!] Exception while loading/parsing " + this.url, e);
    }
}
 
Example 7
Source File: NfsfwRipper.java    From ripme with MIT License 5 votes vote down vote up
@Override
public Document getNextPage(Document page) throws IOException {
    String nextURL = null;
    Elements a = page.select("a.next");
    if (!a.isEmpty()){
        // Get next page of current album
        nextURL = "http://nfsfw.com" + a.first().attr("href");
    } else if (!subalbumURLs.isEmpty()){
        // Get next sub-album
        nextURL = subalbumURLs.remove(0);
        LOGGER.info("Detected subalbum URL at:" + nextURL);
        Matcher m = subalbumURLPattern.matcher(nextURL);
        if (m.matches()) {
            // Set the new save directory and save images with a new index
            this.currentDir = m.group(1);
            this.index = 0;
        } else {
            LOGGER.error("Invalid sub-album URL: " + nextURL);
            nextURL = null;
        }
    }
    // Wait
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        LOGGER.error("Interrupted while waiting to load next page", e);
    }
    if (nextURL == null){
        throw new IOException("No more pages");
    } else {
        return Http.url(nextURL).get();
    }
}
 
Example 8
Source File: MotherlessRipper.java    From ripme with MIT License 5 votes vote down vote up
@Override
public Document getNextPage(Document doc) throws IOException {
    Elements nextPageLink = doc.head().select("link[rel=next]");
    if (nextPageLink.isEmpty()) {
        throw new IOException("Last page reached");
    } else {
        String referrerLink = doc.head().select("link[rel=canonical]").first().attr("href");
        URL nextURL = new URL(this.url, nextPageLink.first().attr("href"));
        return Http.url(nextURL).referrer(referrerLink).get();
    }
}
 
Example 9
Source File: Mnemonic.java    From ankihelper with GNU General Public License v3.0 5 votes vote down vote up
private static String getSingleQueryResult(Document soup, String query, boolean toString){
    Elements re = soup.select(query);
    if(!re.isEmpty()){
        if(toString){
            return re.get(0).toString();
        }else {
            return re.get(0).text();
        }
    }else{
        return "";
    }
}
 
Example 10
Source File: YoutubeSearchProvider.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private AudioItem extractSearchResults(Document document, String query,
                                       Function<AudioTrackInfo, AudioTrack> trackFactory) {

  List<AudioTrack> tracks = new ArrayList<>();
  Elements resultsSelection = document.select("#page > #content #results");
  if (!resultsSelection.isEmpty()) {
    for (Element results : resultsSelection) {
      for (Element result : results.select(".yt-lockup-video")) {
        if (!result.hasAttr("data-ad-impressions") && result.select(".standalone-ypc-badge-renderer-label").isEmpty()) {
          extractTrackFromResultEntry(tracks, result, trackFactory);
        }
      }
    }
  } else {
    log.debug("Attempting to parse results page as polymer");
    try {
      tracks = polymerExtractTracks(document, trackFactory);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  if (tracks.isEmpty()) {
    return AudioReference.NO_TRACK;
  } else {
    return new BasicAudioPlaylist("Search results for: " + query, tracks, null, true);
  }
}
 
Example 11
Source File: WebsterLearners.java    From ankihelper with GNU General Public License v3.0 5 votes vote down vote up
private static String getSingleQueryResult(Document soup, String query, boolean toString){
    Elements re = soup.select(query);
    if(!re.isEmpty()){
        if(toString){
            return re.get(0).toString();
        }else {
            return re.get(0).text().trim();
        }
    }else{
        return "";
    }
}
 
Example 12
Source File: StructuralHtml.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeBody(final JCas jCas, final Element body) {

  final Node<Structure> root = StructureHierarchy.build(jCas, structuralClasses).getRoot();

  walk(body, root);

  // We need to create the proper li tags under ol and ul
  body.select("ul > p").wrap("<li></li>");
  body.select("ol > p").wrap("<li></li>");

  // Correct table cells from td to th in header
  body.select("thead td").tagName("th");

  // Add &nbsp; to any empty td or th's
  body.select("td:empty,th:empty").html("&nbsp");

  if (!outputEmptyTags) {
    Elements e = emptyElements(body);
    while (!e.isEmpty()) {
      e.remove();
      e = emptyElements(body);
    }
  }

  // TODO: In accordance with HTML spec
  // - Captions for Table should be moved inside the table
  // - Captions for Figure should be moved inside the figure

}
 
Example 13
Source File: SolrDictionary.java    From ankihelper with GNU General Public License v3.0 5 votes vote down vote up
static String getSingleQueryResult(Document soup, String query, boolean toString){
    Elements re = soup.select(query);
    if(!re.isEmpty()){
        if(toString){
            return re.get(0).toString();
        }else {
            return re.get(0).text();
        }
    }else{
        return "";
    }
}
 
Example 14
Source File: AttributeWithValuePresenceChecker.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This methods checks whether a given attribute is present for a set of
 * elements
 *
 * @param elements
 * @param testSolutionHandler
 */
private void checkAttributeWithValuePresence(
        Elements elements,
        TestSolutionHandler testSolutionHandler) {

    if (elements.isEmpty()) {
        testSolutionHandler.addTestSolution(TestSolution.NOT_APPLICABLE);
        return;
    }

    TestSolution testSolution = TestSolution.PASSED;

    for (Element el : elements) {
        if (!el.hasAttr(attributeName)
                || (el.hasAttr(attributeName) && !el.attr(attributeName).equals(attributeValue))) {

            testSolution = setTestSolution(testSolution, getFailureSolution());
            createSourceCodeRemark(getFailureSolution(), el, getFailureMsgCode());
            
        } else if (StringUtils.isNotBlank(getSuccessMsgCode())) {
            testSolution = setTestSolution(testSolution, getSuccessSolution());

            createSourceCodeRemark(getSuccessSolution(), el, getSuccessMsgCode());
        }
    }

    testSolutionHandler.addTestSolution(testSolution);

}
 
Example 15
Source File: JsoupDefinitionListExtractor.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean extractTopicsFrom(Document d, String u, TopicMap t) throws Exception{
    
    Elements lists = d.select("dl");
    
    if(lists.isEmpty())
        throw new Exception("No definition lists found!");
    
    //Basic init
    this.tm = t;
    this.wandoraClass = getWandoraClassTopic(tm);
    this.langTopic = getLangTopic(tm);
    
    this.childType = getOrCreateTopic(tm, CHILD_SI, "child");
    this.parentType = getOrCreateTopic(tm, PARENT_SI, "parent");
    this.relationType = getOrCreateTopic(tm, RELATION_SI, "relationship");

    this.documentType = getOrCreateTopic(tm, DOCUMENT_SI, "document");
    makeSubclassOf(tm, documentType, wandoraClass);
    
    this.listType = getOrCreateTopic(tm, LIST_SI, "list");
    makeSubclassOf(tm, listType, wandoraClass);
    
    this.definitionType = getOrCreateTopic(tm, DEF_SI, "definition");
    makeSubclassOf(tm, definitionType, wandoraClass);
    
    this.documentTopic = getOrCreateTopic(tm, u , d.title());
    documentTopic.addType(documentType);
    
    for(Element list: lists) parseList(list, documentTopic);
    
    return true;
}
 
Example 16
Source File: WechatUtil.java    From WechatTestTool with Apache License 2.0 4 votes vote down vote up
private static String getTagValue(String tagName, Element ele) {
	Elements eles = ele.getElementsByTag(tagName);
	return eles.isEmpty() ? null : eles.get(0).text();
}
 
Example 17
Source File: PutHTMLElement.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final Document doc;
    final Elements eles;
    try {
        doc = parseHTMLDocumentFromFlowfile(flowFile, context, session);
        eles = doc.select(context.getProperty(CSS_SELECTOR).evaluateAttributeExpressions().getValue());
    } catch (Exception ex) {
        getLogger().error("Failed to extract HTML from {} due to {}; routing to {}", new Object[] {flowFile, ex.toString(), REL_INVALID_HTML.getName()}, ex);
        session.transfer(flowFile, REL_INVALID_HTML);
        return;
    }


    if (eles == null || eles.isEmpty()) {
        // No element found
        session.transfer(flowFile, REL_NOT_FOUND);
    } else {
        final String putValue = context.getProperty(PUT_VALUE).evaluateAttributeExpressions(flowFile).getValue();

        for (final Element ele : eles) {
            switch (context.getProperty(PUT_LOCATION_TYPE).getValue()) {
                case APPEND_ELEMENT:
                    ele.append(putValue);
                    break;
                case PREPEND_ELEMENT:
                    ele.prepend(putValue);
                    break;
            }
        }

        FlowFile ff = session.write(session.create(flowFile), new StreamCallback() {
            @Override
            public void process(final InputStream in, final OutputStream out) throws IOException {
                out.write(doc.html().getBytes(StandardCharsets.UTF_8));
            }
        });

        session.transfer(ff, REL_SUCCESS);

        // Transfer the original HTML
        session.transfer(flowFile, REL_ORIGINAL);
    }
}
 
Example 18
Source File: Character.java    From KaellyBot with GNU General Public License v3.0 4 votes vote down vote up
public static Character getCharacter(String url, Language lg) throws IOException {
    Document doc = JSoupManager.getDocument(url);
    String bigSkinURL = doc.getElementsByClass("ak-entitylook").first().attr("style");
    bigSkinURL = bigSkinURL.substring(bigSkinURL.indexOf("https://"), bigSkinURL.indexOf(")"));
    String littleSkinURL = doc.getElementsByClass("ak-entitylook").last().toString();
    littleSkinURL = littleSkinURL.substring(littleSkinURL.indexOf("https://"), littleSkinURL.indexOf(")"));
    String pseudo = doc.getElementsByClass("ak-return-link").first().text();
    String level = doc.getElementsByClass("ak-directories-level").first().text()
            .replace(Translator.getLabel(lg, "whois.extract.level"), "").trim();
    String classe = doc.getElementsByClass("ak-directories-breed").first().text();
    String server = doc.getElementsByClass("ak-directories-server-name").first().text();
    String score = doc.getElementsByClass("ak-score-text").first().text() + " ("
            + doc.getElementsByClass("ak-progress-bar-text").first().text() + ")";

    // Optional
    String guildName = null;
    String guildUrl = null;
    String alliName = null;
    String alliUrl = null;

    Elements elem = doc.getElementsByClass("ak-infos-guildname");

    if (!elem.isEmpty()) {
        guildName = elem.first().text();
        guildUrl = elem.first().select("a").attr("abs:href");

        elem = doc.getElementsByClass("ak-infos-alliancename");

        if (!elem.isEmpty()) {
            alliName = elem.first().text();
            alliUrl = elem.first().select("a").attr("abs:href");
        }
    }

    StringBuilder ladderXP = new StringBuilder();
    StringBuilder ladderKoli = new StringBuilder();
    StringBuilder ladderSuccess = new StringBuilder();

    elem = doc.getElementsByClass("ak-container ak-table ak-responsivetable");
    if (!elem.isEmpty()) {
        ladderXP.append(doc.getElementsByClass("ak-total-xp").first().text()).append("\n");

        for(Element cote : doc.getElementsByClass("ak-total-kolizeum"))
            if (! cote.text().endsWith("-1"))
                ladderKoli.append(cote.text().replace(Translator.getLabel(lg, "whois.extract.koli"), "").trim()).append("\n");

        Elements trs = elem.first().getElementsByTag("tbody").first().getElementsByTag("tr");
        for (Element tr : trs) {
            String ladderText = tr.getElementsByTag("td").first().text() + " : ";
            tr.getElementsByTag("td").first().remove();
            if (!tr.getElementsByTag("td").first().text().equals("-"))
                ladderXP.append(ladderText).append(EmojiManager.getEmojiForLadder(tr.getElementsByTag("td").first().text())).append("\n");
            if (!tr.getElementsByTag("td").get(1).text().equals("-"))
                ladderKoli.append(ladderText).append(EmojiManager.getEmojiForLadder(tr.getElementsByTag("td").get(1).text())).append("\n");
            if (!tr.getElementsByTag("td").last().text().equals("-"))
                ladderSuccess.append(ladderText).append(EmojiManager.getEmojiForLadder(tr.getElementsByTag("td").last().text())).append("\n");
        }
    }

    return new Character(pseudo, level, classe, server, score,
            guildName, guildUrl, alliName, alliUrl, littleSkinURL, bigSkinURL, url,
            ladderXP.toString(), ladderKoli.toString(), ladderSuccess.toString());
}
 
Example 19
Source File: ParseV9PronVideo.java    From v9porn with MIT License 4 votes vote down vote up
/**
     * 解析主页
     *
     * @param html 主页html
     * @return 视频列表
     */
    public static List<V9PornItem> parseIndex(String html) {
        Logger.t(TAG).d(html);
//        html= DevHtmlTools.getLocalHtml(MyApplication.getInstance(),"LocalHtml.text");
        List<V9PornItem> v9PornItemList = new ArrayList<>();
        Document doc = Jsoup.parse(html);
        Elements bodys = doc.select("body");
        if(bodys==null||bodys.isEmpty()){
            return v9PornItemList;
        }
        Element body=bodys.first();
        Elements items=body.select("div[class=col-xs-12 col-sm-4 col-md-3 col-lg-3]");
        for (Element element : items) {
            V9PornItem v9PornItem = new V9PornItem();

            String title = element.getElementsByClass("video-title title-truncate m-t-5").first().text();
            v9PornItem.setTitle(title);
            Logger.d(title);

            String imgUrl = element.select("img[class=img-responsive]").first().attr("src");
            v9PornItem.setImgUrl(imgUrl);
            Logger.d(imgUrl);

            String duration = element.select("span[class=duration]").first().text();
            v9PornItem.setDuration(duration);
            Logger.d(duration);

            String contentUrl = element.select("a").first().attr("href");
            String viewKey = contentUrl.substring(contentUrl.indexOf("=") + 1,contentUrl.indexOf("&"));
            v9PornItem.setViewKey(viewKey);
            Logger.d(viewKey);

            String allInfo = element.text();
            int start = allInfo.indexOf("添加时间");
            String info = allInfo.substring(start);

            v9PornItem.setInfo(info);
            // Logger.d(info);
            v9PornItemList.add(v9PornItem);
        }
        return v9PornItemList;
    }
 
Example 20
Source File: RequestSpecHelper.java    From javalite with Apache License 2.0 3 votes vote down vote up
/**
 * Parses controller response content and selects content of HTML element using CSS selectors.<br>
 * <strong>Example:</strong><br>
 *
 *  <pre>
 *     request().get("index");
 *     a(find("div[class='greeting']").shouldEqual("Hello!");
 *  </pre>
 *
 * @param cssQuery CSS query. Implementation is based on <a href="http://jsoup.org/">JSoup</a>.
 * @return contents of selected element as text.
 */
protected String text(String cssQuery){
    Document doc = Jsoup.parse(responseContent());
    Elements elements  = doc.select(cssQuery);
    if(elements.isEmpty()){
        return null;
    }else if(elements.size() > 1){
        throw new IllegalArgumentException("Your query points to multiple elements. Choose only one.");
    }

    return elements.get(0).text();
}