Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
takaaki-mizuno committed Aug 26, 2015
0 parents commit ff5ce76
Show file tree
Hide file tree
Showing 15 changed files with 487 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
#*#
*~
*.swp
.#
/vendor
composer.phar
composer.lock
.idea/*

tests/ReFUEL4/FacebookObject/config.json
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Takaaki Mizuno

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.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TakaakiMizuno/VideoServiceUrlAnalyzer

Analyze Video URL of each video service ( YouTube, Vimeo ... ) and extract information.

## How To Use



## License

This library is available under the MIT license. See the LICENSE file for more info.
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "takaaki-mizuno/video-service-url-analyser",
"description": "Video URL analyzer for famous video sharing services",
"keywords": ["video", "youtube", "vimeo"],
"type": "library",
"license": "MIT",
"homepage": "https://github.com/takaaki-mizuno/video-service-url-analyser",
"authors": [
{
"name": "Takaaki Mizuno",
"email": "takaaki.mizuno@gmail.com"
}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": ">=3.7.0"
},
"autoload": {
"psr-4": {
"TakaakiMizuno\\VideoServiceUrlAnalyzer\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"TakaakiMizuno\\VideoServiceUrlAnalyzer\\Tests": "tests"
}
}

}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
35 changes: 35 additions & 0 deletions src/Analyzers/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace TakaakiMizuno\VideoServiceUrlAnalyzer\Analyzers;

abstract class Base
{

/** @var array */
static protected $domains = [];

/**
* Analyze video url and returns Video entity which includes all analysis result.
*
* @param string $url Video Page URL
* @return \TakaakiMizuno\VideoServiceUrlAnalyzer\Entities\Base|null
*/
abstract public function analyze($url);

/**
* Check if the URL is the video page url of target service
*
* @param string $url Video Page URL
* @return bool
*/
public function check($url)
{
$parsedUrlElements = parse_url($url);
if (in_array(strtolower($parsedUrlElements['host']), static::$domains)) {
return true;
}

return false;
}


}
31 changes: 31 additions & 0 deletions src/Analyzers/Vimeo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace TakaakiMizuno\VideoServiceUrlAnalyzer\Analyzers;

use TakaakiMizuno\VideoServiceUrlAnalyzer\Entities\Vimeo as VimeoEntity;

class Vimeo extends Base
{

static protected $domains = [
'vimeo.com',
'player.vimeo.com',
];

public function analyze($url)
{
if (!$this->check($url)) {
return null;
}
$parsedUrlElements = parse_url($url);
$elements = explode('/', substr(strtolower($parsedUrlElements['path']), 1));

$id = $elements[count($elements)-1];

if (empty($id)) {
return null;
}
$video = new VimeoEntity($id);
return $video;
}

}
63 changes: 63 additions & 0 deletions src/Analyzers/YouTube.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace TakaakiMizuno\VideoServiceUrlAnalyzer\Analyzers;

use TakaakiMizuno\VideoServiceUrlAnalyzer\Entities\YouTube as YouTubeEntity;

class YouTube extends Base
{

static protected $domains = [
'www.youtube.com',
'youtu.be',
];

public function analyze($url)
{
if (!$this->check($url)) {
return null;
}
$parsedUrlElements = parse_url($url);
$id = null;
if (!isset($parsedUrlElements['host'])) {
return null;
}
switch (strtolower($parsedUrlElements['host'])) {
case 'www.youtube.com':
if (isset($parsedUrlElements['path'])) {
$path = explode('/', substr($parsedUrlElements['path'], 1));
if (strtolower($path[0]) == 'embed' && count($path) == 2) {
$id = $path[1];
break;
}
if (isset($parsedUrlElements['query'])) {
parse_str($parsedUrlElements['query'], $queryParams);
if (!array_key_exists('v', $queryParams)) {
return null;
}
$id = $queryParams['v'];
}
}
break;
case 'youtu.be':
$id = explode('/', substr($parsedUrlElements['path'], 1))[0];
break;
}
if (empty($id)) {
return null;
}
$video = new YouTubeEntity($id);

return $video;
}

public function check($url)
{
$parsedUrlElements = parse_url($url);
if (in_array(strtolower($parsedUrlElements['host']), static::$domains)) {
return true;
}

return false;
}

}
47 changes: 47 additions & 0 deletions src/Entities/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace TakaakiMizuno\VideoServiceUrlAnalyzer\Entities;

abstract class Base
{

/** @var string */
protected $id;

/** @var array */
protected $info;

public function __construct($id)
{
$this->id = $id;
$this->info = null;
}

/**
* @return string
*/
public function getId()
{
return $this->id;
}

/**
* @return string
*/
abstract public function getUrl();

/**
* @param int $width
* @param int $height
* @return string
*/
abstract public function getEmbeddedHtml($width, $height);

abstract public function getServiceName();

abstract public function getThumbnailUrl();

abstract public function getTitle();

abstract protected function getInfo($key);

}
54 changes: 54 additions & 0 deletions src/Entities/Vimeo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace TakaakiMizuno\VideoServiceUrlAnalyzer\Entities;

class Vimeo extends Base
{

/** @var array */
protected $info;

function getEmbeddedHtml($width = 500, $height = 281)
{
return '<iframe src="https://player.vimeo.com/video/' . $this->getId() .
'" width="' . intval($width) . '" height="'
. intval($height) .
'" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
}

public function getUrl()
{
return 'https://vimeo.com/' . $this->getId();
}

public function getServiceName()
{
return 'Vimeo';
}

public function getTitle()
{
return $this->getInfo('title');
}

public function getThumbnailUrl()
{
return $this->getInfo('thumbnailUrl');
}

protected function getInfo($key)
{
if (empty($this->info)) {
$content = file_get_contents('https://vimeo.com/api/oembed.json?url=' . htmlspecialchars($this->getUrl()));

$info = json_decode($content, true);
$this->info = [
'title' => isset($info['title']) ? $info['title'] : '',
'thumbnailUrl' => isset($info['thumbnail_url']) ? $info['thumbnail_url'] : '',
'authorName' => isset($info['author_name']) ? $info['author_name'] : '',
];
}

return $this->info['title'];
}

}
49 changes: 49 additions & 0 deletions src/Entities/YouTube.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace TakaakiMizuno\VideoServiceUrlAnalyzer\Entities;

class YouTube extends Base
{

function getEmbeddedHtml($width = 560, $height = 315)
{
return '<iframe width="' . intval($width) . '" height="'
. intval($height) . '" src="https://www.youtube.com/embed/'
. $this->id . '" frameborder="0" allowfullscreen></iframe>';
}

public function getUrl()
{
return 'https://youtu.be/' . $this->getId();
}

public function getServiceName()
{
return 'YouTube';
}

public function getTitle()
{
return $this->getInfo('title');
}

public function getThumbnailUrl()
{
return $this->getInfo('thumbnailUrl');
}

protected function getInfo($key)
{
if (empty($this->info)) {
$content = file_get_contents('http://youtube.com/get_video_info?video_id=' . $this->getId());
parse_str($content, $info);
$this->info = [
'title' => isset($info['title']) ? $info['title'] : '',
'thumbnailUrl' => isset($info['thumbnail_url']) ? $info['thumbnail_url'] : '',
'authorName' => isset($info['author']) ? $info['author'] : '',
];
}

return $this->info['title'];
}

}

0 comments on commit ff5ce76

Please sign in to comment.