Skip to content

Commit

Permalink
Facebook and Instagram adapters #392
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 27, 2020
1 parent 13f237e commit 48e3918
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ If you need to pass settings to your detectors, you can use the `setSettings` me
$info = $embed->get($url);

$info->setSettings([
'oembed:query_parameters' => [] //Extra parameters send to oembed
'twitch:parent' => 'example.com' //Required to embed twitch videos as iframe
'oembed:query_parameters' => [], //Extra parameters send to oembed
'twitch:parent' => 'example.com', //Required to embed twitch videos as iframe
'facebook:token' => '1234|5678', //Required to embed content from Facebook
'instagram:token' => '1234|5678', //Required to embed content from Instagram
]);
```

Expand Down
1 change: 1 addition & 0 deletions src/Adapters/Facebook/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(UriInterface $uri, RequestInterface $request, Respon
{
parent::__construct($uri, $request, $response, $crawler);

$this->oembed = new OEmbed($this);
$this->title = new Detectors\Title($this);
}
}
71 changes: 71 additions & 0 deletions src/Adapters/Facebook/OEmbed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
declare(strict_types = 1);

namespace Embed\Adapters\Facebook;

use Embed\OEmbed as Base;
use Psr\Http\Message\UriInterface;

class OEmbed extends Base
{
const ENDPOINT_PAGE = "https://graph.facebook.com/v8.0/oembed_page";
const ENDPOINT_POST = "https://graph.facebook.com/v8.0/oembed_post";
const ENDPOINT_VIDEO = "https://graph.facebook.com/v8.0/oembed_url";

protected function detectEndpoint(): ?UriInterface
{
$token = $this->extractor->getSetting('facebook:token');

if (!$token) {
return null;
}

$uri = $this->extractor->getUri();
$queryParameters = $this->getOembedQueryParameters((string) $uri);
$queryParameters['access_token'] = $token;

return $this->extractor->getCrawler()
->createUri($this->getEndpoint($uri->getPath()))
->withQuery($queryParameters);
}

private function getEndpoint(string $path): string
{
/* Videos
https://www.facebook.com/{page-name}/videos/{video-id}/
https://www.facebook.com/{username}/videos/{video-id}/
https://www.facebook.com/video.php?id={video-id}
https://www.facebook.com/video.php?v={video-id}
*/
if (strpos($path, '/video.php') === 0
|| strpos($path, '/videos/') !== false
) {
return self::ENDPOINT_VIDEO;
}

/* Posts
https://www.facebook.com/{page-name}/posts/{post-id}
https://www.facebook.com/{username}/posts/{post-id}
https://www.facebook.com/{username}/activity/{activity-id}
https://www.facebook.com/photo.php?fbid={photo-id}
https://www.facebook.com/photos/{photo-id}
https://www.facebook.com/permalink.php?story_fbid={post-id}
https://www.facebook.com/media/set?set={set-id}
https://www.facebook.com/questions/{question-id}
https://www.facebook.com/notes/{username}/{note-url}/{note-id}
*/
if (strpos($path, '/photo.php') === 0
|| strpos($path, '/photos/') === 0
|| strpos($path, '/permalink.php') === 0
|| strpos($path, '/media/') === 0
|| strpos($path, '/questions/') === 0
|| strpos($path, '/notes/') === 0
|| strpos($path, '/posts/') !== false
|| strpos($path, '/activity/') !== false
) {
return self::ENDPOINT_POST;
}

return self::ENDPOINT_PAGE;
}
}
20 changes: 20 additions & 0 deletions src/Adapters/Instagram/Extractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types = 1);

namespace Embed\Adapters\Instagram;

use Embed\Extractor as Base;
use Embed\Http\Crawler;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;

class Extractor extends Base
{
public function __construct(UriInterface $uri, RequestInterface $request, ResponseInterface $response, Crawler $crawler)
{
parent::__construct($uri, $request, $response, $crawler);

$this->oembed = new OEmbed($this);
}
}
29 changes: 29 additions & 0 deletions src/Adapters/Instagram/OEmbed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types = 1);

namespace Embed\Adapters\Instagram;

use Embed\OEmbed as Base;
use Psr\Http\Message\UriInterface;

class OEmbed extends Base
{
const ENDPOINT = "https://graph.facebook.com/v8.0/instagram_oembed";

protected function detectEndpoint(): ?UriInterface
{
$token = $this->extractor->getSetting('instagram:token');

if (!$token) {
return null;
}

$uri = $this->extractor->getUri();
$queryParameters = $this->getOembedQueryParameters((string) $uri);
$queryParameters['access_token'] = $token;

return $this->extractor->getCrawler()
->createUri(self::ENDPOINT)
->withQuery($queryParameters);
}
}
2 changes: 1 addition & 1 deletion src/OEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function fetchData(): array
return $this->extractJSON((string) $response->getBody());
}

private function detectEndpoint(): ?UriInterface
protected function detectEndpoint(): ?UriInterface
{
$document = $this->extractor->getDocument();

Expand Down

0 comments on commit 48e3918

Please sign in to comment.