All Downloads are FREE. Search and download functionalities are using the official Maven repository.

be.ceau.digitalpodcast.DigitalPodcastAPI Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package be.ceau.digitalpodcast;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmlpull.v1.XmlPullParserException;

import be.ceau.opml.Opml;
import be.ceau.opml.Outline;
import be.ceau.opml.Parser;

public class DigitalPodcastAPI {

	private static final Logger logger = LoggerFactory.getLogger(DigitalPodcastAPI.class);

	private static final String DIRECTORY = "http://www.digitalpodcast.com/opml/digitalpodcast.opml";
	private static final String DIRECTORY_NO_ADULT = "http://www.digitalpodcast.com/opml/digitalpodcastnoadult.opml";
	private static final String DIRECTORY_CLEAN = "http://www.digitalpodcast.com/opml/digitalpodcastclean.opml";
	private static final String NEW = "http://www.digitalpodcast.com/opml/digitalpodcastnew.opml";
	private static final String NEW_NO_ADULT = "http://www.digitalpodcast.com/opml/digitalpodcastnewnoadult.opml";
	private static final String NEW_CLEAN = "http://www.digitalpodcast.com/opml/digitalpodcastnewclean.opml";
	private static final String MOST_VIEWED = "http://www.digitalpodcast.com/opml/digitalpodcastmostviewed.opml";
	private static final String MOST_VIEWED_NO_ADULT = "http://www.digitalpodcast.com/opml/digitalpodcastmostviewednoadult.opml";
	private static final String MOST_VIEWED_CLEAN = "http://www.digitalpodcast.com/opml/digitalpodcastmostviewedclean.opml";
	private static final String TOP_RATED = "http://www.digitalpodcast.com/opml/digitalpodcasttoprated.opml";
	private static final String TOP_RATED_NO_ADULT = "http://www.digitalpodcast.com/opml/digitalpodcasttopratednoadult.opml";
	private static final String TOP_RATED_CLEAN = "http://www.digitalpodcast.com/opml/digitalpodcasttopratedclean.opml";
	private static final String SUBSCRIBED = "http://www.digitalpodcast.com/opml/digitalpodcastmostsubscribed.opml";
	private static final String SUBSCRIBED_NO_ADULT = "http://www.digitalpodcast.com/opml/digitalpodcastmostsubscribednoadult.opml";
	private static final String SUBSCRIBED_CLEAN = "http://www.digitalpodcast.com/opml/digitalpodcastmostsubscribedclean.opml";

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #DIRECTORY}
	 */
	public Stream getDirectory() {
		return read(DIRECTORY);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #DIRECTORY_NO_ADULT}
	 */
	public Stream getDirectoryNoAdult() {
		return read(DIRECTORY_NO_ADULT);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #DIRECTORY_CLEAN}
	 */
	public Stream getDirectoryClean() {
		return read(DIRECTORY_CLEAN);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #NEW}
	 */
	public Stream getNew() {
		return read(NEW);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #NEW_NO_ADULT}
	 */
	public Stream getNewNoAdult() {
		return read(NEW_NO_ADULT);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #NEW_CLEAN}
	 */
	public Stream getNewClean() {
		return read(NEW_CLEAN);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #MOST_VIEWED}
	 */
	public Stream getMostViewed() {
		return read(MOST_VIEWED);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #MOST_VIEWED_NO_ADULT}
	 */
	public Stream getMostViewedNoAdult() {
		return read(MOST_VIEWED_NO_ADULT);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #MOST_VIEWED_CLEAN}
	 */
	public Stream getMostViewedClean() {
		return read(MOST_VIEWED_CLEAN);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #TOP_RATED}
	 */
	public Stream getTopRated() {
		return read(TOP_RATED);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #TOP_RATED_NO_ADULT}
	 */
	public Stream getTopRatedNoAdult() {
		return read(TOP_RATED_NO_ADULT);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #TOP_RATED_CLEAN}
	 */
	public Stream getTopRatedClean() {
		return read(TOP_RATED_CLEAN);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #SUBSCRIBED}
	 */
	public Stream getSubscribed() {
		return read(SUBSCRIBED);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #SUBSCRIBED_NO_ADULT}
	 */
	public Stream getSubscribedNoAdult() {
		return read(SUBSCRIBED_NO_ADULT);
	}

	/**
	 * @return a {@link Stream} over any {@link Podcast} found at {@value #SUBSCRIBED_CLEAN}
	 */
	public Stream getSubscribedClean() {
		return read(SUBSCRIBED_CLEAN);
	}

	private Stream read(String link) {

		try {

			URL url = new URL(link);

			try (InputStream in = url.openConnection().getInputStream()) {

				Opml opml = new Parser().parse(in);

				return opml.getBody()
						.getOutlines()
						.stream()
						.flatMap(otl -> extractPodcasts(otl).stream());

			}

		} catch (XmlPullParserException | IOException e) {
			logger.error("read(String {})", link, e);
		}

		return Stream.empty();

	}

	private List extractPodcasts(Outline outline) {

		List podcasts = outline.getSubElements()
				.stream()
				.flatMap(otl -> extractPodcasts(otl).stream())
				.collect(Collectors.toList());

		String type = outline.getAttributes().get("type");
		if ("link".equals(type)) {
			String name = outline.getAttributes().get("text");
			String url = outline.getAttributes().get("url");
			if (name != null && url != null) {
				podcasts.add(new Podcast(name, url));
			} else {
				logger.warn("extractPodcasts(Outline) name or url null in {}", outline.getAttributes());
			}
		}

		return podcasts;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy