Search in sources :

Example 11 with PodcastEpisode

use of org.libresonic.player.domain.PodcastEpisode in project libresonic by Libresonic.

the class PodcastService method refreshEpisodes.

private void refreshEpisodes(PodcastChannel channel, List<Element> episodeElements) {
    List<PodcastEpisode> episodes = new ArrayList<PodcastEpisode>();
    for (Element episodeElement : episodeElements) {
        String title = episodeElement.getChildTextTrim("title");
        String duration = getITunesElement(episodeElement, "duration");
        String description = episodeElement.getChildTextTrim("description");
        if (StringUtils.isBlank(description)) {
            description = getITunesElement(episodeElement, "summary");
        }
        title = StringUtil.removeMarkup(title);
        description = StringUtil.removeMarkup(description);
        Element enclosure = episodeElement.getChild("enclosure");
        if (enclosure == null) {
            LOG.info("No enclosure found for episode " + title);
            continue;
        }
        String url = enclosure.getAttributeValue("url");
        url = sanitizeUrl(url);
        if (url == null) {
            LOG.info("No enclosure URL found for episode " + title);
            continue;
        }
        if (getEpisodeByUrl(url) == null) {
            Long length = null;
            try {
                length = new Long(enclosure.getAttributeValue("length"));
            } catch (Exception x) {
                LOG.warn("Failed to parse enclosure length.", x);
            }
            Date date = parseDate(episodeElement.getChildTextTrim("pubDate"));
            PodcastEpisode episode = new PodcastEpisode(null, channel.getId(), url, null, title, description, date, duration, length, 0L, PodcastStatus.NEW, null);
            episodes.add(episode);
            LOG.info("Created Podcast episode " + title);
        }
    }
    // Sort episode in reverse chronological order (newest first)
    Collections.sort(episodes, new Comparator<PodcastEpisode>() {

        public int compare(PodcastEpisode a, PodcastEpisode b) {
            long timeA = a.getPublishDate() == null ? 0L : a.getPublishDate().getTime();
            long timeB = b.getPublishDate() == null ? 0L : b.getPublishDate().getTime();
            if (timeA < timeB) {
                return 1;
            }
            if (timeA > timeB) {
                return -1;
            }
            return 0;
        }
    });
    // Create episodes in database, skipping the proper number of episodes.
    int downloadCount = settingsService.getPodcastEpisodeDownloadCount();
    if (downloadCount == -1) {
        downloadCount = Integer.MAX_VALUE;
    }
    for (int i = 0; i < episodes.size(); i++) {
        PodcastEpisode episode = episodes.get(i);
        if (i >= downloadCount) {
            episode.setStatus(PodcastStatus.SKIPPED);
        }
        podcastDao.createEpisode(episode);
    }
}
Also used : Element(org.jdom.Element) PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

Example 12 with PodcastEpisode

use of org.libresonic.player.domain.PodcastEpisode in project libresonic by Libresonic.

the class PodcastService method init.

public synchronized void init() {
    try {
        // Clean up partial downloads.
        for (PodcastChannel channel : getAllChannels()) {
            for (PodcastEpisode episode : getEpisodes(channel.getId())) {
                if (episode.getStatus() == PodcastStatus.DOWNLOADING) {
                    deleteEpisode(episode.getId(), false);
                    LOG.info("Deleted Podcast episode '" + episode.getTitle() + "' since download was interrupted.");
                }
            }
        }
        schedule();
    } catch (Throwable x) {
        LOG.error("Failed to initialize PodcastService: " + x, x);
    }
}
Also used : PodcastChannel(org.libresonic.player.domain.PodcastChannel) PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

Example 13 with PodcastEpisode

use of org.libresonic.player.domain.PodcastEpisode in project libresonic by Libresonic.

the class PodcastDaoTestCase method testGetEpisodes.

@Test
public void testGetEpisodes() {
    int channelId = createChannel();
    PodcastEpisode a = new PodcastEpisode(null, channelId, "a", null, null, null, new Date(3000), null, null, null, PodcastStatus.NEW, null);
    PodcastEpisode b = new PodcastEpisode(null, channelId, "b", null, null, null, new Date(1000), null, null, null, PodcastStatus.NEW, "error");
    PodcastEpisode c = new PodcastEpisode(null, channelId, "c", null, null, null, new Date(2000), null, null, null, PodcastStatus.NEW, null);
    PodcastEpisode d = new PodcastEpisode(null, channelId, "c", null, null, null, null, null, null, null, PodcastStatus.NEW, "");
    podcastDao.createEpisode(a);
    podcastDao.createEpisode(b);
    podcastDao.createEpisode(c);
    podcastDao.createEpisode(d);
    List<PodcastEpisode> episodes = podcastDao.getEpisodes(channelId);
    assertEquals("Error in getEpisodes().", 4, episodes.size());
    assertEpisodeEquals(a, episodes.get(0));
    assertEpisodeEquals(c, episodes.get(1));
    assertEpisodeEquals(b, episodes.get(2));
    assertEpisodeEquals(d, episodes.get(3));
}
Also used : Date(java.util.Date) PodcastEpisode(org.libresonic.player.domain.PodcastEpisode) Test(org.junit.Test)

Example 14 with PodcastEpisode

use of org.libresonic.player.domain.PodcastEpisode in project libresonic by Libresonic.

the class PodcastDaoTestCase method testGetEpisode.

@Test
public void testGetEpisode() {
    assertNull("Error in getEpisode()", podcastDao.getEpisode(23));
    int channelId = createChannel();
    PodcastEpisode episode = new PodcastEpisode(null, channelId, "http://bar", "path", "title", "description", new Date(), "12:34", 3276213L, 2341234L, PodcastStatus.NEW, "error");
    podcastDao.createEpisode(episode);
    int episodeId = podcastDao.getEpisodes(channelId).get(0).getId();
    PodcastEpisode newEpisode = podcastDao.getEpisode(episodeId);
    assertEpisodeEquals(episode, newEpisode);
}
Also used : Date(java.util.Date) PodcastEpisode(org.libresonic.player.domain.PodcastEpisode) Test(org.junit.Test)

Example 15 with PodcastEpisode

use of org.libresonic.player.domain.PodcastEpisode in project libresonic by Libresonic.

the class PodcastDaoTestCase method testDeleteEpisode.

@Test
public void testDeleteEpisode() {
    int channelId = createChannel();
    assertEquals("Wrong number of episodes.", 0, podcastDao.getEpisodes(channelId).size());
    PodcastEpisode episode = new PodcastEpisode(null, channelId, "http://bar", null, null, null, null, null, null, null, PodcastStatus.NEW, null);
    podcastDao.createEpisode(episode);
    assertEquals("Wrong number of episodes.", 1, podcastDao.getEpisodes(channelId).size());
    podcastDao.createEpisode(episode);
    assertEquals("Wrong number of episodes.", 2, podcastDao.getEpisodes(channelId).size());
    podcastDao.deleteEpisode(podcastDao.getEpisodes(channelId).get(0).getId());
    assertEquals("Wrong number of episodes.", 1, podcastDao.getEpisodes(channelId).size());
    podcastDao.deleteEpisode(podcastDao.getEpisodes(channelId).get(0).getId());
    assertEquals("Wrong number of episodes.", 0, podcastDao.getEpisodes(channelId).size());
}
Also used : PodcastEpisode(org.libresonic.player.domain.PodcastEpisode) Test(org.junit.Test)

Aggregations

PodcastEpisode (org.libresonic.player.domain.PodcastEpisode)18 Test (org.junit.Test)6 Date (java.util.Date)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Element (org.jdom.Element)2 MediaFile (org.libresonic.player.domain.MediaFile)2 PodcastChannel (org.libresonic.player.domain.PodcastChannel)2 Predicate (com.google.common.base.Predicate)1 File (java.io.File)1 InputStream (java.io.InputStream)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 Document (org.jdom.Document)1 SAXBuilder (org.jdom.input.SAXBuilder)1 User (org.libresonic.player.domain.User)1 org.libresonic.restapi (org.libresonic.restapi)1