* Added "generic providers" embed.ly and oohembed.com. 
* Removed the depending definitions in providers.xml
* Added a "generic provider" configuration with embed.ly API key.
This commit is contained in:
Grischa Brockhaus 2011-12-18 20:27:21 +01:00
parent c4e8bc0e3e
commit a46206acfc
8 changed files with 109 additions and 19 deletions

View file

@ -14,6 +14,14 @@
@define('PLUGIN_EVENT_OEMBED_MAXHEIGHT', 'Max height of replacements');
@define('PLUGIN_EVENT_OEMBED_MAXHEIGHT_DESC','This is the max height the service should produce when providing a replacement. Not all services supports this but most.');
@define('PLUGIN_EVENT_OEMBED_GENERIC_SERVICE', 'Generic oEmbed provider');
@define('PLUGIN_EVENT_OEMBED_GENERIC_SERVICE_DESC','If the plugin is not able to resolve an URL because it is unknown yet, you may let it fall back to a "generic provider". These services implements oEmbed for a huge amount of services not having oEmbed. You have two choices: oohembed.com, a former free service bought by embedly and with a very limited API rate now. embed.ly is a very well maintained service for many oEmbed services (see http://embed.ly/providers), but it needs an API key to be used.');
@define('PLUGIN_EVENT_OEMBED_SERVICE_NONE', 'No generic provider');
@define('PLUGIN_EVENT_OEMBED_SERVICE_OOHEMBED', 'oohembed (free but limited)');
@define('PLUGIN_EVENT_OEMBED_SERVICE_EMBEDLY', 'embed.ly (apikey needed)');
@define('PLUGIN_EVENT_OEMBED_EMBEDLY_APIKEY', 'embed.ly API key');
@define('PLUGIN_EVENT_OEMBED_EMBEDLY_APIKEY_DESC','embed.ly needs an API key to be used. The free account allows 10k calls per month atm, what should be enough even for heavy used blogs, as the results are cached localy and fetched only once per URL. You can register for your free account at http://app.embed.ly/pricing/free');
@define('PLUGIN_EVENT_OEMBED_INFO', '<h3>oEmbed Plugin</h3>' .
'<p>'.
'This plugin expands URLs to pages of known services to a representation of that URL. It shows i.e. the video for a youtube URL or the image instead of a flickr URL.<br/>' .
@ -21,6 +29,8 @@
'If the link is not supported by the plugin at the moment, it will replace the URL by a link pointing to that URL.<br/>'.
'</p><p>'.
'Please put this plugin at the top of your plugins list, so no other plugin can change this syntax (by adding a href i.e.)'.
'</p><p>'.
'The plugin supports representations of the following link types:%s'.
'</p>');
@define('PLUGIN_EVENT_OEMBED_SUPPORTED', '<p>'.
'The plugin supports representations of the following link types without the need of the generic fallback:%s'.
'</p>');

View file

@ -3,7 +3,7 @@
<span class="serendipity_oembed_rich">
{if $oembed.provider_name=="Wikipedia"}
<blockquote>{$oembed.html}</blockquote>
{elseif $oembed.provider_name=="IMDB"} {* beautify IMDB content *}
{elseif $oembed.provider_name=="IMDB"} {* beautify noembed.com IMDB content *}
<blockquote>{$oembed.html|replace:"<h2>":"<strong>"|replace:"</h2>":"</strong>"|replace:"<img":"<img align='right'"}</blockquote>
{elseif $oembed.provider-name=="Soundcloud"} {* beautify SoundCloud *}
{$oembed.html|replace:"</object>":"</object><br/>"}
@ -28,7 +28,7 @@
{if $oembed.provider_name=="Wikipedia"}<blockquote>{/if}
{if $oembed.description}
{if $oembed.title}<strong>{$oembed.title}</strong><br/>{/if}
<p>{if $oembed.thumbnail_url}<img src="{$oembed.thumbnail_url}" align="left">{/if}{$oembed.description}{if $oembed.url}<br/>[<a href="{$oembed.url}" target="_blank">link</a>]{/if}</p>
<p>{if $oembed.thumbnail_url}<img src="{$oembed.thumbnail_url}" align="left" style="padding-right: 1em"{if $oembed.title} alt="{$oembed.title}" title="{$oembed.title}"{/if}>{/if}{$oembed.description}{if $oembed.url} [<a href="{$oembed.url}" target="_blank">link</a>]{/if}</p>
{else}
<a href="{$oembedurl}" title="{$oembed.title}">{$oembed.author_name}</a>
{/if}

View file

@ -0,0 +1,11 @@
<?php
class EmbedlyProvider extends OEmbedProvider {
public function match($url){
// Embedly should match always
return 1;
}
public function __construct($url, $apikey, $maxwidth=null, $maxheight=null){
$endpoint = "http://api.embed.ly/1/oembed?key=$apikey";
parent::__construct($url,$endpoint,false, $maxwidth,$maxheight, true);
}
}

View file

@ -21,8 +21,14 @@ class OEmbedProvider extends EmbedProvider{
$this->xmlEndpoint=preg_replace("/\{format\}/","xml",$endpoint);
$this->xmlEndpoint.="?url={url}";
} else {
$this->jsonEndpoint=$endpoint."?url={url}&format=json";
$this->xmlEndpoint=$endpoint."?url={url}&format=xml";
if (strpos($endpoint, '?') === FALSE) {
$this->jsonEndpoint=$endpoint."?url={url}&format=json";
$this->xmlEndpoint=$endpoint."?url={url}&format=xml";
}
else {
$this->jsonEndpoint=$endpoint."&url={url}&format=json";
$this->xmlEndpoint=$endpoint."&url={url}&format=xml";
}
}
if ($this->dimensionsSupported) {
if (!empty($this->maxwidth)) {

View file

@ -0,0 +1,11 @@
<?php
class OohEmbedProvider extends OEmbedProvider {
public function match($url){
// Embedly should match always
return 1;
}
public function __construct($url, $maxwidth=null, $maxheight=null){
$endpoint = "http://oohembed.com/oohembed/";
parent::__construct($url,$endpoint,true, $maxwidth,$maxheight, true);
}
}

View file

@ -22,8 +22,5 @@ require_once dirname(__FILE__) . '/' . 'VideoEmbed.class.php';
require_once dirname(__FILE__) . '/' . 'EmbedProvider.class.php';
require_once dirname(__FILE__) . '/' . 'OEmbedProvider.class.php';
// Will be loaded via generic oembedprovider!
//require_once dirname(__FILE__) . '/' . 'YouTubeProvider.class.php';
require_once dirname(__FILE__) . '/' . 'ProviderManager.class.php';

View file

@ -132,15 +132,7 @@
<endpoint>http://skitch.com/oembed/</endpoint>
</provider>
<!-- noembed providers -->
<!--
<provider>
<name>Wikipedia (via noembed.com)</name>
<jsononly/>
<url>https?://*.wikipedia.org/wiki/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
-->
<!-- noembed providers =========================================== -->
<provider>
<name>Twitpic (via noembed.com)</name>
@ -154,12 +146,14 @@
<url>http://imgur.com/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
<!--
<provider>
<name>Imdb.com (via noembed.com)</name>
<jsononly/>
<url>http://(www.)?imdb.com/title/*</url>
<endpoint>http://noembed.com/embed</endpoint>
</provider>
-->
<provider>
<name>CloudApp (via noembed.com)</name>
<jsononly/>
@ -194,6 +188,7 @@
-->
<!-- oohembed.com providers -->
<!--
<provider>
<name>Audioboo (via oohembed.com)</name>
<jsononly/>
@ -206,5 +201,12 @@
<url>https?://*.wikipedia.org/wiki/*</url>
<endpoint>http://oohembed.com/oohembed/</endpoint>
</provider>
<provider>
<name>Imdb.com (via oohembed.com)</name>
<jsononly/>
<url>http://(www.)?imdb.com/title/*</url>
<endpoint>http://oohembed.com/oohembed/</endpoint>
</provider>
-->
</providers>

View file

@ -40,12 +40,18 @@ class serendipity_event_oembed extends serendipity_event
$propbag->add('event_hooks', array(
'frontend_display' => true,
));
$configuration = $configuration = array('info','maxwidth','maxheight');
$configuration = $configuration = array('info','maxwidth','maxheight','generic_service','embedly_apikey');
$configuration[] = 'supported'; // always last
$propbag->add('configuration', $configuration);
}
function introspect_config_item($name, &$propbag)
{
$generic_services = array (
'none' => PLUGIN_EVENT_OEMBED_SERVICE_NONE,
'oohembed' => PLUGIN_EVENT_OEMBED_SERVICE_OOHEMBED,
'embedly' => PLUGIN_EVENT_OEMBED_SERVICE_EMBEDLY,
);
switch($name) {
case 'info':
$propbag->add('type', 'content');
@ -63,6 +69,23 @@ class serendipity_event_oembed extends serendipity_event
$propbag->add('description', PLUGIN_EVENT_OEMBED_MAXHEIGHT_DESC);
$propbag->add('default', '');
break;
case 'generic_service':
$propbag->add('type', 'select');
$propbag->add('name', PLUGIN_EVENT_OEMBED_GENERIC_SERVICE);
$propbag->add('description', PLUGIN_EVENT_OEMBED_GENERIC_SERVICE_DESC);
$propbag->add('select_values', $generic_services);
$propbag->add('default', 'oohembed');
break;
case 'embedly_apikey':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_EVENT_OEMBED_EMBEDLY_APIKEY);
$propbag->add('description', PLUGIN_EVENT_OEMBED_EMBEDLY_APIKEY_DESC);
$propbag->add('default', '');
break;
case 'supported':
$propbag->add('type', 'content');
$propbag->add('default', sprintf(PLUGIN_EVENT_OEMBED_SUPPORTED, ProviderList::ul_providernames(true)));
break;
}
return true;
}
@ -126,6 +149,9 @@ class serendipity_event_oembed extends serendipity_event
$manager = ProviderManager::getInstance($maxwidth,$maxheight);
try {
$obj=$manager->provide($url,"object");
if (!isset($obj)) {
$obj = $this->expand_by_general_provider($url,$maxwidth,$maxheight);
}
if (isset($obj)) {
$obj = OEmbedDatabase::save_oembed($url,$obj);
}
@ -138,6 +164,33 @@ class serendipity_event_oembed extends serendipity_event
return $obj;
}
function expand_by_general_provider($url, $maxwidth=null, $maxheight=null) {
$provider = $this->get_config('generic_service', 'none');
$manager = null;
if ('oohembed' == $provider) {
require_once dirname(__FILE__) . '/oembed/OohEmbedProvider.class.php';
$manager = new OohEmbedProvider($url, $maxwidth, $maxheight);
}
elseif ('embedly' == $provider) {
$apikey = $this->get_config('embedly_apikey', '');
if (!empty($apikey)) {
require_once dirname(__FILE__) . '/oembed/EmbedlyProvider.class.php';
$manager = new EmbedlyProvider($url, $apikey, $maxwidth, $maxheight);
}
}
if (isset($manager)) {
try {
return $manager->provide($url,'object');
} catch (Exception $e) {
return null;
}
}
else {
return null;
}
}
function cleanup_html( $str ) {
// Clear unicode stuff
$str=str_ireplace("\u003C","<",$str);