Skip to content

Instantly share code, notes, and snippets.

@jacoby
Created December 7, 2017 22:09
Show Gist options
  • Save jacoby/7ae78d677195bbd374fc16cc4d470ae4 to your computer and use it in GitHub Desktop.
Save jacoby/7ae78d677195bbd374fc16cc4d470ae4 to your computer and use it in GitHub Desktop.
Perl program that takes a list of YouTube URLs and makes markdown links with the video's thumbnail as the link and the video title as the alt text.
#!/usr/bin/env perl
use strict ;
use warnings ;
use utf8 ;
use feature qw{ postderef say signatures state } ;
no warnings qw{ experimental::postderef experimental::signatures } ;
use Mojo::UserAgent ;
map { make_md( $_ ) } @ARGV ;
sub make_md ( $string ) {
return unless $string =~ m{^https?://www.youtube.com}mix ;
my ( $id ) = $string =~ m{v=([^\?]+)} ;
my $link = qq{https://www.youtube.com/watch?v=$id} ;
my $thumb = qq{https://img.youtube.com/vi/$id/0.jpg} ;
my $title = 'Hover Text' ;
my $dom = Mojo::UserAgent->new->get( $link )->res->dom ;
$dom->find( 'title' )->each(
sub {
my $e = $_ ;
$title = $e->text ;
}
) ;
say qq{[ ![$title]($thumb)]( $link ) } ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment