#!/usr/local/bin/perl use strict; use warnings; use Scalar::Util qw(looks_like_number); use File::HomeDir qw(home); use File::Basename qw(basename); use Cwd qw(cwd); use Term::ReadKey; use Term::ANSIColor qw(colored); use constant { MPVIEW_PATH => '/home/jozan/.local/bin/mpview', MPV_PATH => '/usr/local/bin/mpv', YTDL_PATH => '/usr/local/bin/youtube-dl', FETCH_PATH => '/usr/bin/fetch', SXIV_PATH => '/usr/local/bin/sxiv', ZATHURA_PATH => '/usr/local/bin/zathura', W3M_PATH => '/usr/local/bin/w3m', FIREFOX_PATH => '/usr/local/bin/firefox', NOTIFYSEND_PATH => '/usr/local/bin/notify-send' }; sub open_link { my ($a, $url) = @_; my $pid; my $file_name; my $pwd; my $ret; if ($a == 1) { exec( MPVIEW_PATH, $url ); } elsif ($a == 2 || $a == 3) { $pid = fork(); if ($pid == 0) { 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=.+/) { $file_name = `youtube-dl -e $url`; chomp $file_name; } system( NOTIFYSEND_PATH, '-u', 'normal', '-t', '10000', 'download started', ' '. $file_name . ' started downloading' ); $ret = -1; if ($a == 2) { $ret = system(YTDL_PATH . " -q '" . $url . "' 2>/dev/null"); } else { $ret = system(FETCH_PATH . " -q '" . $url . "' 2>/dev/null"); } if ($ret == 0) { system( NOTIFYSEND_PATH, '-u', 'normal', '-t', '10000', 'download complete', ' ' . $file_name . ' downloaded successfully to ' . '' . $pwd . '' ); } else { system( NOTIFYSEND_PATH, '-u', 'critical', '-t', '10000', 'download failed', ' failed to download ' . $file_name . '' ); } exit 0; } } elsif ($a == 4) { exec( SXIV_PATH, '-b', $url ); } elsif ($a == 5) { exec( ZATHURA_PATH, $url ); } elsif ($a == 6) { exec( MPV_PATH, '--really-quiet', '--audio-channels=stereo', '--loop', $url ); } elsif ($a == 7) { exec( W3M_PATH, $url ); } elsif ($a == 8) { $pid = fork(); if (not $pid) { exec(FIREFOX_PATH . ' --kiosk ' . $url . ' >/dev/null 2>&1'); } } elsif ($a == 9) { exec( MPV_PATH, '--audio-channels=stereo', $url ); } return; } sub prompt { my $answer; $answer = -1; print "Open with?\n" . "1) mpv (default)\n" . "2) youtube-dl\n" . "3) fetch\n" . "4) sxiv\n" . "5) zathura\n" . "6) mpv-loop\n" . "7) w3m\n" . "8) firefox\n" . "9) mpv-verbose\n" . "0) quit\n" . "\n" . "> "; open(TTY, " 9 ) { print "linkview: bad answer\n"; return -1; } return $answer; } sub main { my $answer; if (@ARGV == 0) { print STDERR "linkview: no URL\n"; exit 1; } system('tput', 'clear'); print 'URL: ' . colored($ARGV[0], 'red') . "\n\n"; $answer = -1; while ($answer == -1) { $answer = prompt(); } if ($answer == 0) { exit 0; } open_link($answer, $ARGV[0]); return; } main(); __END__