App-YTDL

 view release on metacpan or  search on metacpan

lib/App/YTDL/Options.pm  view on Meta::CPAN

use File::HomeDir      qw();
use Term::Choose       qw( choose );
use Term::Choose::Util qw( choose_a_directory choose_a_file choose_a_number settings_menu insert_sep );
use Term::Form         qw();

use App::YTDL::ChooseVideos qw( set_sort_videolist );
use App::YTDL::Helper       qw( write_json read_json uni_capture HIDE_CURSOR SHOW_CURSOR );


sub _show_info {
    my ( $set, $opt ) = @_;
    my ( $youtube_dl_version, $ffmpeg_version, $ffprobe_version );
    if ( ! eval { $youtube_dl_version = uni_capture( @{$set->{youtube_dl}}, '--version' ); 1 } ) {
        $youtube_dl_version = $@;
    }
    chomp $youtube_dl_version;
    eval {
        $ffmpeg_version = uni_capture( $set->{ffmpeg}, '-version' );
        if ( $ffmpeg_version && $ffmpeg_version =~ /^ffmpeg version (\S+)/m ) {
            $ffmpeg_version = $1;
        }
        else {
            $ffmpeg_version = '';
        }
    };
    eval {
        $ffprobe_version = uni_capture( $set->{ffprobe}, '-version' );
        if ( $ffprobe_version && $ffprobe_version =~ /^ffprobe version (\S+)/m ) {
            $ffprobe_version = $1;
        }
        else {
            $ffprobe_version = '';
        }
    };
    my $prompt = "INFO\n\n";
    $prompt .= "Directories:\n";
    $prompt .= "    Video : $opt->{video_dir}"   . "\n";
    $prompt .= "    Config: $set->{config_file}" . "\n";
    $prompt .= "\n";
    $prompt .= "Versions:\n";
    $prompt .= '    ' . catfile( $RealBin, $RealScript ) . ": " . $main::VERSION                 . "\n";
    $prompt .= '    ' . $set->{youtube_dl}[0]            . ": " . ( $youtube_dl_version // '?' ) . "\n";
    if ( $set->{ffmpeg} ) {
        $prompt .= '    ' . $set->{ffmpeg} . " : " . ( $ffmpeg_version // '?' ) . "\n";
    }
    if ( $set->{ffprobe} ) {
        $prompt .= '    ' . $set->{ffprobe} . ": " . ( $ffprobe_version // '?' ) . "\n";
    }
    # Choose
    choose(
        [ 'Close' ],
        { prompt => $prompt }
    );
}


sub get_defaults {
    return {
        entries_with_info            => 0,
        filename_format              => 3,
        list_sort_item               => 'upload_date',
        max_info_width               => 120,
        max_processes                => 5,
        max_rows_description         => 3,
        max_size_history             => 50,
        no_height_ok                 => 1,
        no_warnings                  => 0,
        prefer_free_formats          => 1,
        quality                      => 'manually',
        retries                      => 7,
        show_video_id                => 0,
        show_view_count              => 0,
        sort_history_by_timestamp    => 1,
        timeout                      => 60,
        useragent                    => 'Mozilla/5.0',
        use_extractor_dir            => 1,
        use_uploader_dir             => 1,
        video_dir                    => File::HomeDir->my_videos() // File::HomeDir->my_home() // curdir,
        yt_dl_config_location        => undef,
        yt_dl_ignore_config          => 0,
    };
}


sub _main {
    return [
        [ 'show_help_text',          "  HELP"                   ],
        [ 'show_info',               "  INFO"                   ],
        [ 'group_directory',         "- Directories"            ],
        [ 'group_quality',           "- Video format"           ],
        [ 'group_download',          "- Download"               ],
        [ 'group_history',           "- History"                ],
        [ 'group_list_menu',         "- Uploader video list"    ],
        [ 'group_output',            "- Info output"            ],
        [ 'group_yt_dl_config_file', "- Youtube-dl config file" ],
        [ 'avail_extractors',        "  List extractors"        ],
        [ 'extractor_descriptions',  "  Extractor descriptions" ],
    ];
}


sub _groups {
    return {
        group_directory => [
            [ 'video_dir',         "- Main video directory" ],
            [ 'use_extractor_dir', "- Extractor directory"  ],
            [ 'use_uploader_dir',  "- Uploader directory"   ],
            [ 'filename_format',   "- Filename format info" ],
        ],
        group_quality => [
            [ 'quality',             "- Resolution"          ],
            [ 'no_height_ok',        "- No video height"     ],
            [ 'prefer_free_formats', "- Prefer free formats" ],
        ],
        group_download => [
            [ 'useragent',  "- UserAgent"        ],
            [ 'retries',    "- Download retries" ],
            [ 'timeout',    "- Timeout"          ],
        ],
        group_output => [
            [ 'no_warnings',          "- Disable warnings" ],
            [ 'max_info_width',       "- Max info width"   ],
            [ 'max_rows_description', "- Max rows of description" ],
        ],
        group_history => [
            [ 'max_size_history',          "- Size history" ],
            [ 'sort_history_by_timestamp', "- History sort" ],
        ],
        group_list_menu => [
            [ 'entries_with_info', "- Additional info" ],
            [ 'max_processes',     "- Max processes"   ],
            [ 'list_sort_item',    "- Sort order"      ],
            [ 'show_view_count',   "- Show view count" ],
            [ 'show_video_id',     "- Show video id"   ],
        ],
        group_yt_dl_config_file => [
            [ 'yt_dl_config_location',        "- set yt-dl config location"   ],
            [ '_reset_yt_dl_config_location', "- reset yt-dl config location" ],
            [ 'yt_dl_ignore_config',          "- Ignore yt-dl config file"    ],
        ],
    };
}


sub set_options {
    my ( $set, $opt ) = @_;
    my $main = _main();
    my $old_idx_main = 0;

    MAIN_MENU: while ( 1 ) {
        my $back_main     = '  QUIT';
        my $continue_main = '  CONTINUE';
        my @pre  = ( undef, $continue_main );
        my @choices_main = map( $_->[1], @$main );
        my $menu_main = [ @pre, @choices_main ];
        # Choose
        my $idx_main = choose(
            $menu_main,
            { prompt => 'Options:', layout => 3, index => 1, clear_screen => 1,
              undef => $back_main, default => $old_idx_main }
        );
        if ( ! defined $idx_main || ! defined $menu_main->[$idx_main] ) {
            if ( $set->{change} ) {
                _write_config_file( $opt, $set->{config_file} );
                delete $set->{change};
            }
            exit;

lib/App/YTDL/Options.pm  view on Meta::CPAN

                    choose(
                        [ map { "  $_" } @capture ],
                        { layout => 3 }
                    );
                }
            }
            elsif ( $main_key eq "extractor_descriptions" ) {
                say 'Close with ENTER';
                my @capture;
                if ( ! eval { @capture = uni_capture( @{$set->{youtube_dl}}, '--extractor-descriptions' ); 1 } ) {
                    say "Extractor descriptions: $@";
                }
                if ( @capture ) {
                    # Choose
                    choose(
                        [ map { '  ' . decode( 'UTF-8', $_ ) } @capture ],
                        { layout => 3 }
                    );
                }
            }
            next MAIN_MENU;
        }
        my $groups = _groups();
        my $group = $groups->{$main_key};
        my $old_idx_group = 0;

        GROUP: while ( 1 ) {
            my $back_group     = '  <<';
            my $continue_group = '  CONTINUE';
            my @pre  = ( undef );
            my @choices_group = map( $_->[1], @$group );
            my $menu_group = [ @pre, @choices_group ];
            # Choose
            my $idx_group = choose(
                $menu_group,
                { prompt => $main_prompt, layout => 3, index => 1, clear_screen => 1, undef => $back_group, default => $old_idx_group }
            );
            if ( ! defined $idx_group || ! defined $menu_group->[$idx_group] ) {
                next MAIN_MENU;
            }
            if ( $old_idx_group == $idx_group ) {
                $old_idx_group = 0;
                next GROUP;
            }
            $old_idx_group = $idx_group;
            my $choice = $menu_group->[$idx_group];
            if ( $choice eq $continue_group ) {
                next MAIN_MENU;
            }
            my $key = $group->[$idx_group-@pre][0];
            if ( $key eq "video_dir" ) {
                my $info = "Video Directory\nNow: " . $opt->{$key} // 'current directory';
                my $name = 'New: ';
                _opt_choose_a_directory( $set, $opt, $key, $info, $name );
            }
            elsif ( $key eq "use_extractor_dir" ) {
                my $prompt = 'Use extractor directories';
                my $list = [ 'no', 'yes' ];
                _opt_choose_from_list_idx( $set, $opt, $key, $prompt, $list );
            }
            elsif ( $key eq "use_uploader_dir" ) {
                my $prompt = 'Use uploader directories';
                my $list = [
                    'no',
                    'yes',
                    'if from list-menu',
                ];
                _opt_choose_from_list_idx( $set, $opt, $key, $prompt, $list );
            }
            elsif ( $key eq "filename_format" ) {
                my $prompt = 'Filename: choose the format info:';
                my $list = [
                    'none (unsafe)',
                    'video-height (unsafe)',
                    'fmt-id',
                    'fmt-id and video-height',
                    'fmt-string',
                ];
                _opt_choose_from_list_idx( $set, $opt, $key, $prompt, $list );
            }
            elsif ( $key eq "useragent" ) {
                my $prompt = 'Set the UserAgent';
                my $list = [
                    [ 'UserAgent', $opt->{useragent} ]
                ];
                _local_readline( $set, $opt, $key, $prompt, $list );
                $opt->{useragent} = '' if $opt->{useragent} eq '""';
            }
            elsif ( $key eq "quality" ) {
                my $prompt = 'Video resolution';
                my $list = [
                    'manually',
                    ' 180 or less',
                    ' 360 or less',
                    ' 720 or less',
                    '1080 or less',
                    '1440 or less',
                    '2160 or less',
                    'best'
                ];
                _opt_choose_from_list_value( $set, $opt, $key, $prompt, $list );
            }
            elsif ( $key eq "no_height_ok" ) {
                my $prompt = 'Download videos with unkown height';
                my $list = [ 'no', 'yes' ];
                _opt_choose_from_list_idx( $set, $opt, $key, $prompt, $list );
            }
            elsif ( $key eq "retries" ) {
                my $digits = 2;
                my $info = sprintf "Download retries\nNow: %${digits}s", insert_sep( $opt->{$key} );
                my $name = 'New: ';
                _opt_number_range( $set, $opt, $key, $name, $info, $digits );
            }
            elsif ( $key eq "timeout" ) {
                my $digits = 3;
                my $info = sprintf "Connection timeout (s)\nNow: %${digits}s", insert_sep( $opt->{$key} );
                my $name = 'New: ';
                _opt_number_range( $set, $opt, $key, $name, $info, $digits );
            }
            elsif ( $key eq "no_warnings" ) {
                my $prompt = 'Disable youtube-dl warnings';
                my $list = [ 'no', 'yes' ];



( run in 1.363 second using v1.01-cache-2.11-cpan-b16cb0d3907 )