ytv: Redone.

This commit is contained in:
Ole Tange 2017-06-29 19:12:57 +02:00
parent ddd10e4c13
commit 0e143651fc

42
ytv/ytv
View file

@ -55,51 +55,27 @@ B<ytv>, B<vlc>
`mkdir -p ~/tmp/Videos`; `mkdir -p ~/tmp/Videos`;
chdir($ENV{'HOME'}."/tmp/Videos"); chdir($ENV{'HOME'}."/tmp/Videos");
map { $before{$_} = -M $_ } <*>;
for my $url (@ARGV) { for my $url (@ARGV) {
if(not fork()) { if(not fork()) {
# Download in the background # Download in the background
system(qw(youtube-dl -f best),$url); system(qw(youtube-dl -f best),$url);
exit; exit;
} }
# https://www.youtube.com/watch?v=ID
if($url =~ m{https://www.youtube.com/watch\?v=([^&]+)}) {
# Get the ID from the url
push @id,$1;
$id{$1}++;
} else {
print STDERR "$url not matched\n";
}
} }
sub match_arr_regexp { $SIG{'CHLD'} = sub { exit; };
# Match any of @regexp
my ($regexp_arr,$content_arr) = @_;
# my $m;
# return map { $m = $_; grep { /$m/ } @$content_arr } @$regexp_arr;
my $regexp = join'|',map { "($_)" } @$regexp_arr;
return grep { /$regexp/ } @$content_arr;
}
my (@ls,@files);
do { do {
# Sleep until there are matching files # Sleep until there are matching files
sleep(1); sleep(1);
@ls = <*>; @new = grep { not $before{$_} or $before{$_} > -M $_ } <*>;
@files = match_arr_regexp(\@id,\@ls); } until @new;
} until @files;
while(@files) { while(@new) {
# Mark as seen
map { $before{$_} = -M $_ } @new;
# Run VLC on the matching files # Run VLC on the matching files
system("vlc",@files); system("vlc",@new);
# Remove regexps from @id @new = grep { not $before{$_} or $before{$_} > -M $_ } <*>;
for my $id (@id) {
if(grep { /$id/ } @ls) {
$id{$id} = 0;
}
}
@id = grep { $id{$_} } @id;
# See if there are new matches
@ls = <*>;
@files = match_arr_regexp(\@id,\@ls);
} }