Пример #1
0
def download_podcast(directory: RadioDirectory, channel: Channel,
                     podcast: Podcast) -> Podcast:
    location = download_location(directory, channel, podcast)
    url = get_podcast_audio_link(podcast)

    # TODO: This takes some time, especially when there are a lot to
    # download. I could have this spawn threads, or add priorities,
    # and so on. For now, since it runs every few hours, and is more
    # of a push than a pull situation for the user, I'm leaving it
    # simple
    success = _download_from_url(url, location)

    if success:
        return podcast._replace(status=NewStatus())
    else:
        return podcast
Пример #2
0
def recent_podcast_from_radio(
    radio: Radio, ) -> typing.Tuple[Radio, InfoContent]:
    info_content = build_info_content(error='none found')

    new_podcasts = [(channel, podcast) for channel in radio.channels
                    for podcast in channel.known_podcasts
                    if type(podcast.status).__name__ == 'NewStatus']

    if new_podcasts:
        channel, recent_podcast = sorted(new_podcasts,
                                         key=lambda p: p[1].data.published,
                                         reverse=True)[0]

        info_content = build_info_content(result=PodcastLocation(
            path=download_location(radio.directory, channel, recent_podcast),
            channel_id=get_channel_id(channel),
            podcast_id=get_podcast_id(recent_podcast),
        ))

    return radio, info_content
Пример #3
0
def test_download_location():
    podcast_data = Podcast(
        status=RequestedStatus(),
        data=podcast_data_factory(
            audio_link={
                'length': u'0',
                'href': u'http://feed.thisamericanlife.org/~r/talpodcast/~5/R0qvREKxypU/597.mp3',  # noqa
                'type': u'audio/mpeg',
                'rel': u'enclosure',
            }))

    channel = channel_factory()

    actual = download_location(
        RadioDirectory('dir'),
        channel,
        podcast_data)

    expected = 'dir/{0}/597.mp3'.format(DEFAULT_CHANNEL_INFO_DIRECTORY)

    assert actual == expected
Пример #4
0
def _delete_podcast_file(
    directory: RadioDirectory,
    channel: Channel,
    podcast: Podcast,
) -> bool:

    # Where should this file be?
    filename = download_location(directory, channel, podcast)

    # If this file exists, move it to the trash_location
    if os.path.exists(filename):
        trash_filename = trash_location(directory, channel, podcast)

        # First, make sure the trash directory exists
        if not os.path.exists(os.path.dirname(trash_filename)):
            os.makedirs(os.path.dirname(trash_filename))

        os.rename(filename, trash_filename)
        return True

    return False
Пример #5
0
def test_move_to_trash():
    channel = channel_factory()
    new_podcast = new_podcast_factory()

    with tempfile.TemporaryDirectory() as tmpdirname:
        filename = download_location(RadioDirectory(tmpdirname), channel,
                                     new_podcast)

        trash_filename = trash_location(RadioDirectory(tmpdirname), channel,
                                        new_podcast)

        os.makedirs(os.path.dirname(filename))

        open(filename, 'a').close()

        assert os.path.exists(filename)
        assert not os.path.exists(trash_filename)

        assert _delete_podcast_file(RadioDirectory(tmpdirname), channel,
                                    new_podcast)

        assert not os.path.exists(filename)
        assert os.path.exists(trash_filename)
Пример #6
0
def recent_podcast_from_channel(
    radio: Radio,
    channel_id: str,
) -> typing.Tuple[Radio, InfoContent]:

    info_content = build_info_content(error='none found')
    channel = read_channel_from_id(radio, channel_id)

    new_podcasts = filter(
        lambda podcast: type(podcast.status).__name__ == 'NewStatus',
        channel.known_podcasts)

    if new_podcasts:
        recent_podcast = sorted(new_podcasts,
                                key=lambda p: p.data.published,
                                reverse=True)[0]

        info_content = build_info_content(result=PodcastLocation(
            path=download_location(radio.directory, channel, recent_podcast),
            channel_id=channel_id,
            podcast_id=get_podcast_id(recent_podcast),
        ))

    return radio, info_content