Search in sources :

Example 1 with PodcastEpisode

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

the class PodcastService method deleteEpisode.

/**
     * Deletes the Podcast episode with the given ID.
     *
     * @param episodeId     The Podcast episode ID.
     * @param logicalDelete Whether to perform a logical delete by setting the
     *                      episode status to {@link PodcastStatus#DELETED}.
     */
public void deleteEpisode(int episodeId, boolean logicalDelete) {
    PodcastEpisode episode = podcastDao.getEpisode(episodeId);
    if (episode == null) {
        return;
    }
    // Delete file.
    if (episode.getPath() != null) {
        File file = new File(episode.getPath());
        if (file.exists()) {
            file.delete();
        // TODO: Delete directory if empty?
        }
    }
    if (logicalDelete) {
        episode.setStatus(PodcastStatus.DELETED);
        episode.setErrorMessage(null);
        podcastDao.updateEpisode(episode);
    } else {
        podcastDao.deleteEpisode(episodeId);
    }
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) File(java.io.File) PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

Example 2 with PodcastEpisode

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

the class PodcastService method getEpisodeByUrl.

private PodcastEpisode getEpisodeByUrl(String url) {
    PodcastEpisode episode = podcastDao.getEpisodeByUrl(url);
    if (episode == null) {
        return null;
    }
    List<PodcastEpisode> episodes = Arrays.asList(episode);
    episodes = filterAllowed(episodes);
    addMediaFileIdToEpisodes(episodes);
    return episodes.isEmpty() ? null : episodes.get(0);
}
Also used : PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

Example 3 with PodcastEpisode

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

the class PodcastService method doRefreshChannel.

@SuppressWarnings({ "unchecked" })
private void doRefreshChannel(PodcastChannel channel, boolean downloadEpisodes) {
    InputStream in = null;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        channel.setStatus(PodcastStatus.DOWNLOADING);
        channel.setErrorMessage(null);
        podcastDao.updateChannel(channel);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(// 2 minutes
        2 * 60 * 1000).setSocketTimeout(// 10 minutes
        10 * 60 * 1000).build();
        HttpGet method = new HttpGet(channel.getUrl());
        method.setConfig(requestConfig);
        try (CloseableHttpResponse response = client.execute(method)) {
            in = response.getEntity().getContent();
            Document document = new SAXBuilder().build(in);
            Element channelElement = document.getRootElement().getChild("channel");
            channel.setTitle(StringUtil.removeMarkup(channelElement.getChildTextTrim("title")));
            channel.setDescription(StringUtil.removeMarkup(channelElement.getChildTextTrim("description")));
            channel.setImageUrl(getChannelImageUrl(channelElement));
            channel.setStatus(PodcastStatus.COMPLETED);
            channel.setErrorMessage(null);
            podcastDao.updateChannel(channel);
            downloadImage(channel);
            refreshEpisodes(channel, channelElement.getChildren("item"));
        }
    } catch (Exception x) {
        LOG.warn("Failed to get/parse RSS file for Podcast channel " + channel.getUrl(), x);
        channel.setStatus(PodcastStatus.ERROR);
        channel.setErrorMessage(getErrorMessage(x));
        podcastDao.updateChannel(channel);
    } finally {
        IOUtils.closeQuietly(in);
    }
    if (downloadEpisodes) {
        for (final PodcastEpisode episode : getEpisodes(channel.getId())) {
            if (episode.getStatus() == PodcastStatus.NEW && episode.getUrl() != null) {
                downloadEpisode(episode);
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) SAXBuilder(org.jdom.input.SAXBuilder) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.jdom.Element) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.jdom.Document) PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

Example 4 with PodcastEpisode

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

the class PodcastService method deleteObsoleteEpisodes.

private synchronized void deleteObsoleteEpisodes(PodcastChannel channel) {
    int episodeCount = settingsService.getPodcastEpisodeRetentionCount();
    if (episodeCount == -1) {
        return;
    }
    List<PodcastEpisode> episodes = getEpisodes(channel.getId());
    // Don't do anything if other episodes of the same channel is currently downloading.
    for (PodcastEpisode episode : episodes) {
        if (episode.getStatus() == PodcastStatus.DOWNLOADING) {
            return;
        }
    }
    // Reverse array to get chronological order (oldest episodes first).
    Collections.reverse(episodes);
    int episodesToDelete = Math.max(0, episodes.size() - episodeCount);
    for (int i = 0; i < episodesToDelete; i++) {
        deleteEpisode(episodes.get(i).getId(), true);
        LOG.info("Deleted old Podcast episode " + episodes.get(i).getUrl());
    }
}
Also used : PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

Example 5 with PodcastEpisode

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

the class PodcastService method getEpisode.

public PodcastEpisode getEpisode(int episodeId, boolean includeDeleted) {
    PodcastEpisode episode = podcastDao.getEpisode(episodeId);
    if (episode == null) {
        return null;
    }
    if (episode.getStatus() == PodcastStatus.DELETED && !includeDeleted) {
        return null;
    }
    addMediaFileIdToEpisodes(Arrays.asList(episode));
    return episode;
}
Also used : PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

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