Created
June 8, 2018 17:04
-
-
Save robhammond/7d4dbc1fbad2fe54f308db85e62751f8 to your computer and use it in GitHub Desktop.
Grab metadata from images contained in URLs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Modern::Perl; | |
use Mojo::UserAgent; | |
use Mojo::JSON qw(decode_json); | |
use Image::Size qw(imgsize); | |
use POSIX; | |
my $urls = [...]; | |
my $ua = Mojo::UserAgent->new; | |
for my $url (@$urls) { | |
my $tx = $ua->get($url); | |
say "----------------------------------------------------"; | |
say $url; | |
say "----------------------------------------------------"; | |
if (my $res = $tx->success) { | |
if ($res->dom->at('meta[property="og:image"]')) { | |
say "Social image"; | |
my $img_url = $res->dom->at('meta[property="og:image"]')->{'content'}; | |
say " $img_url"; | |
say " " . get_img($img_url); | |
} | |
} | |
} | |
sub get_img { | |
my $img_url = shift; | |
my $ua = Mojo::UserAgent->new; | |
my $img = $ua->get($img_url)->result->body; | |
my ($width, $height) = imgsize(\$img); | |
my $aspect = $width / $height; | |
$aspect = float2rat($aspect); | |
my $dims = "$width " . 'x' . " $height\n $aspect\n-------"; | |
return $dims; | |
} | |
sub float2rat { | |
my $x = shift; | |
my $tolerance = 1.0E-6; | |
my $h1 = 1; | |
my $h2 = 0; | |
my $k1 = 0; | |
my $k2 = 1; | |
my $b = $x; | |
do { | |
my $a = floor($b); | |
my $aux = $h1; | |
$h1 = $a * $h1 + $h2; | |
$h2 = $aux; | |
$aux = $k1; | |
$k1 = $a * $k1 + $k2; | |
$k2 = $aux; | |
$b = 1/($b-$a); | |
} | |
while (abs($x - $h1 / $k1) > $x * $tolerance); | |
return $h1 . "/" . $k1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment