[Embed] Using oscarotero/Embed as first attempt to get oEmbed/OpenGraph data, fallback

to previous implementation otherwise.
This commit is contained in:
Miguel Dantas 2019-07-07 13:26:10 +01:00 committed by Diogo Cordeiro
parent b25632ebc4
commit 462ea26303
5 changed files with 61 additions and 41 deletions

View File

@ -29,6 +29,8 @@
defined('GNUSOCIAL') || die(); defined('GNUSOCIAL') || die();
use Embed\Embed;
/** /**
* Base class for the Embed plugin that does most of the heavy lifting to get * Base class for the Embed plugin that does most of the heavy lifting to get
* and display representations for remote content. * and display representations for remote content.
@ -110,16 +112,31 @@ class EmbedPlugin extends Plugin
*/ */
public function onGetRemoteUrlMetadataFromDom($url, DOMDocument $dom, stdClass &$metadata) public function onGetRemoteUrlMetadataFromDom($url, DOMDocument $dom, stdClass &$metadata)
{ {
try { try {
common_log(LOG_INFO, 'Trying to discover an oEmbed endpoint using link headers.'); common_log(LOG_INFO, "Trying to find Embed data for {$url} with 'oscarotero/Embed'");
$api = oEmbedHelper::oEmbedEndpointFromHTML($dom); $info = Embed::create($url);
$metadata->version = '1.0'; // Yes.
$metadata->provider_name = $info->authorName;
$metadata->title = $info->title;
$metadata->html = common_purify($info->description);
$metadata->type = $info->type;
$metadata->url = $info->url;
$metadata->thumbnail_url = $info->image;
$metadata->thumbnail_height = $info->imageHeight;
$metadata->thumbnail_width = $info->imageWidth;
} catch (Exception $e) {
common_log(LOG_INFO, "Failed to find Embed data for {$url} with 'oscarotero/Embed'");
try {
common_log(LOG_INFO, "Trying to discover an oEmbed endpoint for {$url} using link headers.");
$api = EmbedHelper::oEmbedEndpointFromHTML($dom);
common_log(LOG_INFO, 'Found oEmbed API endpoint ' . $api . ' for URL ' . $url); common_log(LOG_INFO, 'Found oEmbed API endpoint ' . $api . ' for URL ' . $url);
$params = array( $params = array(
'maxwidth' => common_config('thumbnail', 'width'), 'maxwidth' => common_config('thumbnail', 'width'),
'maxheight' => common_config('thumbnail', 'height'), 'maxheight' => common_config('thumbnail', 'height'),
); );
$metadata = oEmbedHelper::getOembedFrom($api, $url, $params); $metadata = EmbedHelper::getOembedFrom($api, $url, $params);
// Facebook just gives us javascript in its oembed html, // Facebook just gives us javascript in its oembed html,
// so use the content of the title element instead // so use the content of the title element instead
if (strpos($url, 'https://www.facebook.com/') === 0) { if (strpos($url, 'https://www.facebook.com/') === 0) {
@ -142,10 +159,12 @@ class EmbedPlugin extends Plugin
} }
} catch (Exception $e) { } catch (Exception $e) {
// FIXME - make sure the error was because we couldn't get metadata, not something else! -mb // FIXME - make sure the error was because we couldn't get metadata, not something else! -mb
common_log(LOG_INFO, 'Could not find an oEmbed endpoint using link headers, trying OpenGraph from HTML.'); common_log(LOG_INFO, 'Could not find an oEmbed endpoint using link headers, ' .
'trying OpenGraph from HTML.');
// Just ignore it! // Just ignore it!
$metadata = OpenGraphHelper::ogFromHtml($dom); $metadata = OpenGraphHelper::ogFromHtml($dom);
} }
}
if (isset($metadata->thumbnail_url)) { if (isset($metadata->thumbnail_url)) {
// sometimes sites serve the path, not the full URL, for images // sometimes sites serve the path, not the full URL, for images
@ -153,7 +172,8 @@ class EmbedPlugin extends Plugin
// add protocol and host if the thumbnail_url starts with / // add protocol and host if the thumbnail_url starts with /
if (substr($metadata->thumbnail_url, 0, 1) == '/') { if (substr($metadata->thumbnail_url, 0, 1) == '/') {
$thumbnail_url_parsed = parse_url($metadata->url); $thumbnail_url_parsed = parse_url($metadata->url);
$metadata->thumbnail_url = $thumbnail_url_parsed['scheme']."://".$thumbnail_url_parsed['host'].$metadata->thumbnail_url; $metadata->thumbnail_url = $thumbnail_url_parsed['scheme']."://".
$thumbnail_url_parsed['host'].$metadata->thumbnail_url;
} }
// some wordpress opengraph implementations sometimes return a white blank image // some wordpress opengraph implementations sometimes return a white blank image

View File

@ -77,10 +77,10 @@ class File_embed extends Managed_DataObject
); );
} }
public static function _getOembed($url) public static function _getEmbed($url)
{ {
try { try {
return oEmbedHelper::getObject($url); return EmbedHelper::getObject($url);
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_INFO, "Error during oembed lookup for $url - " . $e->getMessage()); common_log(LOG_INFO, "Error during oembed lookup for $url - " . $e->getMessage());
return false; return false;

View File

@ -104,7 +104,7 @@ class EmbedHelper
$unicode_check_dom = new DOMDocument(); $unicode_check_dom = new DOMDocument();
$ok = $unicode_check_dom->loadHTML($body); $ok = $unicode_check_dom->loadHTML($body);
if (!$ok) { if (!$ok) {
throw new oEmbedHelper_BadHtmlException(); throw new EmbedHelper_BadHtmlException();
} }
$metaNodes = $unicode_check_dom->getElementsByTagName('meta'); $metaNodes = $unicode_check_dom->getElementsByTagName('meta');
foreach ($metaNodes as $metaNode) { foreach ($metaNodes as $metaNode) {
@ -135,7 +135,7 @@ class EmbedHelper
error_reporting($old); error_reporting($old);
if (!$ok) { if (!$ok) {
throw new oEmbedHelper_BadHtmlException(); throw new EmbedHelper_BadHtmlException();
} }
Event::handle('GetRemoteUrlMetadataFromDom', array($url, $dom, &$metadata)); Event::handle('GetRemoteUrlMetadataFromDom', array($url, $dom, &$metadata));
@ -185,7 +185,7 @@ class EmbedHelper
} }
} }
throw new oEmbedHelper_DiscoveryException(); throw new EmbedHelper_DiscoveryException();
} }
/** /**
@ -245,7 +245,7 @@ class EmbedHelper
} }
} }
class oEmbedHelper_Exception extends Exception class EmbedHelper_Exception extends Exception
{ {
public function __construct($message = "", $code = 0, $previous = null) public function __construct($message = "", $code = 0, $previous = null)
{ {
@ -253,7 +253,7 @@ class oEmbedHelper_Exception extends Exception
} }
} }
class oEmbedHelper_BadHtmlException extends oEmbedHelper_Exception class EmbedHelper_BadHtmlException extends EmbedHelper_Exception
{ {
public function __construct($previous=null) public function __construct($previous=null)
{ {
@ -261,7 +261,7 @@ class oEmbedHelper_BadHtmlException extends oEmbedHelper_Exception
} }
} }
class oEmbedHelper_DiscoveryException extends oEmbedHelper_Exception class EmbedHelper_DiscoveryException extends EmbedHelper_Exception
{ {
public function __construct($previous=null) public function __construct($previous=null)
{ {

View File

@ -51,7 +51,7 @@ $url = get_option_value('u', 'url');
print "Contacting URL\n"; print "Contacting URL\n";
$oEmbed = oEmbedHelper::getObject($url); $oEmbed = EmbedHelper::getObject($url);
var_dump($oEmbed); var_dump($oEmbed);
print "\nDONE.\n"; print "\nDONE.\n";

View File

@ -92,7 +92,7 @@ class oEmbedTest extends PHPUnit_Framework_TestCase
public function _doTest($url, $expectedType) public function _doTest($url, $expectedType)
{ {
try { try {
$data = oEmbedHelper::getObject($url); $data = EmbedHelper::getObject($url);
$this->assertEquals($expectedType, $data->type); $this->assertEquals($expectedType, $data->type);
if ($data->type == 'photo') { if ($data->type == 'photo') {
$this->assertTrue(!empty($data->url), 'Photo must have a URL.'); $this->assertTrue(!empty($data->url), 'Photo must have a URL.');