LinkEmbedder
view release on metacpan or search on metacpan
MAINTAINER jhthorsen@cpan.org
RUN apk add -U perl perl-io-socket-ssl \
&& apk add -t builddeps build-base curl perl-dev wget \
&& curl -L https://github.com/jhthorsen/linkembedder/archive/master.tar.gz | tar xvz \
&& curl -L https://cpanmin.us | perl - App::cpanminus \
&& cpanm -M https://cpan.metacpan.org --installdeps ./linkembedder-master \
&& apk del builddeps curl \
&& rm -rf /root/.cpanm /var/cache/apk/*
ENV LINK_EMBEDDER_RESTRICTED 1
ENV MOJO_MODE production
ENV PERL5LIB /linkembedder-master/lib
EXPOSE 8080
ENTRYPOINT ["perl", "/linkembedder-master/examples/embedder.pl", "prefork", "-l", "http://*:8080"]
examples/embedder.pl view on Meta::CPAN
$ENV{TEST_SHADOWCAT} ||= '586840';
helper embedder => sub { state $e = LinkEmbedder->new };
get '/' => 'index';
get '/oembed' => sub {
my $c = shift;
my $url = $c->param('url');
if ($c->stash('restricted') and !grep { $_ eq $url } @{$c->stash('predefined')}) {
return $c->render(json => {error => "LINK_EMBEDDER_RESTRICTED is set."});
}
$c->embedder->serve($c);
};
app->defaults(
restricted => $ENV{LINK_EMBEDDER_RESTRICTED} ? 1 : 0,
predefined => [
"https://xkcd.com/927",
"https://catoverflow.com/cats/r4cIt4z.gif",
"https://www.ted.com/talks/jill_bolte_taylor_s_powerful_stroke_of_insight",
"https://imgur.com/gallery/ohL3e",
"https://www.aftenposten.no",
"https://www.instagram.com/p/BSRYg_Sgbqe/",
"http://ix.io",
"http://ix.io/fpW",
"https://catoverflow.com/",
lib/LinkEmbedder.pm view on Meta::CPAN
use Mojo::Base -base;
use LinkEmbedder::Link;
use Mojo::JSON;
use Mojo::Loader 'load_class';
use Mojo::Promise;
use Mojo::UserAgent;
use constant TLS => eval { require IO::Socket::SSL; IO::Socket::SSL->VERSION('2.009'); 1 };
use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
our $VERSION = '1.20';
my $PROTOCOL_RE = qr!^(\w+):\w+!i; # Examples: mail:, spotify:, ...
has force_secure => sub { $ENV{LINK_EMBEDDER_FORCE_SECURE} || 0 };
has ua => sub { Mojo::UserAgent->new->max_redirects(3); };
has url_to_link => sub {
return {
'default' => 'LinkEmbedder::Link::Basic',
'dropbox.com' => 'LinkEmbedder::Link::Dropbox',
'git.io' => 'LinkEmbedder::Link::Github',
'github.com' => 'LinkEmbedder::Link::Github',
'google' => 'LinkEmbedder::Link::Google',
lib/LinkEmbedder/Link.pm view on Meta::CPAN
package LinkEmbedder::Link;
use Mojo::Base -base;
use Mojo::Template;
use Mojo::Util qw(html_unescape trim);
use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
my %DOM_SEL = (
':desc' => ['meta[property="og:description"]', 'meta[name="twitter:description"]', 'meta[name="description"]'],
':image' => ['meta[property="og:image"]', 'meta[property="og:image:url"]', 'meta[name="twitter:image"]'],
':site_name' => ['meta[property="og:site_name"]', 'meta[property="twitter:site"]'],
':title' => ['meta[property="og:title"]', 'meta[name="twitter:title"]', 'title'],
);
my @JSON_ATTRS = (
'author_name', 'author_url', 'cache_age', 'height', 'provider_name', 'provider_url',
lib/LinkEmbedder/Link/Github.pm view on Meta::CPAN
package LinkEmbedder::Link::Github;
use Mojo::Base 'LinkEmbedder::Link';
use Mojo::Util qw(html_unescape trim);
use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
has provider_name => 'GitHub';
has provider_url => sub { Mojo::URL->new('https://github.com') };
sub learn_p {
my $self = shift;
return $self->url =~ m!gist\.github\.com/(.+)! ? $self->_learn_from_gist_p($1) : $self->SUPER::learn_p;
}
sub _learn_from_dom {
lib/LinkEmbedder/Link/OpenSUSE.pm view on Meta::CPAN
package LinkEmbedder::Link::OpenSUSE;
use Mojo::Base 'LinkEmbedder::Link';
use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
has provider_name => 'openSUSE';
has _paste => undef;
sub learn_p {
my $self = shift;
my $url = $self->url;
my $fetch = $url->host eq 'paste.opensuse.org' && @{$url->path} && $url->path->[-1] =~ /^(\d+)$/;
return $fetch ? $self->_fetch_paste($1) : $self->SUPER::learn_p;
lib/LinkEmbedder/Link/Shadowcat.pm view on Meta::CPAN
package LinkEmbedder::Link::Shadowcat;
use Mojo::Base 'LinkEmbedder::Link';
use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
sub force_secure {0}
has provider_name => 'Shadowcat';
has provider_url => sub { Mojo::URL->new('https://shadow.cat/') };
sub learn_p {
my $self = shift;
my $path = $self->url->path;
return $self->_fetch_paste($1) if @$path and $path->[-1] =~ /^(\d+)$/;
lib/LinkEmbedder/Link/Travis.pm view on Meta::CPAN
package LinkEmbedder::Link::Travis;
use Mojo::Base 'LinkEmbedder::Link';
has provider_name => 'Travis';
has provider_url => sub { Mojo::URL->new('https://travis-ci.org') };
has thumbnail_height => 501;
has thumbnail_url => $ENV{LINK_EMBEDDER_TRAVIS_THUMBNAIL_URL}
|| 'https://cdn.travis-ci.org/images/logos/TravisCI-Mascot-1-20feeadb48fc2492ba741d89cb5a5c8a.png';
has thumbnail_width => 497;
sub learn_p {
my $self = shift;
my $api_url = $self->url->clone->host('api.travis-ci.org');
my $api_path = $api_url->path;
return $self->SUPER::learn_p unless $api_path =~ m!^/(.*/builds/\d+)$!;
lib/LinkEmbedder/Link/oEmbed.pm view on Meta::CPAN
package LinkEmbedder::Link::oEmbed;
use Mojo::Base 'LinkEmbedder::Link';
use constant DEBUG => $ENV{LINK_EMBEDDER_DEBUG} || 0;
require LinkEmbedder;
# please report back if you add more urls to this hash
our %API = (
'ted.com' => 'https://www.ted.com/services/v1/oembed.json',
'vimeo.com' => 'https://vimeo.com/api/oembed.json',
'youtube.com' => 'https://www.youtube.com/oembed',
'youtu.be' => 'https://www.youtube.com/oembed',
);
use Mojo::Base -strict;
use Test::Mojo;
use Test::More;
use LinkEmbedder;
$ENV{LINK_EMBEDDER_FORCE_SECURE} = 0;
my $link;
my $embedder = LinkEmbedder->new;
isa_ok($embedder->ua, 'Mojo::UserAgent');
use Mojolicious::Lite;
get '/example' => 'example';
get
'/oembed' => [format => [qw(html json jsonp)]],
{format => undef}, sub { $embedder->serve(shift) };
( run in 1.372 second using v1.01-cache-2.11-cpan-71847e10f99 )