diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2021-07-17 15:21:19 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2021-07-17 15:21:19 +0200 |
commit | 27d3bf49286afd8b2ce5f8eaf4f8ef25606fbc50 (patch) | |
tree | def0ef04960c9c8785b9e12358b24253b9c8331f /.local/bin/linkview | |
parent | fbsd 13 update (diff) | |
download | dotfiles-bsd-27d3bf49286afd8b2ce5f8eaf4f8ef25606fbc50.tar.gz dotfiles-bsd-27d3bf49286afd8b2ce5f8eaf4f8ef25606fbc50.tar.bz2 dotfiles-bsd-27d3bf49286afd8b2ce5f8eaf4f8ef25606fbc50.tar.xz dotfiles-bsd-27d3bf49286afd8b2ce5f8eaf4f8ef25606fbc50.tar.zst dotfiles-bsd-27d3bf49286afd8b2ce5f8eaf4f8ef25606fbc50.zip |
bug fixes, new quality selection menu
Diffstat (limited to '.local/bin/linkview')
-rwxr-xr-x | .local/bin/linkview | 242 |
1 files changed, 174 insertions, 68 deletions
diff --git a/.local/bin/linkview b/.local/bin/linkview index 0653b27..83b7cc8 100755 --- a/.local/bin/linkview +++ b/.local/bin/linkview @@ -32,14 +32,26 @@ use constant PROG_LIST => "" . "zathura" . "\n" . "w3m" . "\n" . "firefox" . "\n"; +use constant QUAL_LIST => "" . +"1080" . "\n" . +"720" . "\n" . +"480" . "\n" . +"360" . "\n" . +"240" . "\n" . +"144" . "\n" . +"uncap" . "\n"; sub open_link { my ($a, $url) = @_; + my $count; my $file_name; my $i; + my $list; my $pid; + my $pid2; my $pwd; + my $quality; my $ret; my $tmp; my $tmpfile; @@ -65,95 +77,189 @@ sub open_link $pid = fork(); if (not $pid) { setsid(); - chdir home() . "/dl" or - chdir home() . "/Downloads" or - chdir home() or die $!; - if (basename(cwd()) eq basename(home())) { - $pwd = '~/'; - } - else { - $pwd = '~/' . basename(cwd()); - } - $file_name = $url; - $file_name =~ s/.+\///g; - if ($file_name =~ m/^watch\?v=.+/) { + $ret = -1; + if ($a eq "youtube-dl") { + $list = QUAL_LIST; + $quality = `printf "$list" | dmenu -i -l 7 -m 0`; + if (not $quality) { + exit 0; + } + chomp $quality; + chdir home() . "/dl" or + chdir home() . "/Downloads" or + chdir home() or die $!; + if (basename(cwd()) eq basename(home())) { + $pwd = '~/'; + } + else { + $pwd = '~/' . basename(cwd()); + } + $file_name = $url; + $file_name =~ s/.+\///g; $file_name = `youtube-dl -e $url`; if (not $file_name) { - $file_name = "Youtube video"; + $file_name = "YouTube video"; } chomp $file_name; - } - @wc = split / /, $file_name; - if (@wc > 8) { - $file_name = ""; - $i = 0; - while ($i < 8) { - $file_name .= "$wc[$i] "; - $i++; + @wc = split / /, $file_name; + if (@wc > 8) { + $file_name = ""; + $i = 0; + while ($i < 8) { + $file_name .= "$wc[$i] "; + $i++; + } + $file_name .= "[...]"; } - $file_name .= "[...]"; - } - $file_name =~ s/^[0-9]+/\[\.\.\.\]/; - system( - NOTIFYSEND_PATH, - '-u', 'low', - '-t', '10000', - 'download started', - ' <b>' . $file_name . '</b> started downloading' + $file_name =~ s/^[0-9]+/\[\.\.\.\]/; + system( + NOTIFYSEND_PATH, + '-u', 'low', + '-t', '2000', + 'download started', + ' <b>' . $file_name . '</b> started downloading' ); - $ret = -1; - if ($a eq "youtube-dl") { $tmpfile = `mktemp`; chomp $tmpfile; - (undef, undef, $ret) = capture { - system(YTDL_PATH . ' --newline --add-metadata ' . $url . ' >' . $tmpfile . ' &'); - }; - while (1) { - $val = `tail -n 1 "$tmpfile" | awk '{print \$2}' | tr -d '%'`; - if (looks_like_number($val) != 0) { - system( + $pid2 = fork(); + if (not $pid2) { + (undef, undef, $ret) = capture { + if ($quality eq 'uncap') { + system(YTDL_PATH . ' --newline --add-metadata ' . $url . ' >' . $tmpfile); + } + else { + system(YTDL_PATH . " -f '[height<=" . $quality . "]' --newline --add-metadata " . $url . ' >' . $tmpfile); + } + }; + if ($ret == 0) { + system( + NOTIFYSEND_PATH, + '-u', 'normal', + '-t', '10000', + 'download complete', + ' <b>' . $file_name . '</b> downloaded successfully to ' . + '<b>' . $pwd . '</b>' + ); + } + else { + system( NOTIFYSEND_PATH, - '-h', - 'int:value:' . $val, - '-u', 'low', - '-t', '5000', - 'downloading', - ' downloading <b>' . $file_name . '</b>' + '-u', 'critical', + '-t', '10000', + 'download failed', + ' failed to download <b>' . $file_name . '</b>' + ); + } + exit 0; + } + elsif ($pid2 < 0) { + system( + NOTIFYSEND_PATH, + '-u', 'critical', + '-t', '10000', + 'download failed', + ' failed to <b>fork(2)</b>' ); - if ($val == 100 || system('pgrep python >/dev/null 2>&1') != 0) { - last; + exit 0; + } + else { + $count = 0; + while (1) { + $val = `tail -n 1 "$tmpfile" | awk '{print \$2}' | tr -d '%'`; + if (looks_like_number($val) != 0) { + system( + NOTIFYSEND_PATH, + '-h', + 'int:value:' . $val, + '-u', 'low', + '-t', '5000', + 'downloading', + ' downloading <b>' . $file_name . '</b>' + ); + if ($val == 100 || system('pgrep python >/dev/null 2>&1') != 0) { + last; + } + } + else { + if ($count == 10) { + last; + } + $count += 1; } + sleep 2; } - sleep 2; + unlink $tmpfile; } - unlink $tmpfile; - } - else { - (undef, undef, $ret) = capture { - system(FETCH_PATH, $url); - }; - } - if ($ret == 0) { - system( - NOTIFYSEND_PATH, - '-u', 'normal', - '-t', '10000', - 'download complete', - ' <b>' . $file_name . '</b> downloaded successfully to ' . - '<b>' . $pwd . '</b>' - ); + exit 0; } else { + chdir home() . "/dl" or + chdir home() . "/Downloads" or + chdir home() or die $!; + if (basename(cwd()) eq basename(home())) { + $pwd = '~/'; + } + else { + $pwd = '~/' . basename(cwd()); + } + $file_name = $url; + $file_name =~ s/.+\///g; + @wc = split / /, $file_name; + if (@wc > 8) { + $file_name = ""; + $i = 0; + while ($i < 8) { + $file_name .= "$wc[$i] "; + $i++; + } + $file_name .= "[...]"; + } + $file_name =~ s/^[0-9]+/\[\.\.\.\]/; system( NOTIFYSEND_PATH, - '-u', 'critical', + '-u', 'low', '-t', '10000', - 'download failed', - ' failed to download <b>' . $file_name . '</b>' - ); + 'download started', + ' <b>' . $file_name . '</b> started downloading' + ); + (undef, undef, $ret) = capture { + system(FETCH_PATH, $url); + }; + if ($ret == 0) { + system( + NOTIFYSEND_PATH, + '-u', 'normal', + '-t', '10000', + 'download complete', + ' <b>' . $file_name . '</b> downloaded successfully to ' . + '<b>' . $pwd . '</b>' + ); + } + else { + system( + NOTIFYSEND_PATH, + '-u', 'critical', + '-t', '10000', + 'download failed', + ' failed to download <b>' . $file_name . '</b>' + ); + } } exit 0; } + elsif ($pid < 0) { + system( + NOTIFYSEND_PATH, + '-u', 'critical', + '-t', '10000', + 'download failed', + ' failed to <b>fork(2)</b>' + ); + exit 0; + } + else { + exit 0; + } } elsif ($a eq "youtube-dl thumbnail") { $pid = fork(); |