示例#1
0
文件: commands.py 项目: matachi/riley
    def handle(self, podcasts_and_episodes):
        file_storage = FileStorage()
        podcasts = file_storage.get_podcasts()

        podcasts_and_episodes_to_download = OrderedDict()
        podcast = None
        for string in podcasts_and_episodes:
            if podcast is not None:
                episode_ranges = string
                try:
                    episode_indices_to_download = self.get_indices(
                        episode_ranges)
                except ValueError:
                    episodes = podcast.episodes
                    podcasts_and_episodes_to_download[podcast] = episodes
                    podcast = None
                else:
                    if episode_indices_to_download[-1] < len(podcast.episodes):
                        episodes = [podcast.episodes[i] for i in
                                    episode_indices_to_download]
                        podcasts_and_episodes_to_download[podcast] = episodes
                        podcast = None
                        continue
                    else:
                        last = episode_indices_to_download[-1]
                        sys.exit('\n'.join([
                            "The podcast does not have an episode with index '%d'." % last,
                            "",
                            "The valid range is: [%d, %d]" % (0, len(podcast.episodes) - 1),
                            ]))
            if podcast is None:
                podcast_name = string
                if podcast_name in podcasts:
                    podcast = podcasts[podcast_name]
                    podcasts_and_episodes_to_download[podcast] = None
                else:
                    list_of_podcasts = '\n'.join(
                        '* %s' % name for name in podcasts.keys())
                    sys.exit('\n'.join([
                        "A podcast with the name '%s' does not exist." % podcast_name,
                        "",
                        "Try one of these instead:",
                        "",
                        list_of_podcasts
                    ]))

        if len(podcasts_and_episodes_to_download) > 0:
            episodes = chain.from_iterable(
                podcasts_and_episodes_to_download.values())
        else:
            episodes = chain.from_iterable(
                podcast.episodes for podcast in podcasts.values())

        download_directory = file_storage.get_config()['storage']
        for episode in episodes:
            download.download(episode.media_href, download_directory,
                              episode.published)
            episode.downloaded = True
            FileStorage().save_podcast(episode.podcast)
示例#2
0
def test_download_url(tmpdir, monkeypatch):
    monkeypatch.setattr('riley.download._download', dummy_download)

    response = MagicMock()
    response.url = 'http://example.com/123.mp3'
    response.headers.get.return_value = None
    # The header 'content-length' is missing, which was the case with
    # http://www.linuxvoice.com/podcast_opus.rss
    monkeypatch.setattr(
        'riley.download.requests.get', lambda x, stream: response)

    time_struct = time.strptime('2015-11-12 01:02:03', '%Y-%m-%d %H:%M:%S')

    download(response.url, tmpdir.strpath, time_struct)

    file = tmpdir.join('123.mp3')
    assert file.read() == 'abc'
    assert os.stat(file.strpath).st_mtime == time.mktime(time_struct)
示例#3
0
文件: commands.py 项目: matachi/riley
    def handle(self, number_of_episodes):
        file_storage = FileStorage()
        podcasts = file_storage.get_podcasts()

        episodes = []
        for podcast in podcasts.values():
            episodes.extend(podcast.episodes)

        episodes = heapq.nlargest(
            number_of_episodes, episodes, key=lambda e: e.score)

        download_directory = file_storage.get_config()['storage']
        for episode in episodes:
            print("Downloading '{}' from '{}'.".format(
                episode.title, episode.podcast.name))
            download.download(episode.media_href, download_directory,
                              episode.published)
            episode.downloaded = True
            FileStorage().save_podcast(episode.podcast)
示例#4
0
文件: commands.py 项目: matachi/riley
    def handle(self, number_of_episodes):
        file_storage = FileStorage()
        podcasts = file_storage.get_podcasts()

        episodes = []
        for podcast in podcasts.values():
            episodes.extend(podcast.episodes)

        episodes = heapq.nlargest(number_of_episodes,
                                  episodes,
                                  key=lambda e: e.score)

        download_directory = file_storage.get_config()['storage']
        for episode in episodes:
            print("Downloading '{}' from '{}'.".format(episode.title,
                                                       episode.podcast.name))
            download.download(episode.media_href, download_directory,
                              episode.published)
            episode.downloaded = True
            FileStorage().save_podcast(episode.podcast)
示例#5
0
文件: commands.py 项目: matachi/riley
    def handle(self, podcasts_and_episodes):
        file_storage = FileStorage()
        podcasts = file_storage.get_podcasts()

        podcasts_and_episodes_to_download = OrderedDict()
        podcast = None
        for string in podcasts_and_episodes:
            if podcast is not None:
                episode_ranges = string
                try:
                    episode_indices_to_download = self.get_indices(
                        episode_ranges)
                except ValueError:
                    episodes = podcast.episodes
                    podcasts_and_episodes_to_download[podcast] = episodes
                    podcast = None
                else:
                    if episode_indices_to_download[-1] < len(podcast.episodes):
                        episodes = [
                            podcast.episodes[i]
                            for i in episode_indices_to_download
                        ]
                        podcasts_and_episodes_to_download[podcast] = episodes
                        podcast = None
                        continue
                    else:
                        last = episode_indices_to_download[-1]
                        sys.exit('\n'.join([
                            "The podcast does not have an episode with index '%d'."
                            % last,
                            "",
                            "The valid range is: [%d, %d]" %
                            (0, len(podcast.episodes) - 1),
                        ]))
            if podcast is None:
                podcast_name = string
                if podcast_name in podcasts:
                    podcast = podcasts[podcast_name]
                    podcasts_and_episodes_to_download[podcast] = None
                else:
                    list_of_podcasts = '\n'.join('* %s' % name
                                                 for name in podcasts.keys())
                    sys.exit('\n'.join([
                        "A podcast with the name '%s' does not exist." %
                        podcast_name, "", "Try one of these instead:", "",
                        list_of_podcasts
                    ]))

        if len(podcasts_and_episodes_to_download) > 0:
            episodes = chain.from_iterable(
                podcasts_and_episodes_to_download.values())
        else:
            episodes = chain.from_iterable(podcast.episodes
                                           for podcast in podcasts.values())

        download_directory = file_storage.get_config()['storage']
        for episode in episodes:
            download.download(episode.media_href, download_directory,
                              episode.published)
            episode.downloaded = True
            FileStorage().save_podcast(episode.podcast)