Skip to content

Commit

Permalink
Add support for Facebook video, closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Sep 17, 2020
1 parent e71c8f1 commit ddfa18b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 22 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 23 additions & 22 deletions lib/LinkEmbedder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
};

Expand Down
28 changes: 28 additions & 0 deletions lib/LinkEmbedder/Link/Facebook.pm
Original file line number Diff line number Diff line change
@@ -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
<iframe class="le-rich le-provider-facebook" width="476" height="476" style="border:0;width:100%" frameborder="0" allowfullscreen src="https://www.facebook.com/plugins/video.php?<%== Mojo::Util::url_escape($l->url) %>&show_text=0&width=476"></iframe>
29 changes: 29 additions & 0 deletions t/facebook.t
Original file line number Diff line number Diff line change
@@ -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{<iframe class="le-rich le-provider-facebook" .* src="https://www\.facebook\.com/plugins/video\.php\?$encoded_url},
}
);
}

done_testing;

0 comments on commit ddfa18b

Please sign in to comment.