ytv: If no URL arg: Read from stdin. --kodi => run "idok $file".

This commit is contained in:
Ole Tange 2019-02-03 17:50:45 +01:00
parent 1e96940cb0
commit 9d500db854

51
ytv/ytv
View file

@ -9,7 +9,8 @@ ytv - Start downloading youtube video and play it immediately
=head1 SYNOPSIS =head1 SYNOPSIS
B<ytv> [--tor] I<youtube-url> B<ytv> [--tor] [--kodi] I<youtube-url>
cat urlfile | B<ytv> [--tor] [--kodi]
=head1 DESCRIPTION =head1 DESCRIPTION
@ -32,7 +33,7 @@ Use tor proxy to download. It assumes you are running a tor proxy on
=head1 AUTHOR =head1 AUTHOR
Copyright (C) 2017 Ole Tange, Copyright (C) 2017-2019 Ole Tange,
http://ole.tange.dk and Free Software Foundation, Inc. http://ole.tange.dk and Free Software Foundation, Inc.
@ -75,11 +76,19 @@ my @tor = $opt::tor ? qw(--proxy socks4a://127.0.0.1:9050/) : ();
chdir($ENV{'HOME'}."/tmp/Videos"); chdir($ENV{'HOME'}."/tmp/Videos");
map { $before{$_} = -M $_ } <*>; map { $before{$_} = -M $_ } <*>;
my @youtubeid; my @youtubeid;
for (@ARGV) {
play($_)
}
if(not @ARGV) { if(not @ARGV) {
chomp(@ARGV = <>); while(<>) {
chomp;
play($_)
}
} }
for my $url (@ARGV) { sub play {
my $url = shift;
print "Playing $url\n"; print "Playing $url\n";
if(not fork()) { if(not fork()) {
# Download in the background # Download in the background
@ -90,13 +99,19 @@ for my $url (@ARGV) {
do { do {
# Sleep until there are matching files # Sleep until there are matching files
@new = ((grep { -e $_ } $url), @new = (
(grep { !/Frag\d/ } grep { -s $_ > 1000000 } # Remove fragments and subtitles
grep { not $before{$_} or $before{$_} > -M $_ } <*>), grep { !/vtt$|Frag\d/ }
map { my $id = $_; grep { /$id/ } <*> } # Include $url if that is a file
grep { s/^.*watch.v=([a-z0-9]+).*$/$1/i (grep { -e $_ } $url),
or # Look for files > 1M, and that we have not seen before
s/^.*youtu.be.([a-z0-9]+).*$/$1/i } $url); (grep { -s $_ > 1000000 }
grep { not $before{$_} or $before{$_} > -M $_ } <*>),
# Include all files that match the id if $urls is a youtube ID
map { my $id = $_; grep { /$id/ } <*> }
grep { s/^.*watch.v=([a-z0-9]+).*$/$1/i
or
s/^.*youtu.be.([a-z0-9]+).*$/$1/i } $url);
usleep(10); usleep(10);
} until @new; } until @new;
@ -112,12 +127,14 @@ for my $url (@ARGV) {
if(not fork()) { if(not fork()) {
exec("idok", $_); exec("idok", $_);
} }
print "Press ENTER\n"; print "Press (r)etry, (d)elete, (n)ext\n";
open(my $tty_fh, "<", "/dev/tty") || ::die_bug("interactive-tty"); while(not $answer) {
$answer = <$tty_fh>; open(my $tty_fh, "<", "/dev/tty") || ::die_bug("interactive-tty");
close $tty_fh; $answer = <$tty_fh>;
if($answer =~ /d/i) { close $tty_fh;
unlink $_; if($answer =~ /d/i) {
unlink $_;
}
} }
} while($answer =~ /r/i); } while($answer =~ /r/i);
} }