Exemple #1
0
def has_new_podcast_from_channel(
        radio: Radio, channel_id: str) -> typing.Tuple[Radio, InfoContent]:

    info_content = build_info_content(result=False)

    channel = read_channel_from_id(radio, channel_id)
    for podcast in channel.known_podcasts:
        if type(podcast.status).__name__ == 'NewStatus':
            info_content = build_info_content(result=True)
            break
    return radio, info_content
Exemple #2
0
def main() -> None:
    parser = argparse.ArgumentParser(description='Loads podcasts.')
    parser.add_argument('command', choices=available_commands)

    # TODO: these are required options, eeh
    parser.add_argument('--config', required=True)
    parser.add_argument('--directory', required=True)

    parser.add_argument('--channel-id')
    parser.add_argument('--podcast-id')

    args = parser.parse_args()

    radio = load_radio(args.directory, args.config)

    if all([args.podcast_id, args.channel_id, args.command in podcast_action]):
        radio, info_content = podcast_action[args.command](radio,
                                                           args.channel_id,
                                                           args.podcast_id)
    elif all([args.channel_id, args.command in channel_action]):
        radio, info_content = channel_action[args.command](radio,
                                                           args.channel_id)
    elif args.command in radio_action:
        radio, info_content = radio_action[args.command](radio)
    else:
        info_content = build_info_content(error='command not found')

    save_radio(radio)

    info = build_info(args, info_content)
    print(output_info(info))
Exemple #3
0
def download_radio(radio: Radio) -> typing.Tuple[Radio, InfoContent]:
    downloaded_channels = [
        download_channel(radio.directory, channel)
        for channel in radio.channels
    ]

    radio = radio._replace(channels=downloaded_channels)
    info_content = build_info_content()

    return (radio, info_content)
Exemple #4
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
Exemple #5
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
Exemple #6
0
def delete_podcast(radio: Radio, channel_id: str,
                   podcast_id: str) -> typing.Tuple[Radio, InfoContent]:
    def apply_delete_on_right_podcast(channel: Channel,
                                      podcast: Podcast) -> Podcast:
        if get_podcast_id(podcast) == podcast_id:
            _delete_podcast_file(radio.directory, channel, podcast)
            podcast = podcast._replace(status=DeletedStatus())
        return podcast

    def apply_delete_on_right_channel(channel: Channel) -> Channel:
        if get_channel_id(channel) == channel_id:
            channel = map_channel_podcasts(channel,
                                           apply_delete_on_right_podcast)
        return channel

    radio = map_radio_channels(radio, apply_delete_on_right_channel)

    return radio, build_info_content()  # TODO, add logging info
Exemple #7
0
def update_radio(radio: Radio) -> typing.Tuple[Radio, InfoContent]:
    updated_channels = [update_channel(channel) for channel in radio.channels]

    radio = radio._replace(channels=updated_channels)

    return (radio, build_info_content())
Exemple #8
0
def print_status(radio: Radio) -> typing.Tuple[Radio, InfoContent]:
    radio_status = RadioStatus(
        dict((get_channel_id(channel), build_channel_status(channel))
             for channel in radio.channels))

    return radio, build_info_content(result=radio_status)