Permalink
Browse files

Create TubeLink with Youtube only. Others to come.

  • Loading branch information...
GromNaN committed Jun 26, 2012
0 parents commit 0510290ceee5ce997124f5dc89a4a2c77b6653a5
19 LICENCE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Jérôme Tamarelle <jerome@tamarelle.net>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,70 @@
+TubeLink - Parse any video URL
+===============================
+
+**TubeLink** is a PHP library to extract video information from any URL.
+
+Supported Services
+------------------
+
+For each video-sharing website of the following list, a _Service_ class can identify
+a supported URL and extract the video ID.
+
+* [Youtube](http://www.youtube.com/)
+* ... more to come
+
+
+Installation
+============
+
+The recommended way to install TubeLink is through composer.
+
+Just create a `composer.json` file for your project:
+
+``` json
+{
+ "require": {
+ "grom/tube-link": "dev-master"
+ }
+}
+```
+
+And run these two commands to install it:
+
+``` bash
+$ wget http://getcomposer.org/composer.phar
+$ php composer.phar install
+```
+
+
+Now you can add the autoloader, and you will have access to the library:
+
+``` php
+require 'vendor/autoload.php';
+```
+
+If you don't use neither **Composer** nor a _ClassLoader_ in your application, just require the provided autoloader:
+
+``` php
+require_once 'src/autoload.php';
+```
+
+You're done.
+
+Usage
+=====
+
+
+
+``` php
+use TubeLink\TubeLink;
+
+$url = 'http://youtu.be/kffacxfA7G4';
+
+$parser = new TubeLink();
+$parser->registerService(new TubeLink\Service\Youtube());
+
+$video = $parser->parse($url);
+
+// Shows the embedded video HTML
+echo $video->render();
+```
@@ -0,0 +1,23 @@
+{
+ "name": "grom/tube-link",
+ "description": "Extract video information from any URL and render HTML",
+ "keywords": ["video", "youtube", "url", "parser"],
+ "license": "MIT",
+ "authors": [
+ {
+ "name": "Jérôme Tamarelle",
+ "email": "jerome@tamarelle.net"
+ }
+ ],
+ "require": {
+ "php": ">=5.3.0"
+ },
+ "autoload": {
+ "psr-0": { "VideoDetective": "src/" }
+ },
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.0-dev"
+ }
+ }
+}
@@ -0,0 +1,26 @@
+<?php
+/*
+ * This file is part of the TubeLink package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @licence MIT
+ */
+
+require __DIR__ . '/src/autoload.php';
+
+use TubeLink\Video;
+
+$videoYoutube = new Video(new TubeLink\Service\Youtube());
+$videoYoutube->id = 'gHYfY9lZaRE';
+
+?>
+<!DOCTYPE html>
+<html>
+<head><title>Videos</title></head>
+<body>
+ <h1>Youtube</h1>
+ <?php echo $videoYoutube->render() ?>
+</body>
+</html>
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit backupGlobals="false"
+ backupStaticAttributes="false"
+ colors="true"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ processIsolation="false"
+ stopOnFailure="false"
+ syntaxCheck="false"
+ bootstrap="src/autoload.php"
+ >
+
+ <testsuites>
+ <testsuite name="TubeLink Test Suite">
+ <directory>./src/TubeLink/Tests/</directory>
+ </testsuite>
+ </testsuites>
+
+ <filter>
+ <whitelist>
+ <directory>./src/TubeLink/</directory>
+ <exclude>
+ <directory>./src/TubeLink/Tests</directory>
+ </exclude>
+ </whitelist>
+ </filter>
+
+</phpunit>
@@ -0,0 +1,16 @@
+<?php
+/*
+ * This file is part of the TubeLink package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @licence MIT
+ */
+
+namespace TubeLink\Exception;
+
+class ServiceNotFoundException extends \RuntimeException
+{
+
+}
@@ -0,0 +1,41 @@
+<?php
+/*
+ * This file is part of the TubeLink package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @licence MIT
+ */
+
+namespace TubeLink\Service;
+
+use TubeLink\Video;
+
+interface ServiceInterface
+{
+ /**
+ * Try to extract video data from an URL.
+ *
+ * @param string $url URL to a video provider
+ *
+ * @return Video|bool The video or FALSE if not supported
+ */
+ public function parse($url);
+
+ /**
+ * Get the HTML to integrate a video from a given URL.
+ *
+ * @param Video $video Video object
+ *
+ * @return string HTML code
+ */
+ public function html(Video $video);
+
+ /**
+ * Get the provider name
+ *
+ * @return string Lowercase name
+ */
+ public function getName();
+}
@@ -0,0 +1,67 @@
+<?php
+/*
+ * This file is part of the TubeLink package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @licence MIT
+ */
+
+namespace TubeLink\Service;
+
+use TubeLink\Video;
+
+class Youtube implements ServiceInterface
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function parse($url)
+ {
+ $data = parse_url($url);
+ $query = array();
+ if (isset($data['query'])) {
+ parse_str($data['query'], $query);
+ }
+
+ if (false !== strpos($data['host'], 'youtube.')
+ && in_array($data['path'], array('/watch', '/all_comments'))
+ && isset($query['v'])
+ && preg_match('#^\w{11}$#', $query['v'])
+ ) {
+ $id = $query['v'];
+ } else if (false !== strpos($data['host'], 'youtu.be')
+ && preg_match('#^/?\w{11}/?$#', $data['path'])
+ ) {
+ $id = trim($data['path'], '/');
+ } else {
+ return false;
+ }
+
+ $video = new Video($this);
+ $video->id = $id;
+
+ return $video;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function html(Video $video)
+ {
+ $html = <<<HTML
+<iframe width="560" height="315" src="http://www.youtube.com/embed/{id}" frameborder="0" allowfullscreen></iframe>
+HTML;
+
+ return str_replace('{id}', $video->id, $html);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getName()
+ {
+ return 'youtube';
+ }
+}
@@ -0,0 +1,41 @@
+<?php
+/*
+ * This file is part of the TubeLink package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @licence MIT
+ */
+
+namespace TubeLink\Tests\Service;
+
+abstract class ServiceTestCase extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @dataProvider dataForTestParse
+ */
+ public function testParse($url, $id)
+ {
+ $video = $this->getService()->parse($url);
+
+ $this->assertInstanceOf('\TubeLink\Video', $video);
+ $this->assertEquals($id, $video->id);
+ }
+
+ /**
+ * @dataProvider dataForTestParseFalse
+ */
+ public function testParseFalse($url)
+ {
+ $video = $this->getService()->parse($url);
+
+ $this->assertSame(false, $video);
+ }
+
+ abstract public function dataForTestParse();
+
+ abstract public function dataForTestParseFalse();
+
+ abstract protected function getService();
+}
@@ -0,0 +1,43 @@
+<?php
+/*
+ * This file is part of the TubeLink package.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @licence MIT
+ */
+
+namespace TubeLink\Tests\Service;
+
+use TubeLink\Service\Youtube;
+
+class YoutubeTest extends ServiceTestCase
+{
+ public function dataForTestParse()
+ {
+ return array(
+ array('http://www.youtube.com/watch?v=gHYfY9lZaRE&feature=g-vrec', 'gHYfY9lZaRE'),
+ array('http://youtube.com/watch?v=gHYfY9lZaRE&feature=g-vrec', 'gHYfY9lZaRE'),
+ array('http://www.youtube.fr/watch?v=gHYfY9lZaRE', 'gHYfY9lZaRE'),
+ array('http://www.youtube.com/watch?gl=FR&hl=fr&v=gHYfY9lZaRE#share', 'gHYfY9lZaRE'),
+ array('http://www.youtube.com/all_comments?v=gHYfY9lZaRE', 'gHYfY9lZaRE'),
+ array('http://youtu.be/gHYfY9lZaRE', 'gHYfY9lZaRE'),
+ array('http://www.youtu.be/gHYfY9lZaRE', 'gHYfY9lZaRE'),
+ array('http://www.youtu.be/gHYfY9lZaRE/?feature=g-vrec', 'gHYfY9lZaRE'),
+ );
+ }
+
+ public function dataForTestParseFalse()
+ {
+ return array(
+ array('http://www.youtube.com/watch?v=HYfY9lZaRE'), // Invalid ID
+ array('http://www.youtu.be/HYfY9lZaRE'), // Invalid ID
+ );
+ }
+
+ protected function getService()
+ {
+ return new Youtube();
+ }
+}
Oops, something went wrong.

0 comments on commit 0510290

Please sign in to comment.