#!/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 Capture::Tiny qw(capture); use POSIX qw(setsid); use constant { TERMINAL_PATH => '/usr/local/bin/st', 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' }; use constant PROG_LIST => "" . "mpv" . "\n" . "mpv-term" . "\n" . "youtube-dl" . "\n" . "youtube-dl thumbnail" . "\n" . "fetch" . "\n" . "sxiv" . "\n" . "zathura" . "\n" . "w3m" . "\n" . "firefox" . "\n"; sub open_link { my ($a, $url) = @_; my $file_name; my $i; my $pid; my $pwd; my $ret; my $tmp; my @wc; if ($a eq "mpv") { if (system(MPVIEW_PATH, $url) != 0) { system( NOTIFYSEND_PATH, '-u', 'critical', '-t', '10000', 'playback failed', ' Failed to open ' . $url . '' ); } exit 0; } elsif ($a eq "mpv-term") { exec(TERMINAL_PATH, '-e', MPV_PATH, '--audio-channels=stereo', $url); } elsif ($a eq "fetch" || $a eq "youtube-dl") { $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=.+/) { $file_name = `youtube-dl -e $url`; if (not $file_name) { $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++; } $file_name .= "[...]"; } $file_name =~ s/^[0-9]+/\[\.\.\.\]/; system( NOTIFYSEND_PATH, '-u', 'low', '-t', '10000', 'download started', ' '. $file_name . ' started downloading' ); $ret = -1; if ($a eq "youtube-dl") { (undef, undef, $ret) = capture { system(YTDL_PATH, $url); }; } else { (undef, undef, $ret) = capture { system(FETCH_PATH, $url); }; } 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 eq "youtube-dl thumbnail") { $pid = fork(); if (not $pid) { setsid(); chdir '/tmp' or die $!; $file_name = $url; $file_name =~ s/.+\///g; if ($file_name =~ m/^watch\?v=.+/) { $file_name = `youtube-dl -e $url`; if (not $file_name) { $file_name = "Youtube video"; } chomp $file_name; } system( NOTIFYSEND_PATH, '-u', 'low', '-t', '5000', 'download started', ' fetching thumbnail for '. $file_name . '' ); ($tmp, undef, $ret) = capture { system(YTDL_PATH, '--get-thumbnail', $url); }; chomp $tmp; if ($ret != 0) { system( NOTIFYSEND_PATH, '-u', 'critical', '-t', '10000', 'failed to get thumbnail', ' failed to get thumbail for '. $file_name . '' ); exit 0; } system(FETCH_PATH, '-q', $tmp); $file_name = $tmp; $file_name =~ s/.+\///g; chomp $file_name; system(SXIV_PATH, '-b', '-a', $file_name); unlink($file_name); exit 0; } } elsif ($a eq "sxiv" || $a eq "zathura") { $pid = fork(); if (not $pid) { setsid(); chdir '/tmp' or die $!; $file_name = $url; $file_name =~ s/.+\///g; system( NOTIFYSEND_PATH, '-u', 'low', '-t', '5000', 'download started', ' fetching '. $file_name . ' to /tmp' ); system(FETCH_PATH, '-q', $url); if ($a == "sxiv") { exec(SXIV_PATH, '-b', '-a', $file_name); } else { exec(ZATHURA_PATH, $file_name); } } } elsif ($a eq "w3m") { exec(TERMINAL_PATH, '-e', W3M_PATH, $url); } elsif ($a eq "firefox") { $pid = fork(); if (not $pid) { setsid(); capture { exec(FIREFOX_PATH, '--kiosk', $url); }; } } return; } sub dmenu_prompt { my ($url) = @_; my $answer; my $list = PROG_LIST; $answer = `printf "$list\nURL: $url\n" | dmenu -i -l 11`; chomp $answer; return $answer; } sub main { my $answer; if (@ARGV == 0) { print STDERR "linkview: no URL\n"; exit 1; } $answer = dmenu_prompt($ARGV[0]); open_link($answer, $ARGV[0]); return; } main(); __END__