From ddfa18ba7fbb05f38f1eb58e13aa1a9b10b39fc3 Mon Sep 17 00:00:00 2001 From: Jan Henning Thorsen Date: Thu, 17 Sep 2020 10:54:34 +0900 Subject: [PATCH] Add support for Facebook video, closes #9 --- Changes | 1 + lib/LinkEmbedder.pm | 45 ++++++++++++++++--------------- lib/LinkEmbedder/Link/Facebook.pm | 28 +++++++++++++++++++ t/facebook.t | 29 ++++++++++++++++++++ 4 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 lib/LinkEmbedder/Link/Facebook.pm create mode 100644 t/facebook.t diff --git a/Changes b/Changes index 4883041..de4be46 100644 --- a/Changes +++ b/Changes @@ -5,6 +5,7 @@ Revision history for perl distribution LinkEmbedder - Fix parsing metacpan.org links - Fix parsing pastebini.com links - Fix parsing xkcd.com links + - Add support for Facebook video #9 - Add support for extracting "application/ld+json" information - Add support for paste.centos.org (replaced fedoraproject paste) - Changed to use twitframe.com to embed twitter posts diff --git a/lib/LinkEmbedder.pm b/lib/LinkEmbedder.pm index 51661df..cbb7e70 100644 --- a/lib/LinkEmbedder.pm +++ b/lib/LinkEmbedder.pm @@ -21,28 +21,29 @@ 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', - 'goo.gl' => 'LinkEmbedder::Link::Google', - 'imgur.com' => 'LinkEmbedder::Link::Imgur', - 'ix.io' => 'LinkEmbedder::Link::Ix', - 'instagram.com' => 'LinkEmbedder::Link::oEmbed', - 'metacpan.org' => 'LinkEmbedder::Link::Metacpan', - 'nhl.com' => 'LinkEmbedder::Link::NHL', - 'paste.opensuse.org' => 'LinkEmbedder::Link::OpenSUSE', - 'paste.scsys.co.uk' => 'LinkEmbedder::Link::Shadowcat', - 'pastebin.com' => 'LinkEmbedder::Link::Pastebin', - 'spotify' => 'LinkEmbedder::Link::Spotify', - 'ted.com' => 'LinkEmbedder::Link::oEmbed', - 'travis-ci.org' => 'LinkEmbedder::Link::Travis', - 'twitter.com' => 'LinkEmbedder::Link::Twitter', - 'vimeo.com' => 'LinkEmbedder::Link::oEmbed', - 'xkcd.com' => 'LinkEmbedder::Link::Xkcd', - 'whereby.com' => 'LinkEmbedder::Link::AppearIn', - 'youtube.com' => 'LinkEmbedder::Link::oEmbed', + 'default' => 'LinkEmbedder::Link::Basic', + 'dropbox.com' => 'LinkEmbedder::Link::Dropbox', + 'facebook.com' => 'LinkEmbedder::Link::Facebook', + 'git.io' => 'LinkEmbedder::Link::Github', + 'github.com' => 'LinkEmbedder::Link::Github', + 'google' => 'LinkEmbedder::Link::Google', + 'goo.gl' => 'LinkEmbedder::Link::Google', + 'imgur.com' => 'LinkEmbedder::Link::Imgur', + 'ix.io' => 'LinkEmbedder::Link::Ix', + 'instagram.com' => 'LinkEmbedder::Link::oEmbed', + 'metacpan.org' => 'LinkEmbedder::Link::Metacpan', + 'nhl.com' => 'LinkEmbedder::Link::NHL', + 'paste.opensuse.org' => 'LinkEmbedder::Link::OpenSUSE', + 'paste.scsys.co.uk' => 'LinkEmbedder::Link::Shadowcat', + 'pastebin.com' => 'LinkEmbedder::Link::Pastebin', + 'spotify' => 'LinkEmbedder::Link::Spotify', + 'ted.com' => 'LinkEmbedder::Link::oEmbed', + 'travis-ci.org' => 'LinkEmbedder::Link::Travis', + 'twitter.com' => 'LinkEmbedder::Link::Twitter', + 'vimeo.com' => 'LinkEmbedder::Link::oEmbed', + 'xkcd.com' => 'LinkEmbedder::Link::Xkcd', + 'whereby.com' => 'LinkEmbedder::Link::AppearIn', + 'youtube.com' => 'LinkEmbedder::Link::oEmbed', }; }; diff --git a/lib/LinkEmbedder/Link/Facebook.pm b/lib/LinkEmbedder/Link/Facebook.pm new file mode 100644 index 0000000..974878c --- /dev/null +++ b/lib/LinkEmbedder/Link/Facebook.pm @@ -0,0 +1,28 @@ +package LinkEmbedder::Link::Facebook; +use Mojo::Base 'LinkEmbedder::Link'; + +use Mojo::Util; + +has provider_name => 'Facebook'; +has provider_url => sub { Mojo::URL->new('https://facebook.com') }; + +sub learn_p { + my $self = shift; + my $path = $self->url->path; + return $self->_learn_from_video_p if $path->[0] and $path->[0] eq 'watch'; + return $self->_learn_from_video_p if $path->[1] and $path->[1] eq 'videos'; + return $self->SUPER::learn_p(@_); +} + +sub _learn_from_video_p { + my $self = shift; + $self->template([__PACKAGE__, 'iframe.html.ep']); + $self->type('rich'); + return Mojo::Promise->resolve($self); +} + +1; + +__DATA__ +@@ iframe.html.ep + diff --git a/t/facebook.t b/t/facebook.t new file mode 100644 index 0000000..5f752c5 --- /dev/null +++ b/t/facebook.t @@ -0,0 +1,29 @@ +use Mojo::Base -strict; +use Test::More; +use LinkEmbedder; + +plan skip_all => 'TEST_ONLINE=1' unless $ENV{TEST_ONLINE}; +plan skip_all => 'cpanm IO::Socket::SSL' unless LinkEmbedder::TLS; + +my @urls = ( + 'https://www.facebook.com/watch/?v=2170684279662399', + 'https://www.facebook.com/HachikoDistrict/videos/2170684279662399/', +); + +for my $url (@urls) { + my $encoded_url = Mojo::Util::url_escape($url); + + LinkEmbedder->new->test_ok( + $url => { + provider_name => 'Facebook', + provider_url => 'https://facebook.com', + type => 'rich', + version => '1.0', + cache_age => 0, + html => + qr{