ytv: Read files/urls from STDIN if not given on the command line.

This commit is contained in:
Ole Tange 2018-12-13 21:03:25 +01:00
parent 5fa560e7cb
commit d95d465850

72
ytv/ytv
View file

@ -74,30 +74,58 @@ my @tor = $opt::tor ? qw(--proxy socks4a://127.0.0.1:9050/) : ();
`mkdir -p ~/tmp/Videos`; `mkdir -p ~/tmp/Videos`;
chdir($ENV{'HOME'}."/tmp/Videos"); chdir($ENV{'HOME'}."/tmp/Videos");
map { $before{$_} = -M $_ } <*>; map { $before{$_} = -M $_ } <*>;
my @youtubeid;
if(not @ARGV) {
chomp(@ARGV = <>);
}
for my $url (@ARGV) { for my $url (@ARGV) {
print "Playing $url\n";
if(not fork()) { if(not fork()) {
# Download in the background # Download in the background
system(qw(youtube-dl --all-subs --skip-download), @tor,$url); system(qw(youtube-dl --all-subs --skip-download), @tor,$url);
system(qw(youtube-dl -f best), @tor,$url); system(qw(youtube-dl -f best), @tor,$url);
exit; exit;
} }
}
$SIG{'CHLD'} = sub { exit; }; do {
# Sleep until there are matching files
@new = ((grep { -e $_ } $url),
(grep { !/Frag\d/ } grep { -s $_ > 1000000 }
grep { not $before{$_} or $before{$_} > -M $_ } <*>),
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);
} until @new;
do { while(@new) {
# Sleep until there are matching files # Mark as seen
sleep(1); map { $before{$_} = -M $_ } @new;
@new = (grep { !/Frag\d/ } grep { -s $_ > 1000000 } # Run VLC on the matching files
grep { not $before{$_} or $before{$_} > -M $_ } <*>); if($opt::kodi) {
} until @new; for(uniq(map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @new)) {
my $answer;
while(@new) { do {
# Mark as seen print "KODI playing $_\n";
map { $before{$_} = -M $_ } @new; if(not fork()) {
# Run VLC on the matching files exec("idok", $_);
system("vlc", map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @new); }
@new = grep { not $before{$_} or $before{$_} > -M $_ } <*>; print "Press ENTER\n";
open(my $tty_fh, "<", "/dev/tty") || ::die_bug("interactive-tty");
$answer = <$tty_fh>;
close $tty_fh;
if($answer =~ /d/i) {
unlink $_;
}
} while($answer =~ /r/i);
}
} else {
system("vlc", map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @new);
}
@new = grep { not $before{$_} or $before{$_} > -M $_ } <*>;
}
} }
sub get_options_from_array { sub get_options_from_array {
@ -118,6 +146,7 @@ sub get_options_from_array {
my @retval = GetOptions my @retval = GetOptions
("debug|D" => \$opt::debug, ("debug|D" => \$opt::debug,
"tor" => \$opt::tor, "tor" => \$opt::tor,
"kodi|k" => \$opt::kodi,
"version|V" => \$opt::version, "version|V" => \$opt::version,
); );
if(not $this_is_ARGV) { if(not $this_is_ARGV) {
@ -126,3 +155,16 @@ sub get_options_from_array {
} }
return @retval; return @retval;
} }
sub usleep {
# Sleep this many milliseconds.
# Input:
# $ms = milliseconds to sleep
my $ms = shift;
select(undef, undef, undef, $ms/1000);
}
sub uniq {
# Remove duplicates and return unique values
return keys %{{ map { $_ => 1 } @_ }};
}