Search in sources :

Example 1 with UnsupportedFeedtypeException

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

the class FeedParserTask method call.

@Override
public FeedHandlerResult call() {
    Feed feed = new Feed(request.getSource(), request.getLastModified());
    feed.setFile_url(request.getDestination());
    feed.setId(request.getFeedfileId());
    feed.setDownloaded(true);
    feed.setPreferences(new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, VolumeAdaptionSetting.OFF, request.getUsername(), request.getPassword()));
    feed.setPageNr(request.getArguments().getInt(DownloadRequest.REQUEST_ARG_PAGE_NR, 0));
    DownloadError reason = null;
    String reasonDetailed = null;
    FeedHandler feedHandler = new FeedHandler();
    FeedHandlerResult result = null;
    try {
        result = feedHandler.parseFeed(feed);
        Log.d(TAG, feed.getTitle() + " parsed");
        checkFeedData(feed);
    } catch (SAXException | IOException | ParserConfigurationException e) {
        successful = false;
        e.printStackTrace();
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
    } catch (UnsupportedFeedtypeException e) {
        e.printStackTrace();
        successful = false;
        reason = DownloadError.ERROR_UNSUPPORTED_TYPE;
        if ("html".equalsIgnoreCase(e.getRootElement())) {
            reason = DownloadError.ERROR_UNSUPPORTED_TYPE_HTML;
        }
        reasonDetailed = e.getMessage();
    } catch (InvalidFeedException e) {
        e.printStackTrace();
        successful = false;
        reason = DownloadError.ERROR_PARSER_EXCEPTION;
        reasonDetailed = e.getMessage();
    } finally {
        File feedFile = new File(request.getDestination());
        if (feedFile.exists()) {
            boolean deleted = feedFile.delete();
            Log.d(TAG, "Deletion of file '" + feedFile.getAbsolutePath() + "' " + (deleted ? "successful" : "FAILED"));
        }
    }
    if (successful) {
        downloadStatus = new DownloadStatus(feed, feed.getHumanReadableIdentifier(), DownloadError.SUCCESS, successful, reasonDetailed, request.isInitiatedByUser());
        return result;
    } else {
        downloadStatus = new DownloadStatus(feed, feed.getTitle(), reason, successful, reasonDetailed, request.isInitiatedByUser());
        return null;
    }
}
Also used : FeedPreferences(de.danoeh.antennapod.model.feed.FeedPreferences) InvalidFeedException(de.danoeh.antennapod.core.util.InvalidFeedException) UnsupportedFeedtypeException(de.danoeh.antennapod.parser.feed.UnsupportedFeedtypeException) FeedHandlerResult(de.danoeh.antennapod.parser.feed.FeedHandlerResult) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) DownloadError(de.danoeh.antennapod.core.util.DownloadError) FeedHandler(de.danoeh.antennapod.parser.feed.FeedHandler) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) Feed(de.danoeh.antennapod.model.feed.Feed)

Example 2 with UnsupportedFeedtypeException

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

the class OnlineFeedViewActivity method doParseFeed.

/**
 * Try to parse the feed.
 * @return  The FeedHandlerResult if successful.
 *          Null if unsuccessful but we started another attempt.
 * @throws Exception If unsuccessful but we do not know a resolution.
 */
@Nullable
private FeedHandlerResult doParseFeed() throws Exception {
    FeedHandler handler = new FeedHandler();
    try {
        return handler.parseFeed(feed);
    } catch (UnsupportedFeedtypeException e) {
        Log.d(TAG, "Unsupported feed type detected");
        if ("html".equalsIgnoreCase(e.getRootElement())) {
            boolean dialogShown = showFeedDiscoveryDialog(new File(feed.getFile_url()), feed.getDownload_url());
            if (dialogShown) {
                // Should not display an error message
                return null;
            } else {
                throw new UnsupportedFeedtypeException(getString(R.string.download_error_unsupported_type_html));
            }
        } else {
            throw e;
        }
    } catch (Exception e) {
        Log.e(TAG, Log.getStackTraceString(e));
        throw e;
    } finally {
        boolean rc = new File(feed.getFile_url()).delete();
        Log.d(TAG, "Deleted feed source file. Result: " + rc);
    }
}
Also used : UnsupportedFeedtypeException(de.danoeh.antennapod.parser.feed.UnsupportedFeedtypeException) FeedHandler(de.danoeh.antennapod.parser.feed.FeedHandler) File(java.io.File) UnsupportedFeedtypeException(de.danoeh.antennapod.parser.feed.UnsupportedFeedtypeException) FeedUrlNotFoundException(de.danoeh.antennapod.core.feed.FeedUrlNotFoundException) IOException(java.io.IOException) Nullable(androidx.annotation.Nullable)

Example 3 with UnsupportedFeedtypeException

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

the class TypeGetter method getType.

public Type getType(Feed feed) throws UnsupportedFeedtypeException {
    XmlPullParserFactory factory;
    if (feed.getFile_url() != null) {
        Reader reader = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            reader = createReader(feed);
            xpp.setInput(reader);
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    String tag = xpp.getName();
                    switch(tag) {
                        case ATOM_ROOT:
                            feed.setType(Feed.TYPE_ATOM1);
                            Log.d(TAG, "Recognized type Atom");
                            String strLang = xpp.getAttributeValue("http://www.w3.org/XML/1998/namespace", "lang");
                            if (strLang != null) {
                                feed.setLanguage(strLang);
                            }
                            return Type.ATOM;
                        case RSS_ROOT:
                            String strVersion = xpp.getAttributeValue(null, "version");
                            if (strVersion == null) {
                                feed.setType(Feed.TYPE_RSS2);
                                Log.d(TAG, "Assuming type RSS 2.0");
                                return Type.RSS20;
                            } else if (strVersion.equals("2.0")) {
                                feed.setType(Feed.TYPE_RSS2);
                                Log.d(TAG, "Recognized type RSS 2.0");
                                return Type.RSS20;
                            } else if (strVersion.equals("0.91") || strVersion.equals("0.92")) {
                                Log.d(TAG, "Recognized type RSS 0.91/0.92");
                                return Type.RSS091;
                            }
                            throw new UnsupportedFeedtypeException("Unsupported rss version");
                        default:
                            Log.d(TAG, "Type is invalid");
                            throw new UnsupportedFeedtypeException(Type.INVALID, tag);
                    }
                } else {
                    try {
                        eventType = xpp.next();
                    } catch (RuntimeException e) {
                        // Apparently this happens on some devices...
                        throw new UnsupportedFeedtypeException("Unable to get type");
                    }
                }
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
            // XML document might actually be a HTML document -> try to parse as HTML
            String rootElement = null;
            try {
                if (Jsoup.parse(new File(feed.getFile_url()), null) != null) {
                    rootElement = "html";
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new UnsupportedFeedtypeException(Type.INVALID, rootElement);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    Log.d(TAG, "Type is invalid");
    throw new UnsupportedFeedtypeException(Type.INVALID);
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) UnsupportedFeedtypeException(de.danoeh.antennapod.parser.feed.UnsupportedFeedtypeException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) Reader(java.io.Reader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) File(java.io.File)

Aggregations

UnsupportedFeedtypeException (de.danoeh.antennapod.parser.feed.UnsupportedFeedtypeException)3 File (java.io.File)3 IOException (java.io.IOException)3 FeedHandler (de.danoeh.antennapod.parser.feed.FeedHandler)2 Nullable (androidx.annotation.Nullable)1 FeedUrlNotFoundException (de.danoeh.antennapod.core.feed.FeedUrlNotFoundException)1 DownloadStatus (de.danoeh.antennapod.core.service.download.DownloadStatus)1 DownloadError (de.danoeh.antennapod.core.util.DownloadError)1 InvalidFeedException (de.danoeh.antennapod.core.util.InvalidFeedException)1 Feed (de.danoeh.antennapod.model.feed.Feed)1 FeedPreferences (de.danoeh.antennapod.model.feed.FeedPreferences)1 FeedHandlerResult (de.danoeh.antennapod.parser.feed.FeedHandlerResult)1 Reader (java.io.Reader)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XmlStreamReader (org.apache.commons.io.input.XmlStreamReader)1 SAXException (org.xml.sax.SAXException)1 XmlPullParser (org.xmlpull.v1.XmlPullParser)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)1