Skip to content

Commit 19a8fd3

Browse files
authored
Merge pull request #468 from dftd/feature-1
Twitter detector
2 parents 6884a53 + 0fabf2a commit 19a8fd3

File tree

10 files changed

+205
-0
lines changed

10 files changed

+205
-0
lines changed

src/Adapters/Twitter/Api.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter;
5+
6+
use function Embed\getDirectory;
7+
use Embed\HttpApiTrait;
8+
9+
class Api
10+
{
11+
use HttpApiTrait;
12+
13+
protected function fetchData(): array
14+
{
15+
$token = $this->extractor->getSetting('twitter:token');
16+
17+
if (!$token) {
18+
return null;
19+
}
20+
21+
$uri = $this->extractor->getUri();
22+
23+
$id = getDirectory($uri->getPath(), 2);
24+
25+
if (empty($id)) {
26+
return [];
27+
}
28+
29+
$this->extractor->getCrawler()->addDefaultHeaders(array('Authorization' => "Bearer $token"));
30+
$this->endpoint = $this->extractor->getCrawler()->createUri("https://api.twitter.com/2/tweets/{$id}?expansions=author_id,attachments.media_keys&tweet.fields=created_at&media.fields=preview_image_url,url&user.fields=id,name");
31+
32+
$data = $this->fetchJSON($this->endpoint);
33+
34+
return $data ?? [];
35+
}
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter\Detectors;
5+
6+
use Embed\Detectors\AuthorName as Detector;
7+
8+
class AuthorName extends Detector
9+
{
10+
public function detect(): ?string
11+
{
12+
$api = $this->extractor->getApi();
13+
14+
return $api->str('includes', 'users', '0', 'name')
15+
?: parent::detect();
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter\Detectors;
5+
6+
use Embed\Detectors\AuthorUrl as Detector;
7+
use Psr\Http\Message\UriInterface;
8+
9+
class AuthorUrl extends Detector
10+
{
11+
public function detect(): ?UriInterface
12+
{
13+
$api = $this->extractor->getApi();
14+
$username = $api->str('includes', 'users', '0', 'username');
15+
16+
if ($username) {
17+
return $this->extractor->getCrawler()->createUri("https://twitter.com/{$username}");
18+
}
19+
20+
return parent::detect();
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter\Detectors;
5+
6+
use Embed\Detectors\Description as Detector;
7+
8+
class Description extends Detector
9+
{
10+
public function detect(): ?string
11+
{
12+
$api = $this->extractor->getApi();
13+
14+
return $api->str('data', 'text')
15+
?: parent::detect();
16+
}
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter\Detectors;
5+
6+
use Embed\Detectors\Image as Detector;
7+
use Psr\Http\Message\UriInterface;
8+
9+
class Image extends Detector
10+
{
11+
public function detect(): ?UriInterface
12+
{
13+
$api = $this->extractor->getApi();
14+
$preview = $api->url('includes', 'media', '0', 'preview_image_url');
15+
16+
if ($preview) {
17+
return $preview;
18+
}
19+
20+
$regular = $api->url('includes', 'media', '0', 'url');
21+
22+
if ($regular) {
23+
return $regular;
24+
}
25+
26+
return parent::detect();
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter\Detectors;
5+
6+
use Embed\Detectors\ProviderName as Detector;
7+
8+
class ProviderName extends Detector
9+
{
10+
public function detect(): string
11+
{
12+
return 'Twitter';
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter\Detectors;
5+
6+
use Datetime;
7+
use Embed\Detectors\PublishedTime as Detector;
8+
9+
class PublishedTime extends Detector
10+
{
11+
public function detect(): ?Datetime
12+
{
13+
$api = $this->extractor->getApi();
14+
15+
return $api->time('data', 'created_at')
16+
?: parent::detect();
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter\Detectors;
5+
6+
use Embed\Detectors\Title as Detector;
7+
8+
class Title extends Detector
9+
{
10+
public function detect(): ?string
11+
{
12+
$api = $this->extractor->getApi();
13+
$name = $api->str('includes', 'users', '0', 'name');
14+
15+
if ($name) {
16+
return "Tweet by $name";
17+
}
18+
19+
return parent::detect();
20+
}
21+
}

src/Adapters/Twitter/Extractor.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Embed\Adapters\Twitter;
5+
6+
use Embed\Extractor as Base;
7+
8+
class Extractor extends Base
9+
{
10+
private Api $api;
11+
12+
public function getApi(): Api
13+
{
14+
return $this->api;
15+
}
16+
17+
public function createCustomDetectors(): array
18+
{
19+
$this->api = new Api($this);
20+
21+
return [
22+
'authorName' => new Detectors\AuthorName($this),
23+
'authorUrl' => new Detectors\AuthorUrl($this),
24+
'description' => new Detectors\Description($this),
25+
'image' => new Detectors\Image($this),
26+
'providerName' => new Detectors\ProviderName($this),
27+
'publishedTime' => new Detectors\PublishedTime($this),
28+
'title' => new Detectors\Title($this),
29+
];
30+
}
31+
}

src/ExtractorFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ExtractorFactory
2929
'youtube.com' => Adapters\Youtube\Extractor::class,
3030
'twitch.tv' => Adapters\Twitch\Extractor::class,
3131
'bandcamp.com' => Adapters\Bandcamp\Extractor::class,
32+
'twitter.com' => Adapters\Twitter\Extractor::class,
3233
];
3334
private array $customDetectors = [];
3435
private array $settings;

0 commit comments

Comments
 (0)