diff --git a/README.md b/README.md index 5e7ea194..f24cf995 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index 1b928024..af66e13c 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -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 diff --git a/src/FeedBundle/Extractor/Twitch.php b/src/FeedBundle/Extractor/Twitch.php new file mode 100644 index 00000000..823732e4 --- /dev/null +++ b/src/FeedBundle/Extractor/Twitch.php @@ -0,0 +1,75 @@ +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 '

'.$data['title'].'

'.$data['description'].'

'; + } +} diff --git a/src/FeedBundle/Resources/config/extractors.yml b/src/FeedBundle/Resources/config/extractors.yml index 51f19f88..69722cda 100644 --- a/src/FeedBundle/Resources/config/extractors.yml +++ b/src/FeedBundle/Resources/config/extractors.yml @@ -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 } diff --git a/tests/FeedBundle/Extractor/TwitchTest.php b/tests/FeedBundle/Extractor/TwitchTest.php new file mode 100644 index 00000000..d38e6685 --- /dev/null +++ b/tests/FeedBundle/Extractor/TwitchTest.php @@ -0,0 +1,69 @@ +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('

hihi

hoho

', $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'); + } +}