OEmbed:
* Added directory for custom OEmbed providers, that implemend OEmbed conversion providers for services that don't support oembed * Implemented a post.ly custom provider, fetching info from the Posterous API and converting it into photo or video OEmbeds as an example for a hadwritten custom provider.
This commit is contained in:
parent
f25165fa5c
commit
0860f97c66
|
@ -3,15 +3,6 @@ if (!defined("PLUGIN_OEMBED_PROVIDER_XML_FILE")) {
|
|||
@define("PLUGIN_OEMBED_PROVIDER_XML_FILE", dirname(__FILE__) . '/' . "providers.xml");
|
||||
}
|
||||
|
||||
// Include all class files
|
||||
/*
|
||||
$oembed_config_class_wildcard = dirname(__FILE__) . "/*class.php";
|
||||
foreach (glob($oembed_config_class_wildcard) as $filename)
|
||||
{
|
||||
//echo "$filename<br/>\n";
|
||||
@include $filename;
|
||||
}
|
||||
*/
|
||||
require_once dirname(__FILE__) . '/' . 'Exception404.class.php';
|
||||
require_once dirname(__FILE__) . '/' . 'OEmbed.class.php';
|
||||
require_once dirname(__FILE__) . '/' . 'LinkEmbed.class.php';
|
||||
|
@ -24,3 +15,9 @@ require_once dirname(__FILE__) . '/' . 'OEmbedProvider.class.php';
|
|||
|
||||
require_once dirname(__FILE__) . '/' . 'ProviderManager.class.php';
|
||||
|
||||
// Include all custom providers if any
|
||||
$oembed_config_class_wildcard = dirname(__FILE__) . "/customs/*class.php";
|
||||
foreach (glob($oembed_config_class_wildcard) as $filename)
|
||||
{
|
||||
@include $filename;
|
||||
}
|
||||
|
|
129
serendipity_event_oembed/oembed/customs/PostlyProvider.class.php
Normal file
129
serendipity_event_oembed/oembed/customs/PostlyProvider.class.php
Normal file
|
@ -0,0 +1,129 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* This is a kind of example class how to do a oembed provider for a service, that doesn't support oembed.
|
||||
* This provider reads the Posterous API to resolve post.ly links. If the result is an image or a video, the XML result
|
||||
* will be converted as an OEmbed
|
||||
*
|
||||
* Converting is done in the getEmbed function. This is the main code.
|
||||
* Everything else is only type converting that should be nearly the same in any custom provider
|
||||
*
|
||||
* All *.class.php files found in the customs directory will be included automatically
|
||||
*
|
||||
* After implementing the provider you have to add it to the providers.xml like this:
|
||||
* <provider>
|
||||
* <name>Posterous (Imge and Video Posts)</name>
|
||||
* <url>http://post.ly/*</url>
|
||||
* <class>PostlyProvider</class>
|
||||
* </provider>
|
||||
*
|
||||
* @author Grischa Brockhaus
|
||||
*
|
||||
*/
|
||||
class PostlyProvider extends EmbedProvider {
|
||||
|
||||
/**
|
||||
* This is the main function calling the Posterous postly API and converting it into a OEmbed object
|
||||
* @param string $url post.ly url
|
||||
* @return OEmbed the embed object
|
||||
*/
|
||||
public function getEmbed($url){
|
||||
if(preg_match("/post\.ly\/([\w-]+)/",$url,$matches)){
|
||||
$post_id=$matches[1];
|
||||
}
|
||||
if (empty($post_id)) return null;
|
||||
|
||||
$api_fetch = "http://posterous.com/api/getpost?id=" . $post_id;
|
||||
$xml = simplexml_load_file($api_fetch);
|
||||
$rsp_attributes = $xml->attributes();
|
||||
if ($rsp_attributes['stat']!="ok") return null;
|
||||
|
||||
$post = $xml->post;
|
||||
if (!empty($post->media)) {
|
||||
$mtype = (string)$post->media->type[0];
|
||||
//print_r($medium);
|
||||
if ($mtype=='video') {
|
||||
$medium = $post->media;
|
||||
$oembed = new VideoEmbed();
|
||||
$oembed->type='video';
|
||||
$oembed->url=(string)$medium->url;
|
||||
$oembed->html=(string)$post->body;
|
||||
$oembed->thumbnail_url=(string)$medium->thumb;
|
||||
if (!empty($medium->mp4)) $oembed->url = (string)$medium->mp4;
|
||||
}
|
||||
elseif ($mtype=='image') {
|
||||
$medium = $post->media->medium[0];
|
||||
$oembed = new PhotoEmbed();
|
||||
$oembed->type="photo";
|
||||
$oembed->url=(string)$medium->url;
|
||||
$oembed->width=(string)$medium->width;
|
||||
$oembed->height=(string)$medium->height;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
$oembed->version='1.0';
|
||||
$oembed->provider_name="Posterous";
|
||||
$oembed->provider_url="http://posterous.com";
|
||||
$oembed->resource_url=(string)$post->link;
|
||||
$oembed->title = (string)$post->title;
|
||||
//$oembed->html = $post->body;
|
||||
$oembed->author_name = (string)$post->author;
|
||||
$oembed->author_pic = (string)$post->authorpic; // normaly unsupported
|
||||
return $oembed;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// === here comes the regular stuff for providers, what is very similar in any custom provider =========
|
||||
private function provideXML($url){
|
||||
$string="";
|
||||
$oembed = $this->getEmbed($url);
|
||||
if (isset($oembed)) {
|
||||
foreach($this->getEmbed($url) as $key=>$value){
|
||||
if(isset($value)&& $value!="") $string.=" <".$key.">".$value."</".$key.">\n";
|
||||
}
|
||||
$string="<oembed>\n".$string."</oembed>";
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
private function provideObject($url){
|
||||
return $this->getEmbed($url);
|
||||
}
|
||||
private function provideJSON($url){
|
||||
$oembed = $this->getEmbed($url);
|
||||
if (isset($oembed)) return json_encode($this->getEmbed($url));
|
||||
else return null;
|
||||
}
|
||||
private function provideSerialized($url){
|
||||
$oembed = $this->getEmbed($url);
|
||||
if (isset($oembed)) return serialize($this->getEmbed($url));
|
||||
else return null;
|
||||
}
|
||||
public function provide($url,$format="json"){
|
||||
if($format=="xml"){
|
||||
return $this->provideXML($url);
|
||||
} else if ($format=="object"){
|
||||
return $this->provideObject($url);
|
||||
} else if ($format=="serialized"){
|
||||
return $this->provideSerialized($url);
|
||||
} else {
|
||||
return $this->provideJSON($url);;
|
||||
}
|
||||
}
|
||||
|
||||
public function match($url) {
|
||||
return preg_match('/post\.ly\/([\w-]+)/',$url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Enter description here ...
|
||||
* @param simplexml $config holds the entry in the providers.xml for this Provider. You can add more parameters parsed here
|
||||
*/
|
||||
public function __construct($config){
|
||||
parent::__construct("http://post.ly","");
|
||||
}
|
||||
}
|
|
@ -5,12 +5,6 @@
|
|||
<url>http://*.flickr.com/*</url>
|
||||
<endpoint>http://www.flickr.com/services/oembed/</endpoint>
|
||||
</provider>
|
||||
<!-- down
|
||||
<provider>
|
||||
<url>http://*.pownce.com/*</url>
|
||||
<endpoint>http://api.pownce.com/2.1/oembed.{format}</endpoint>
|
||||
</provider>
|
||||
-->
|
||||
<provider>
|
||||
<name>Twitter Status</name>
|
||||
<url>https?://*.twitter.com/*/status(es)?/*</url>
|
||||
|
@ -126,6 +120,13 @@
|
|||
<endpoint>http://skitch.com/oembed/</endpoint>
|
||||
</provider>
|
||||
|
||||
<!-- custom providers =========================================== -->
|
||||
<provider>
|
||||
<name>Posterous (Imge and Video Posts)</name>
|
||||
<url>http://post.ly/*</url>
|
||||
<class>PostlyProvider</class>
|
||||
</provider>
|
||||
|
||||
<!-- noembed providers =========================================== -->
|
||||
|
||||
<provider>
|
||||
|
|
Loading…
Reference in a new issue