-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.pl
executable file
·43 lines (39 loc) · 1.12 KB
/
app.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::UserAgent;
use Data::Dumper;
my $ua = Mojo::UserAgent->new;
plugin 'CORS';
my $config = plugin 'JSONConfig';
helper parse_meta => sub {
my $c = shift;
my $url = shift;
my $data;
my $tx = $ua->get( $url );
if ( my $res = $tx->success ) {
$data = {
title => $res->dom->at( 'meta[property="og:title"]' )
->attr( 'content' ) || $res->dom->at( 'title' )->text,
teaser => $res->dom->at( 'meta[property="og:description"]' )
->attr( 'content' ),
image => $res->dom->at( 'meta[property="og:image"]' )
->attr( 'content' ),
site => $res->dom->at( 'meta[property="og:site_name"]' )
->attr( 'content' )
};
}
else {
my $err = $tx->error;
$data = {
error => "Connection error: $err->{message}"
};
}
return $data;
};
get '/' => sub {
my $c = shift;
my $url = $c->param( 'url' );
my $data = $c->parse_meta( $url );
$c->render( json => { url => $url, data => $data } );
};
app->start;