Skip to content

Fix for #62: YouTube 'TooManyRequests' #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ $config = [
];
```

### The request

Embed uses \Embed\Request to wrap necessary http request data. It also checks if request was correct.

The request configuration is defined under the "request" key, and it has one options:

* allowedCodes: array of http codes, that are allowed (i.e response with such code is considered to be valid). By default it allows only 200.


```php
$config = [
'request' => [
'allowedCodes' => [ 200 ]
]
];
```

### The request resolver

Embed uses the `Embed\RequestResolvers\Curl` class to resolve all requests using the curl library. You can set options to the curl request or use your custom resolver creating a class implementing the `Embed\RequestResolvers\RequestResolverInterface`.
Expand Down
23 changes: 13 additions & 10 deletions src/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Embed
*/
public static function create($request, array $config = array())
{
$request = self::getRequest($request, isset($config['resolver']) ? $config['resolver'] : null);
$request = self::getRequest($request, isset($config['request']) ? $config['request'] : [], isset($config['resolver']) ? $config['resolver'] : null);

if (!$request->isValid()) {
throw new Exceptions\InvalidUrlException("The url '{$request->getUrl()}' returns the http code '{$request->getHttpCode()}'");
Expand Down Expand Up @@ -51,14 +51,15 @@ public static function create($request, array $config = array())
/**
* Gets the info from a source (list of urls)
*
* @param string|Request $request The url or a request with the source url
* @param null|array $config Options passed to the adapter
* @param string|Request $request The url or a request with the source url
* @param null|array $resolverConfig Options passed to the adapter
* @param null|array $config Options passed to the request
*
* @return false|Sources\SourceInterface
*/
public static function createSource($request, array $config = null)
public static function createSource($request, array $resolverConfig = null, array $config = [])
{
$request = self::getRequest($request, $config);
$request = self::getRequest($request, $resolverConfig, $config);

if (!$request->isValid()) {
return false;
Expand Down Expand Up @@ -103,20 +104,22 @@ private static function executeAdapter($adapter, Request $request, array $config
/**
* Init a request
*
* @param string|Request $request The url or a request with the url
* @param null|array $config Options passed to the adapter
* @param string|Request $request The url or a request with the url
* @param null|array $config Options passed to the request
* @param null|array $resolverConfig Options passed to the adapter
*
* @throws \InvalidArgumentException If the class in not Embed\Request instance
*
* @return Request
*/
private static function getRequest($request, array $config = null)
private static function getRequest($request, array $config = [], array $resolverConfig = null)
{
if (is_string($request)) {
return new Request(
$request,
isset($config['class']) ? $config['class'] : null,
isset($config['config']) ? $config['config'] : []
isset($resolverConfig['class']) ? $resolverConfig['class'] : null,
isset($resolverConfig['config']) ? $resolverConfig['config'] : [],
$config
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Providers/OEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public function run()
$endPoint = null;
$params = $this->config['parameters'];

if (!($html = $this->request->getHtmlContent()) || !($endPoint = self::getEndPointFromDom($html))) {
if (($info = self::getEndPointFromRequest($this->request, $this->config))) {
$endPoint = $info['endPoint'];
$params += $info['params'];
}
if (($info = self::getEndPointFromRequest($this->request, $this->config))) {
$endPoint = $info['endPoint'];
$params += $info['params'];
} else if ($html = $this->request->getHtmlContent()) {
$endPoint = self::getEndPointFromDom($html);
}

if (!$endPoint) {
Expand Down
21 changes: 0 additions & 21 deletions src/Providers/OEmbed/Soundcloud.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Providers/OEmbed/Youtube.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


namespace Embed\Providers\OEmbed;


class Youtube extends OEmbedImplementation
{
/**
* {@inheritdoc}
*/
public static function getEndPoint()
{
return 'http://www.youtube.com/oembed';
}

/**
* {@inheritdoc}
*/
public static function getPatterns()
{
return [
'http://*youtube.com/watch*',
'http://*.youtube.com/v/*',
'https://*youtube.com/watch*',
'https://*.youtube.com/v/*',
'http://youtu.be/*',
'http://*.youtube.com/user/*',
'http://*.youtube.com/*#*/*',
'http://m.youtube.com/watch*',
'http://m.youtube.com/index*',
'http://*.youtube.com/profile*',
'http://*.youtube.com/view_play_list*',
'http://*.youtube.com/playlist*'
];
}

}
15 changes: 10 additions & 5 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Request extends Url
{
private $startingUrl;

private $config;
private $resolver;
private $resolverClass = 'Embed\\RequestResolvers\\Curl';
private $resolverConfig = [];
Expand All @@ -23,9 +24,13 @@ class Request extends Url
* @param string $url The request url
* @param null|string $resolverClass The resolver classname
* @param array $resolverConfig The resolver configuration
* @param array $config The request configuration
*/
public function __construct($url, $resolverClass = null, array $resolverConfig = array())
public function __construct($url, $resolverClass = null, array $resolverConfig = array(), array $config = [])
{
$defaultConfig = [ 'allowedCodes' => [ 200 ] ];
$this->config = array_merge($defaultConfig, $config);

if ($resolverClass !== null) {
if (!class_exists($resolverClass)) {
throw new \InvalidArgumentException("This class does not exists");
Expand All @@ -41,7 +46,7 @@ public function __construct($url, $resolverClass = null, array $resolverConfig =
}

$this->resolverConfig = array_replace($this->resolverConfig, $resolverConfig);
$this->startingUrl = new Url($url);
$this->startingUrl = new Url($url);;
$this->parseUrl($url);
}

Expand Down Expand Up @@ -245,10 +250,10 @@ public function getXMLContent()
*/
public function isValid()
{
if ($this->getHttpCode() !== 200) {
return false;
if (in_array($this->getHttpCode(), $this->config['allowedCodes'])) {
return true;
}

return true;
return false;
}
}