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

31
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,9 +99,15 @@ 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 { !/vtt$|Frag\d/ }
# Include $url if that is a file
(grep { -e $_ } $url),
# Look for files > 1M, and that we have not seen before
(grep { -s $_ > 1000000 }
grep { not $before{$_} or $before{$_} > -M $_ } <*>), grep { not $before{$_} or $before{$_} > -M $_ } <*>),
# Include all files that match the id if $urls is a youtube ID
map { my $id = $_; grep { /$id/ } <*> } map { my $id = $_; grep { /$id/ } <*> }
grep { s/^.*watch.v=([a-z0-9]+).*$/$1/i grep { s/^.*watch.v=([a-z0-9]+).*$/$1/i
or or
@ -112,13 +127,15 @@ 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";
while(not $answer) {
open(my $tty_fh, "<", "/dev/tty") || ::die_bug("interactive-tty"); open(my $tty_fh, "<", "/dev/tty") || ::die_bug("interactive-tty");
$answer = <$tty_fh>; $answer = <$tty_fh>;
close $tty_fh; close $tty_fh;
if($answer =~ /d/i) { if($answer =~ /d/i) {
unlink $_; unlink $_;
} }
}
} while($answer =~ /r/i); } while($answer =~ /r/i);
} }
} else { } else {