PHP Classes

Adding media websites to Media Embed class - Media Embed package blog

Recommend this page to a friend!
  All package blogs All package blogs   Media Embed Media Embed   Blog Media Embed package blog   RSS 1.0 feed RSS 2.0 feed   Blog Adding media websites...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 3

Package: Media Embed

Got any interesting media website that you think needs be added to Media Embed class?




Loaded Article

Got any interesting media website that you think might be added to media embed class?

I'll show you how to add new media websites and I'll use break.com as example.

First we'll look at video URL pattern, for example:
http://www.break.com/index/ awesome-running-backflip-into-pool-chair-2084368

So we'll add new pattern to private $all_types array with meaningful key "break", like this:

"break" => array(

	"link" => '/http://[w.]*break.com/index/([^?&#"']*)/is',

)

Then we examine embed code:

<object width="464" height="376" id="2084368" type="application/x-shockwave-flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" alt="Awesome Running Backflip Into Pool Chair Funny Videos"><param name="movie" value="http://embed.break.com/MjA4NDM2OA=="></param><param name="allowScriptAccess" value="always"></param><embed src="http://embed.break.com/MjA4NDM2OA==" type="application/x-shockwave-flash" allowScriptAccess=always width="464" height="376"></embed></object><br><font size=1><a href="http://www.break.com/index/awesome-running-backflip-into-pool-chair-2084368" target="_blank">Awesome Running Backflip Into Pool Chair</a> - Watch more <a href="http://www.break.com/" target="_blank">Funny Videos</a></font>

As you see link to embedded media is not related to video URL: http://embed.break.com/MjA4NjA0OQ==

So there's no point in adding embed pattern to $all_types array, because it would not be possible to restore video URL.

Then we need to create methods just for break.com website. We'll need to get embed url, thumbnails, size of video and it's title. For that we'll try to examine source code of URL video webpage. First thing to look for is oEmbed protocol. Unfortunately break.com doesn't support that one, so to get needed information we'll query same video URL and parse its HTML code to get what we need.

So we'll create break.com function section where all functions will have break prefix (same value as we used in $all_types array key value for regex)

/**************************
* BREAK FUNCTIONS
**************************/

So break_site function returns website address, this is easy

//return website URL

private function break_site(){

	return "http://www.break.com";

}

Then we also need to return Video URL for compatibility with class interface:

//return canonical URL

private function break_url(){

	return "http://www.break.com/index/".($this->code);

}

Most important thing is to return embedded code and after examining webpage's source code, we found that there is an alternative embed URL:

<meta name="embed_video_url" content="http://embed.break.com/2084368">
	http://www.break.com/index/ awesome-running-backflip-into-pool-chair-2084368

Digit code in the end of embed URL is the same code that is in the end of Videos URL, so there is a way it can be generated without http request to website.
 So here are the methods to return embed URL:

//return iframe url

private function break_iframe(){

	$parts = explode("-", $this->code);

        //get the last part of url

	return "http://embed.break.com/".$parts[sizeof($parts)-1];

}

//return embed url

private function break_embed(){

	return $this->break_iframe();

}

Next is a video title. It can be retrieved from websites source code, or in this case, it can also be restored from video's URL.

	http://www.break.com/index/ awesome-running-backflip-into-pool-chair-2084368
//return title

private function break_title(){

	$parts = explode("-", $this->code);

	array_pop($parts);

	return ucwords(implode(" ", $parts));

}

All methods above didn't require HTTP request to get additional information. All other method will require it.
To get needed information we will provide URL that needs to be queried - video's webpage:

We've already created method which returns video URL, so we'll use it:

//return http request URL where to get data

private function break_req(){

	return $this->break_url();

}

Next thing to get thumbnail URL. In WebPages source code it can be found in multiple places, we'll use the first one:

<meta property="og:image" content = "http://media1.break.com/dnet/media/2011/7/16/1c98cdc3-8518-4db3-86d2-65d32702e60d.jpg">

It might get tricky to get different thumbnail sizes. You'll need to check URL of video's thumbnail images on different parts of website, to try to figure out how to get different sizes by changing image URL. Sometimes it might not even be possible; in that case we will use same size to all image types for compatability. Break.com is one of these cases:

 

//return thumbnails

private function break_thumb($res){

	$arr = array();

	preg_match( '/property="og:image"s*content="([^"]*)"/i', $res, $match);

	if(!empty($match))

	{

	  $arr["large"] = $match[1];

	  $arr["medium"] = $match[1];

	  $arr["small"] = $match[1];

	}

	return $arr;

}

Note that now this method requires http request, thus it receives one paramater which is content of HTTP queried page.

Next thing to get is video dimensions (width and height)
After examining HTML code we found

<meta name="video_height" content="392"><meta name="video_width" content="464">

And created method is one that also receives HTTP request contents:

//return size

private function break_size($res){

	$arr = array();

	preg_match( '/name="video_width"s*content="([^"]*)"/i', $res, $match);

	if(!empty($match))

	{

	  $arr["w"] = $match[1];

	}

	preg_match( '/name="video_height"s*content="([^"]*)"/i', $res, $match);

	if(!empty($match))

	{

	  $arr["h"] = $match[1];

	}

	return $arr;

}

Last thing to do is to specify which methods require HTTP request and which not:

//which data needs additional http request

private function break_data(){

	return array(

	  "thumb" => true,

	  "size" => true,

	  "embed" => false,

	  "iframe" => false,

	  "url" => false,

	  "site" => false,

	  "title" => false

	);

}

That's it, now this class supports all break.com methods required.

There are some shorthand functions in SOME STANDARD section which can be used with oEembed or some og:properties, just to make code cleaner and shorter. Feel free to add something there as well, if you see that same function applies to couple of different websites.
 




You need to be a registered user or login to post a comment

1,611,120 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.




  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   Media Embed Media Embed   Blog Media Embed package blog   RSS 1.0 feed RSS 2.0 feed   Blog Adding media websites...