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['description'].'
hoho