-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from j0k3r/extractor-twitch
Add Twitch extractor
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Api43\FeedBundle\Extractor; | ||
|
||
use GuzzleHttp\Exception\RequestException; | ||
|
||
class Twitch extends AbstractExtractor | ||
{ | ||
protected $twitchCliendId; | ||
protected $twitchId = null; | ||
|
||
/** | ||
* @param string $twitchCliendId | ||
*/ | ||
public function __construct($twitchCliendId) | ||
{ | ||
$this->twitchCliendId = $twitchCliendId; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function match($url) | ||
{ | ||
$host = parse_url($url, PHP_URL_HOST); | ||
$path = parse_url($url, PHP_URL_PATH); | ||
|
||
if (false === $host || false === $path) { | ||
return false; | ||
} | ||
|
||
if (!in_array($host, array('www.twitch.tv', 'twitch.tv'))) { | ||
return false; | ||
} | ||
|
||
// match twitch id | ||
preg_match('/v\/([0-9]+)/i', $path, $matches); | ||
|
||
if (!isset($matches[1])) { | ||
return false; | ||
} | ||
|
||
$this->twitchId = $matches[1]; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getContent() | ||
{ | ||
if (!$this->twitchId) { | ||
return ''; | ||
} | ||
|
||
try { | ||
$data = $this->client | ||
->get('https://api.twitch.tv/kraken/videos/v'.$this->twitchId, ['headers' => ['Client-ID' => $this->twitchCliendId]]) | ||
->json(); | ||
} catch (RequestException $e) { | ||
$this->logger->warning('Twitch extract failed for: '.$this->twitchId, [ | ||
'exception' => $e, | ||
]); | ||
|
||
return ''; | ||
} | ||
|
||
if (!is_array($data) || empty($data)) { | ||
return ''; | ||
} | ||
|
||
return '<div><h2>'.$data['title'].'</h2><p>'.$data['description'].'</p><p><img src="'.$data['preview'].'"></p><iframe src="https://player.twitch.tv/?video=v'.$this->twitchId.'" frameborder="0" scrolling="no" height="378" width="620"></iframe></div>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Tests\FeedBundle\Extractor; | ||
|
||
use Api43\FeedBundle\Extractor\Twitch; | ||
use Monolog\Logger; | ||
use Monolog\Handler\TestHandler; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Subscriber\Mock; | ||
use GuzzleHttp\Message\Response; | ||
use GuzzleHttp\Stream\Stream; | ||
|
||
class TwitchTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function dataMatch() | ||
{ | ||
return array( | ||
array('https://www.twitch.tv/tomfawkes/v/91819468', true), | ||
array('https://twitch.tv/tomfawkes/v/91819468', true), | ||
array('http://www.twitch.tv/tomfawkes/v/91819468', true), | ||
array('https://www.twitch.tv/directory/game/Clustertruck', false), | ||
array('http://google.com', false), | ||
array('http://user@:80', false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataMatch | ||
*/ | ||
public function testMatch($url, $expected) | ||
{ | ||
$twitch = new Twitch('apikey'); | ||
$this->assertEquals($expected, $twitch->match($url)); | ||
} | ||
|
||
public function testContent() | ||
{ | ||
$client = new Client(); | ||
|
||
$mock = new Mock([ | ||
new Response(200, [], Stream::factory(json_encode(['title' => 'hihi', 'description' => 'hoho', 'preview' => 'http://0.0.0.0/image.jpg']))), | ||
new Response(200, [], Stream::factory(json_encode([]))), | ||
new Response(400, [], Stream::factory('oops')), | ||
]); | ||
|
||
$client->getEmitter()->attach($mock); | ||
|
||
$twitch = new Twitch('apikey'); | ||
$twitch->setClient($client); | ||
|
||
$logHandler = new TestHandler(); | ||
$logger = new Logger('test', array($logHandler)); | ||
$twitch->setLogger($logger); | ||
|
||
// first test fail because we didn't match an url, so TwitchId isn't defined | ||
$this->assertEmpty($twitch->getContent()); | ||
|
||
$twitch->match('https://www.twitch.tv/tomfawkes/v/91819468'); | ||
|
||
// consecutive calls | ||
$this->assertEquals('<div><h2>hihi</h2><p>hoho</p><p><img src="http://0.0.0.0/image.jpg"></p><iframe src="https://player.twitch.tv/?video=v91819468" frameborder="0" scrolling="no" height="378" width="620"></iframe></div>', $twitch->getContent()); | ||
// this one will got an empty array | ||
$this->assertEmpty($twitch->getContent()); | ||
// this one will catch an exception | ||
$this->assertEmpty($twitch->getContent()); | ||
|
||
$this->assertTrue($logHandler->hasWarning('Twitch extract failed for: 91819468'), 'Warning message matched'); | ||
} | ||
} |