Search in sources :

Example 6 with SyndElement

use of de.danoeh.antennapod.parser.feed.element.SyndElement in project AntennaPod by AntennaPod.

the class Atom method handleElementStart.

@Override
public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
    if (ENTRY.equals(localName)) {
        state.setCurrentItem(new FeedItem());
        state.getItems().add(state.getCurrentItem());
        state.getCurrentItem().setFeed(state.getFeed());
    } else if (localName.matches(isText)) {
        String type = attributes.getValue(TEXT_TYPE);
        return new AtomText(localName, this, type);
    } else if (LINK.equals(localName)) {
        String href = attributes.getValue(LINK_HREF);
        String rel = attributes.getValue(LINK_REL);
        SyndElement parent = state.getTagstack().peek();
        if (parent.getName().matches(isFeedItem)) {
            if (rel == null || LINK_REL_ALTERNATE.equals(rel)) {
                state.getCurrentItem().setLink(href);
            } else if (LINK_REL_ENCLOSURE.equals(rel)) {
                String strSize = attributes.getValue(LINK_LENGTH);
                long size = 0;
                try {
                    if (strSize != null) {
                        size = Long.parseLong(strSize);
                    }
                } catch (NumberFormatException e) {
                    Log.d(TAG, "Length attribute could not be parsed.");
                }
                String type = attributes.getValue(LINK_TYPE);
                if (type == null) {
                    type = SyndTypeUtils.getMimeTypeFromUrl(href);
                }
                FeedItem currItem = state.getCurrentItem();
                if (SyndTypeUtils.enclosureTypeValid(type) && currItem != null && !currItem.hasMedia()) {
                    currItem.setMedia(new FeedMedia(currItem, href, size, type));
                }
            } else if (LINK_REL_PAYMENT.equals(rel)) {
                state.getCurrentItem().setPaymentLink(href);
            }
        } else if (parent.getName().matches(isFeed)) {
            if (rel == null || LINK_REL_ALTERNATE.equals(rel)) {
                String type = attributes.getValue(LINK_TYPE);
                /*
                     * Use as link if a) no type-attribute is given and
                     * feed-object has no link yet b) type of link is
                     * LINK_TYPE_HTML or LINK_TYPE_XHTML
                     */
                if (state.getFeed() != null && ((type == null && state.getFeed().getLink() == null) || (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)))) {
                    state.getFeed().setLink(href);
                } else if (LINK_TYPE_ATOM.equals(type) || LINK_TYPE_RSS.equals(type)) {
                    // treat as podlove alternate feed
                    String title = attributes.getValue(LINK_TITLE);
                    if (TextUtils.isEmpty(title)) {
                        title = href;
                    }
                    state.addAlternateFeedUrl(title, href);
                }
            } else if (LINK_REL_ARCHIVES.equals(rel) && state.getFeed() != null) {
                String type = attributes.getValue(LINK_TYPE);
                if (LINK_TYPE_ATOM.equals(type) || LINK_TYPE_RSS.equals(type)) {
                    String title = attributes.getValue(LINK_TITLE);
                    if (TextUtils.isEmpty(title)) {
                        title = href;
                    }
                    state.addAlternateFeedUrl(title, href);
                } else if (LINK_TYPE_HTML.equals(type) || LINK_TYPE_XHTML.equals(type)) {
                // A Link such as to a directory such as iTunes
                }
            } else if (LINK_REL_PAYMENT.equals(rel) && state.getFeed() != null) {
                state.getFeed().addPayment(new FeedFunding(href, ""));
            } else if (LINK_REL_NEXT.equals(rel) && state.getFeed() != null) {
                state.getFeed().setPaged(true);
                state.getFeed().setNextPageLink(href);
            }
        }
    }
    return new SyndElement(localName, this);
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) AtomText(de.danoeh.antennapod.parser.feed.element.AtomText) FeedFunding(de.danoeh.antennapod.model.feed.FeedFunding)

Example 7 with SyndElement

use of de.danoeh.antennapod.parser.feed.element.SyndElement in project AntennaPod by AntennaPod.

the class PodcastIndex method handleElementStart.

@Override
public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
    if (FUNDING.equals(localName)) {
        String href = attributes.getValue(URL);
        FeedFunding funding = new FeedFunding(href, "");
        state.setCurrentFunding(funding);
        state.getFeed().addPayment(state.getCurrentFunding());
    }
    return new SyndElement(localName, this);
}
Also used : SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement) FeedFunding(de.danoeh.antennapod.model.feed.FeedFunding)

Example 8 with SyndElement

use of de.danoeh.antennapod.parser.feed.element.SyndElement in project AntennaPod by AntennaPod.

the class Rss20 method handleElementStart.

@Override
public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
    if (ITEM.equals(localName) && CHANNEL.equals(state.getTagstack().lastElement().getName())) {
        state.setCurrentItem(new FeedItem());
        state.getItems().add(state.getCurrentItem());
        state.getCurrentItem().setFeed(state.getFeed());
    } else if (ENCLOSURE.equals(localName) && ITEM.equals(state.getTagstack().peek().getName())) {
        String type = attributes.getValue(ENC_TYPE);
        String url = attributes.getValue(ENC_URL);
        boolean validType = SyndTypeUtils.enclosureTypeValid(type);
        if (!validType) {
            type = SyndTypeUtils.getMimeTypeFromUrl(url);
            validType = SyndTypeUtils.enclosureTypeValid(type);
        }
        boolean validUrl = !TextUtils.isEmpty(url);
        if (state.getCurrentItem() != null && state.getCurrentItem().getMedia() == null && validType && validUrl) {
            long size = 0;
            try {
                size = Long.parseLong(attributes.getValue(ENC_LEN));
                if (size < 16384) {
                    // less than 16kb is suspicious, check manually
                    size = 0;
                }
            } catch (NumberFormatException e) {
                Log.d(TAG, "Length attribute could not be parsed.");
            }
            FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type);
            state.getCurrentItem().setMedia(media);
        }
    }
    return new SyndElement(localName, this);
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement)

Example 9 with SyndElement

use of de.danoeh.antennapod.parser.feed.element.SyndElement in project AntennaPod by AntennaPod.

the class Rss20 method handleElementEnd.

@Override
public void handleElementEnd(String localName, HandlerState state) {
    if (ITEM.equals(localName)) {
        if (state.getCurrentItem() != null) {
            FeedItem currentItem = state.getCurrentItem();
            // as a title if the item has no title-tag.
            if (currentItem.getTitle() == null) {
                currentItem.setTitle(currentItem.getDescription());
            }
            if (state.getTempObjects().containsKey(Itunes.DURATION)) {
                if (currentItem.hasMedia()) {
                    Integer duration = (Integer) state.getTempObjects().get(Itunes.DURATION);
                    currentItem.getMedia().setDuration(duration);
                }
                state.getTempObjects().remove(Itunes.DURATION);
            }
        }
        state.setCurrentItem(null);
    } else if (state.getTagstack().size() >= 2 && state.getContentBuf() != null) {
        String contentRaw = state.getContentBuf().toString();
        String content = SyndStringUtils.trimAllWhitespace(contentRaw);
        String contentFromHtml = HtmlCompat.fromHtml(content, HtmlCompat.FROM_HTML_MODE_COMPACT).toString();
        SyndElement topElement = state.getTagstack().peek();
        String top = topElement.getName();
        SyndElement secondElement = state.getSecondTag();
        String second = secondElement.getName();
        String third = null;
        if (state.getTagstack().size() >= 3) {
            third = state.getThirdTag().getName();
        }
        if (GUID.equals(top) && ITEM.equals(second)) {
            // which should be ignored
            if (!TextUtils.isEmpty(contentRaw) && state.getCurrentItem() != null) {
                state.getCurrentItem().setItemIdentifier(contentRaw);
            }
        } else if (TITLE.equals(top)) {
            if (ITEM.equals(second) && state.getCurrentItem() != null) {
                state.getCurrentItem().setTitle(contentFromHtml);
            } else if (CHANNEL.equals(second) && state.getFeed() != null) {
                state.getFeed().setTitle(contentFromHtml);
            }
        } else if (LINK.equals(top)) {
            if (CHANNEL.equals(second) && state.getFeed() != null) {
                state.getFeed().setLink(content);
            } else if (ITEM.equals(second) && state.getCurrentItem() != null) {
                state.getCurrentItem().setLink(content);
            }
        } else if (PUBDATE.equals(top) && ITEM.equals(second) && state.getCurrentItem() != null) {
            state.getCurrentItem().setPubDate(DateUtils.parseOrNullIfFuture(content));
        } else if (URL.equals(top) && IMAGE.equals(second) && CHANNEL.equals(third)) {
            // prefer itunes:image
            if (state.getFeed() != null && state.getFeed().getImageUrl() == null) {
                state.getFeed().setImageUrl(content);
            }
        } else if (DESCR.equals(localName)) {
            if (CHANNEL.equals(second) && state.getFeed() != null) {
                state.getFeed().setDescription(contentFromHtml);
            } else if (ITEM.equals(second) && state.getCurrentItem() != null) {
                // fromHtml here breaks \n when not html
                state.getCurrentItem().setDescriptionIfLonger(content);
            }
        } else if (LANGUAGE.equals(localName) && state.getFeed() != null) {
            state.getFeed().setLanguage(content.toLowerCase(Locale.US));
        }
    }
}
Also used : FeedItem(de.danoeh.antennapod.model.feed.FeedItem) SyndElement(de.danoeh.antennapod.parser.feed.element.SyndElement)

Aggregations

SyndElement (de.danoeh.antennapod.parser.feed.element.SyndElement)9 FeedItem (de.danoeh.antennapod.model.feed.FeedItem)4 FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)3 AtomText (de.danoeh.antennapod.parser.feed.element.AtomText)3 FeedFunding (de.danoeh.antennapod.model.feed.FeedFunding)2 Namespace (de.danoeh.antennapod.parser.feed.namespace.Namespace)1