From 34e4ad8ba5e0a77b797f24ed9af1cee6dce82c36 Mon Sep 17 00:00:00 2001 From: Sem Date: Thu, 10 Nov 2011 23:03:58 +0100 Subject: Refactored library. One file per videotype. --- lib/bliptv.php | 29 +++++++++ lib/gisstv.php | 41 ++++++++++++ lib/metacafe.php | 29 +++++++++ lib/videolist.php | 183 +++++------------------------------------------------- lib/vimeo.php | 31 +++++++++ lib/youtube.php | 28 +++++++++ 6 files changed, 175 insertions(+), 166 deletions(-) create mode 100644 lib/bliptv.php create mode 100644 lib/gisstv.php create mode 100644 lib/metacafe.php create mode 100644 lib/vimeo.php create mode 100644 lib/youtube.php diff --git a/lib/bliptv.php b/lib/bliptv.php new file mode 100644 index 000000000..f4c0f772c --- /dev/null +++ b/lib/bliptv.php @@ -0,0 +1,29 @@ + 'bliptv', + 'video_id' => $parsed['path'], + ); +} + +function videolist_get_data_bliptv($parsed){ + $video_id = $parsed['video_id']; + + $buffer = file_get_contents('http://blip.tv'.$video_id.'?skin=rss'); + $xml = new SimpleXMLElement($buffer); + + return array( + 'title' => sanitize_string(current($xml->xpath('/rss/channel/item/title'))), + 'description' => strip_tags(current($xml->xpath('/rss/channel/item/description'))), + 'thumbnail' => sanitize_string(current($xml->xpath('/rss/channel/item/media:thumbnail/@url'))), + 'embedurl' => sanitize_string(current($xml->xpath('/rss/channel/item/blip:embedUrl'))), + ); +} diff --git a/lib/gisstv.php b/lib/gisstv.php new file mode 100644 index 000000000..24e11340a --- /dev/null +++ b/lib/gisstv.php @@ -0,0 +1,41 @@ + 'gisstv', + 'video_id' => $video_id, + ); +} + +function videolist_get_data_gisstv($parsed){ + $video_id = $parsed['video_id']; + + $buffer = file_get_contents('http://giss.tv/dmmdb//rss.php'); + $xml = new SimpleXMLElement($buffer); + + $data = array(); + foreach($xml->xpath('/rss/channel/item') as $item){ + if(sanitize_string($item->link) == 'http://giss.tv/dmmdb//contents/'.$video_id) { + $data['title'] = sanitize_string($item->title); + $data['description'] = strip_tags($item->description); + $data['thumbnail'] = sanitize_string($item->thumbnail); + break; + } + } + return $data; +} diff --git a/lib/metacafe.php b/lib/metacafe.php new file mode 100644 index 000000000..34b006a32 --- /dev/null +++ b/lib/metacafe.php @@ -0,0 +1,29 @@ + 'metacafe', + 'video_id' => $path[2], + ); +} + +function videolist_get_data_metacafe($parsed){ + $video_id = $parsed['video_id']; + + $buffer = file_get_contents("http://www.metacafe.com/api/item/$video_id"); + $xml = new SimpleXMLElement($buffer); + + return array( + 'title' => sanitize_string(current($xml->xpath('/rss/channel/item/title'))), + 'description' => strip_tags(current($xml->xpath('/rss/channel/item/description'))), + 'thumbnail' => sanitize_string(current($xml->xpath('/rss/channel/item/media:thumbnail/@url'))), + 'embedurl' => sanitize_string(current($xml->xpath('/rss/channel/item/media:content/@url'))), + ); +} diff --git a/lib/videolist.php b/lib/videolist.php index c30916773..2dfc7d8fc 100644 --- a/lib/videolist.php +++ b/lib/videolist.php @@ -1,178 +1,29 @@ 'youtube', - 'video_id' => $query['v'], - ); -} - -function videolist_parseurl_vimeo($url) { - $parsed = parse_url($url); - $path = explode('/', $parsed['path']); - - if ($parsed['host'] != 'vimeo.com' || !(int) $path[1]) { - return false; - } - - return array( - 'videotype' => 'vimeo', - 'video_id' => $path[1], - ); -} - -function videolist_parseurl_metacafe($url) { - $parsed = parse_url($url); - $path = explode('/', $parsed['path']); - - if ($parsed['host'] != 'www.metacafe.com' || $path[1] != 'watch' || !(int) $path[2]) { - return false; - } - - return array( - 'videotype' => 'metacafe', - 'video_id' => $path[2], - ); -} +define('VIDEOLIST_SUPPORTED_PLATFORMS', 'youtube, vimeo, metacafe, bliptv, gisstv'); -function videolist_parseurl_bliptv($url) { - $parsed = parse_url($url); - $path = explode('/', $parsed['path']); - - if ($parsed['host'] != 'blip.tv' || count($path) < 3) { - return false; - } - - return array( - 'videotype' => 'bliptv', - 'video_id' => $parsed['path'], - ); -} - -function videolist_parseurl_gisstv($url) { - $parsed = parse_url($url); - $path = explode('/', $parsed['path']); - - if ($parsed['host'] != 'giss.tv' || $path[1] != 'dmmdb') { - return false; - } - - if($path[2] == 'contents' && isset($path[3])) { - $video_id = $path[3]; - } elseif($path[3] == 'contents' && isset($path[4])) { - $video_id = $path[4]; - } else { - return false; - } - - return array( - 'videotype' => 'gisstv', - 'video_id' => $video_id, - ); +foreach(explode(', ', VIDEOLIST_SUPPORTED_PLATFORMS) as $videotype){ + include(elgg_get_plugins_path()."videolist/lib/$videotype.php"); } function videolist_parseurl($url){ - if ($parsed = videolist_parseurl_youtube($url)) return $parsed; - elseif ($parsed = videolist_parseurl_vimeo($url)) return $parsed; - elseif ($parsed = videolist_parseurl_metacafe($url)) return $parsed; - elseif ($parsed = videolist_parseurl_bliptv($url)) return $parsed; - elseif ($parsed = videolist_parseurl_gisstv($url)) return $parsed; - else return array(); -} - -function videolist_get_data($video_parsed_url) { - $videotype = $video_parsed_url['videotype']; - $video_id = $video_parsed_url['video_id']; - switch($videotype){ - case 'youtube': return videolist_get_data_youtube($video_id); - case 'vimeo': return videolist_get_data_vimeo($video_id); - case 'metacafe': return videolist_get_data_metacafe($video_id); - case 'bliptv': return videolist_get_data_bliptv($video_id); - case 'gisstv': return videolist_get_data_gisstv($video_id); - default: return array(); + foreach(explode(', ', VIDEOLIST_SUPPORTED_PLATFORMS) as $videotype){ + if (is_callable("videolist_parseurl_$videotype")){ + if ($parsed = call_user_func("videolist_parseurl_$videotype", $url)) { + return $parsed; + } + } } + return array(); } - -function videolist_get_data_youtube($video_id){ - $buffer = file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$video_id); - $xml = new SimpleXMLElement($buffer); - - return array( - 'title' => sanitize_string($xml->title), - 'description' => strip_tags($xml->content), - 'thumbnail' => "http://img.youtube.com/vi/$video_id/default.jpg", - 'video_id' => $video_id, - 'videotype' => 'youtube', - ); -} - -function videolist_get_data_vimeo($video_id){ - $buffer = file_get_contents("http://vimeo.com/api/v2/video/$video_id.xml"); - $xml = new SimpleXMLElement($buffer); - - $videos = $xml->children(); - $video = $videos[0]; - - return array( - 'title' => sanitize_string($video->title), - 'description' => strip_tags($video->description), - 'thumbnail' => sanitize_string($video->thumbnail_medium), - 'video_id' => $video_id, - 'videotype' => 'vimeo', - ); -} - -function videolist_get_data_metacafe($video_id){ - $buffer = file_get_contents("http://www.metacafe.com/api/item/$video_id"); - $xml = new SimpleXMLElement($buffer); - - return array( - 'title' => sanitize_string(current($xml->xpath('/rss/channel/item/title'))), - 'description' => strip_tags(current($xml->xpath('/rss/channel/item/description'))), - 'thumbnail' => sanitize_string(current($xml->xpath('/rss/channel/item/media:thumbnail/@url'))), - 'embedurl' => sanitize_string(current($xml->xpath('/rss/channel/item/media:content/@url'))), - 'video_id' => $video_id, - 'videotype' => 'metacafe', - ); -} - -function videolist_get_data_bliptv($video_id){ - $buffer = file_get_contents('http://blip.tv'.$video_id.'?skin=rss'); - $xml = new SimpleXMLElement($buffer); +function videolist_get_data($parsed) { + $videotype = $parsed['videotype']; + $video_id = $parsed['video_id']; - return array( - 'title' => sanitize_string(current($xml->xpath('/rss/channel/item/title'))), - 'description' => strip_tags(current($xml->xpath('/rss/channel/item/description'))), - 'thumbnail' => sanitize_string(current($xml->xpath('/rss/channel/item/media:thumbnail/@url'))), - 'embedurl' => sanitize_string(current($xml->xpath('/rss/channel/item/blip:embedUrl'))), - 'video_id' => $video_id, - 'videotype' => 'bliptv', - ); -} - -function videolist_get_data_gisstv($video_id){ - $buffer = file_get_contents('http://giss.tv/dmmdb//rss.php'); - $xml = new SimpleXMLElement($buffer); - - $data = array(); - foreach($xml->xpath('/rss/channel/item') as $item){ - if(sanitize_string($item->link) == 'http://giss.tv/dmmdb//contents/'.$video_id) { - $data['title'] = sanitize_string($item->title); - $data['description'] = strip_tags($item->description); - $data['thumbnail'] = sanitize_string($item->thumbnail); - break; - } + if(is_callable("videolist_get_data_$videotype")){ + return array_merge($parsed, call_user_func("videolist_get_data_$videotype", $parsed)); + } else { + return $parsed; } - return array_merge($data, array( - 'video_id' => $video_id, - 'videotype' => 'gisstv', - )); } diff --git a/lib/vimeo.php b/lib/vimeo.php new file mode 100644 index 000000000..0433c5a94 --- /dev/null +++ b/lib/vimeo.php @@ -0,0 +1,31 @@ + 'vimeo', + 'video_id' => $path[1], + ); +} + +function videolist_get_data_vimeo($parsed){ + $video_id = $parsed['video_id']; + + $buffer = file_get_contents("http://vimeo.com/api/v2/video/$video_id.xml"); + $xml = new SimpleXMLElement($buffer); + + $videos = $xml->children(); + $video = $videos[0]; + + return array( + 'title' => sanitize_string($video->title), + 'description' => strip_tags($video->description), + 'thumbnail' => sanitize_string($video->thumbnail_medium), + ); +} diff --git a/lib/youtube.php b/lib/youtube.php new file mode 100644 index 000000000..6ed9344b0 --- /dev/null +++ b/lib/youtube.php @@ -0,0 +1,28 @@ + 'youtube', + 'video_id' => $query['v'], + ); +} + +function videolist_get_data_youtube($parsed){ + $video_id = $parsed['video_id']; + + $buffer = file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$video_id); + $xml = new SimpleXMLElement($buffer); + + return array( + 'title' => sanitize_string($xml->title), + 'description' => strip_tags($xml->content), + 'thumbnail' => "http://img.youtube.com/vi/$video_id/default.jpg", + ); +} -- cgit v1.2.3