Skip to content
This repository has been archived by the owner on Jul 15, 2018. It is now read-only.

add SoundCloud #9

Merged
merged 1 commit into from
Nov 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ a supported URL and extract the video ID.
* [Dailymotion](http://www.dailymotion.com/) * [Dailymotion](http://www.dailymotion.com/)
* [Vimeo](http://www.vimeo.com/) * [Vimeo](http://www.vimeo.com/)
* [Spotify](http://www.spotify.com/) * [Spotify](http://www.spotify.com/)
* [SoundCloud](http://soundcloud.com/)
* ... more to come * ... more to come


Installation Installation
Expand Down
3 changes: 3 additions & 0 deletions demo.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
<h1>Spotify</h1> <h1>Spotify</h1>
<?php echo $t->parse('https://embed.spotify.com/?uri=spotify:track:4bz7uB4edifWKJXSDxwHcs')->render() ?> <?php echo $t->parse('https://embed.spotify.com/?uri=spotify:track:4bz7uB4edifWKJXSDxwHcs')->render() ?>


<h1>SoundCloud</h1>
<?php echo $t->parse('http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F17373708')->render() ?>

</body> </body>
</html> </html>
4 changes: 3 additions & 1 deletion src/TubeLink/Service/Dailymotion.php
100755 → 100644
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class Dailymotion implements ServiceInterface
public function parse($url) public function parse($url)
{ {
$data = parse_url($url); $data = parse_url($url);

if (empty($data['host'])) {
return false;
}
if (false !== strpos($data['host'], 'dailymotion.com') if (false !== strpos($data['host'], 'dailymotion.com')
&& preg_match('#^(/embed)?/video/([0-9a-z]+)(_[-_\w]+)?$#', $data['path'], $matches) && preg_match('#^(/embed)?/video/([0-9a-z]+)(_[-_\w]+)?$#', $data['path'], $matches)
) { ) {
Expand Down
59 changes: 59 additions & 0 deletions src/TubeLink/Service/SoundCloud.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\Tube;

class SoundCloud implements ServiceInterface
{
/**
* {@inheritDoc}
*/
public function parse($url)
{
$data = parse_url($url);
if (empty($data['host'])) {
return false;
}
if (false !== strpos($data['host'], 'soundcloud.com') && isset($data['query'])) {
$queryFields = array();
parse_str($data['query'], $queryFields);
if (isset($queryFields['url']) && preg_match('#http://api.soundcloud.com/([^&]*)#', $queryFields['url'], $matches)) {
$id = $matches[1];
} else {
return false;
}
} else {
return false;
}

$tube = new Tube($this);
$tube->id = $id;

return $tube;
}

/**
* {@inheritDoc}
*/
public function generateEmbedUrl(Tube $video)
{
return 'http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2F'.urlencode($video->id);
}

/**
* {@inheritDoc}
*/
public function getName()
{
return 'soundcloud';
}
}
4 changes: 3 additions & 1 deletion src/TubeLink/Service/Spotify.php
100755 → 100644
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class Spotify implements ServiceInterface
public function parse($url) public function parse($url)
{ {
$data = parse_url($url); $data = parse_url($url);

if (empty($data['host'])) {
return false;
}
if (false !== strpos($data['host'], 'embed.spotify.com') if (false !== strpos($data['host'], 'embed.spotify.com')
&& isset($data['query']) && isset($data['query'])
&& preg_match('#^uri=spotify:([0-9a-zA-Z:]+)$#', $data['query'], $matches)) { && preg_match('#^uri=spotify:([0-9a-zA-Z:]+)$#', $data['query'], $matches)) {
Expand Down
4 changes: 3 additions & 1 deletion src/TubeLink/Service/Vimeo.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class Vimeo implements ServiceInterface
public function parse($url) public function parse($url)
{ {
$data = parse_url($url); $data = parse_url($url);

if (empty($data['host'])) {
return false;
}
if (false !== strpos($data['host'], 'vimeo.com') if (false !== strpos($data['host'], 'vimeo.com')
&& preg_match('#^/(video/)?([0-9]+)?$#', $data['path'], $matches) && preg_match('#^/(video/)?([0-9]+)?$#', $data['path'], $matches)
) { ) {
Expand Down
13 changes: 9 additions & 4 deletions src/TubeLink/Service/Youtube.php
100755 → 100644
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ class Youtube implements ServiceInterface
public function parse($url) public function parse($url)
{ {
$data = parse_url($url); $data = parse_url($url);

if (empty($data['host'])) {
return false;
}

$query = array(); $query = array();
if (isset($data['query'])) { if (isset($data['query'])) {
parse_str($data['query'], $query); parse_str($data['query'], $query);
} }


if (!isset($data['host'])) {
return false;
}

if (false !== strpos($data['host'], 'youtube.') if (false !== strpos($data['host'], 'youtube.')
&& in_array($data['path'], array('/watch', '/all_comments')) && in_array($data['path'], array('/watch', '/all_comments'))
&& isset($query['v']) && isset($query['v'])
Expand All @@ -47,6 +48,10 @@ public function parse($url)
&& preg_match('{^/v/([\w-]{11})}', $data['path'], $matches) && preg_match('{^/v/([\w-]{11})}', $data['path'], $matches)
) { ) {
$id = $matches[1]; $id = $matches[1];
} elseif (false != preg_match('/^www\.youtube(-nocookie)?\.com$/',$data['host'])
&& preg_match('{^/p/([\w-]{16})}', $data['path'], $matches)
) {
$id = $matches[1];
} else { } else {
return false; return false;
} }
Expand Down
43 changes: 43 additions & 0 deletions src/TubeLink/Tests/Service/SoundCloudTest.php
Original file line number Original file line Diff line number Diff line change
@@ -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\SoundCloud;

class SoundCloudTest extends ServiceTestCase
{
public function dataForTestParse()
{
return array(
array('http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F1482008&show_artwork=true', 'playlists/1482008'),
array('https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F17373708', 'tracks/17373708'),
);
}

public function dataForTestParseFalse()
{
return array(
array('http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com'),
);
}

public function dataForTestGenerateEmbedUrl()
{
return array(
array('tracks/17373708', 'http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F17373708'),
);
}

protected function getService()
{
return new SoundCloud();
}
}
1 change: 1 addition & 0 deletions src/TubeLink/Tests/Service/YoutubeTest.php
100755 → 100644
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function dataForTestParse()
array('http://www.youtube-nocookie.com/embed/P-8llsSbVDc?rel=0', 'P-8llsSbVDc'), array('http://www.youtube-nocookie.com/embed/P-8llsSbVDc?rel=0', 'P-8llsSbVDc'),
array('http://www.youtube.com/v/uPgLxYUHRcg?fs=1&hl=fr_FR', 'uPgLxYUHRcg'), array('http://www.youtube.com/v/uPgLxYUHRcg?fs=1&hl=fr_FR', 'uPgLxYUHRcg'),
array('http://www.youtube-nocookie.com/v/Kw_yO2utN0c?fs=1&hl=fr_FR', 'Kw_yO2utN0c'), array('http://www.youtube-nocookie.com/v/Kw_yO2utN0c?fs=1&hl=fr_FR', 'Kw_yO2utN0c'),
array('http://www.youtube.com/p/18DCC2CD9CCCCB91?hl=fr_FR&fs=1', '18DCC2CD9CCCCB91'),
); );
} }


Expand Down
Empty file modified src/TubeLink/Tube.php
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions src/TubeLink/TubeLink.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static public function create()
$t->registerService(new Service\Dailymotion()); $t->registerService(new Service\Dailymotion());
$t->registerService(new Service\Vimeo()); $t->registerService(new Service\Vimeo());
$t->registerService(new Service\Spotify()); $t->registerService(new Service\Spotify());
$t->registerService(new Service\SoundCloud());


return $t; return $t;
} }
Expand Down