Skip to content

Commit

Permalink
Merge pull request #101 from j0k3r/extractor-twitch
Browse files Browse the repository at this point in the history
Add Twitch extractor
  • Loading branch information
j0k3r authored Oct 8, 2016
2 parents 30f3405 + 63f0926 commit 0c9b36b
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ For each external API that improvers / extractors / parsers use, you will need a
* Tumblr: https://www.tumblr.com/oauth/apps
* Imgur: https://api.imgur.com/oauth2/addclient
* Readability: https://www.readability.com/settings/account#api
* Twitch: https://www.twitch.tv/kraken/oauth2/clients/new

### Install

Expand Down
3 changes: 3 additions & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ parameters:
# https://www.tumblr.com/oauth/apps
tumblr.api_key: xxxx

# https://www.twitch.tv/kraken/oauth2/clients/new
twitch.client_id: xxxx

parser.regexps:
# class or id
unlikelyCandidates: /noprint|hidden|avatar|ligatus|col_right|news-colonne-droite|combx|comment|community|disqus|extra|foot|header|menu|remark|rss|shoutbox|sidebar|sponsor|ad-break|agegate|pagination|pager|popup|addthis|response|slate_associated_bn|reseaux|sharing|auteur|tag|feedback|meta|kudo|sidebar|copyright|bio|moreInfo|legal|share|contributor/i
Expand Down
75 changes: 75 additions & 0 deletions src/FeedBundle/Extractor/Twitch.php
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>';
}
}
10 changes: 10 additions & 0 deletions src/FeedBundle/Resources/config/extractors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,13 @@ services:
- [ setClient, [ "@guzzle.client" ]]
tags:
- { name: feed.extractor, alias: giphy }

feed.extractor.twitch:
class: Api43\FeedBundle\Extractor\Twitch
arguments:
- "%twitch.client_id%"
calls:
- [ setLogger, [ "@logger" ]]
- [ setClient, [ "@guzzle.client" ]]
tags:
- { name: feed.extractor, alias: twitch }
69 changes: 69 additions & 0 deletions tests/FeedBundle/Extractor/TwitchTest.php
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');
}
}

0 comments on commit 0c9b36b

Please sign in to comment.