File tree Expand file tree Collapse file tree 10 files changed +195
-0
lines changed Expand file tree Collapse file tree 10 files changed +195
-0
lines changed Original file line number Diff line number Diff line change
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&user.fields=id,name " );
31
+
32
+ $ data = $ this ->fetchJSON ($ this ->endpoint );
33
+
34
+ return $ data ?? [];
35
+ }
36
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
15
+ return $ api ->url ('includes ' , 'media ' , '0 ' , 'preview_image_url ' )
16
+ ?: parent ::detect ();
17
+ }
18
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ class ExtractorFactory
29
29
'youtube.com ' => Adapters \Youtube \Extractor::class,
30
30
'twitch.tv ' => Adapters \Twitch \Extractor::class,
31
31
'bandcamp.com ' => Adapters \Bandcamp \Extractor::class,
32
+ 'twitter.com ' => Adapters \Twitter \Extractor::class,
32
33
];
33
34
private array $ customDetectors = [];
34
35
private array $ settings ;
You can’t perform that action at this time.
0 commit comments