Search in sources :

Example 6 with PodcastChannel

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

the class PodcastDaoTestCase method testChannelId.

@Test
public void testChannelId() {
    int channelId = podcastDao.createChannel(new PodcastChannel("http://foo"));
    assertEquals("Error in createChannel.", channelId + 1, podcastDao.createChannel(new PodcastChannel("http://foo")));
    assertEquals("Error in createChannel.", channelId + 2, podcastDao.createChannel(new PodcastChannel("http://foo")));
    assertEquals("Error in createChannel.", channelId + 3, podcastDao.createChannel(new PodcastChannel("http://foo")));
    podcastDao.deleteChannel(channelId + 1);
    assertEquals("Error in createChannel.", channelId + 4, podcastDao.createChannel(new PodcastChannel("http://foo")));
    podcastDao.deleteChannel(channelId + 4);
    assertEquals("Error in createChannel.", channelId + 5, podcastDao.createChannel(new PodcastChannel("http://foo")));
}
Also used : PodcastChannel(org.libresonic.player.domain.PodcastChannel) Test(org.junit.Test)

Example 7 with PodcastChannel

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

the class PodcastDaoTestCase method createChannel.

private int createChannel() {
    PodcastChannel channel = new PodcastChannel("http://foo");
    podcastDao.createChannel(channel);
    channel = podcastDao.getAllChannels().get(0);
    return channel.getId();
}
Also used : PodcastChannel(org.libresonic.player.domain.PodcastChannel)

Example 8 with PodcastChannel

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

the class PodcastService method doDownloadEpisode.

private void doDownloadEpisode(PodcastEpisode episode) {
    InputStream in = null;
    OutputStream out = null;
    if (isEpisodeDeleted(episode)) {
        LOG.info("Podcast " + episode.getUrl() + " was deleted. Aborting download.");
        return;
    }
    LOG.info("Starting to download Podcast from " + episode.getUrl());
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        PodcastChannel channel = getChannel(episode.getChannelId());
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(// 2 minutes
        2 * 60 * 1000).setSocketTimeout(// 10 minutes
        10 * 60 * 1000).build();
        HttpGet method = new HttpGet(episode.getUrl());
        method.setConfig(requestConfig);
        try (CloseableHttpResponse response = client.execute(method)) {
            in = response.getEntity().getContent();
            File file = getFile(channel, episode);
            out = new FileOutputStream(file);
            episode.setStatus(PodcastStatus.DOWNLOADING);
            episode.setBytesDownloaded(0L);
            episode.setErrorMessage(null);
            episode.setPath(file.getPath());
            podcastDao.updateEpisode(episode);
            byte[] buffer = new byte[4096];
            long bytesDownloaded = 0;
            int n;
            long nextLogCount = 30000L;
            while ((n = in.read(buffer)) != -1) {
                out.write(buffer, 0, n);
                bytesDownloaded += n;
                if (bytesDownloaded > nextLogCount) {
                    episode.setBytesDownloaded(bytesDownloaded);
                    nextLogCount += 30000L;
                    // Abort download if episode was deleted by user.
                    if (isEpisodeDeleted(episode)) {
                        break;
                    }
                    podcastDao.updateEpisode(episode);
                }
            }
            if (isEpisodeDeleted(episode)) {
                LOG.info("Podcast " + episode.getUrl() + " was deleted. Aborting download.");
                IOUtils.closeQuietly(out);
                file.delete();
            } else {
                addMediaFileIdToEpisodes(Arrays.asList(episode));
                episode.setBytesDownloaded(bytesDownloaded);
                podcastDao.updateEpisode(episode);
                LOG.info("Downloaded " + bytesDownloaded + " bytes from Podcast " + episode.getUrl());
                IOUtils.closeQuietly(out);
                updateTags(file, episode);
                episode.setStatus(PodcastStatus.COMPLETED);
                podcastDao.updateEpisode(episode);
                deleteObsoleteEpisodes(channel);
            }
        }
    } catch (Exception x) {
        LOG.warn("Failed to download Podcast from " + episode.getUrl(), x);
        episode.setStatus(PodcastStatus.ERROR);
        episode.setErrorMessage(getErrorMessage(x));
        podcastDao.updateEpisode(episode);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpGet(org.apache.http.client.methods.HttpGet) PodcastChannel(org.libresonic.player.domain.PodcastChannel) FileOutputStream(java.io.FileOutputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MediaFile(org.libresonic.player.domain.MediaFile) File(java.io.File)

Example 9 with PodcastChannel

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

the class PodcastService method refreshChannels.

private void refreshChannels(final List<PodcastChannel> channels, final boolean downloadEpisodes) {
    for (final PodcastChannel channel : channels) {
        Runnable task = new Runnable() {

            public void run() {
                doRefreshChannel(channel, downloadEpisodes);
            }
        };
        refreshExecutor.submit(task);
    }
}
Also used : PodcastChannel(org.libresonic.player.domain.PodcastChannel)

Example 10 with PodcastChannel

use of org.libresonic.player.domain.PodcastChannel 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)

Aggregations

PodcastChannel (org.libresonic.player.domain.PodcastChannel)13 Test (org.junit.Test)4 File (java.io.File)2 MediaFile (org.libresonic.player.domain.MediaFile)2 PodcastEpisode (org.libresonic.player.domain.PodcastEpisode)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)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 org.libresonic.restapi (org.libresonic.restapi)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1