Skip to content

Commit 8f6a21b

Browse files
committed
wip
1 parent c37774c commit 8f6a21b

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

app/Crawler.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Embed\Http\CurlClient;
6+
use Embed\Http\FactoryDiscovery;
7+
use Psr\Http\Client\ClientInterface;
8+
use Psr\Http\Message\RequestFactoryInterface;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\UriFactoryInterface;
12+
use Psr\Http\Message\UriInterface;
13+
14+
class Crawler extends \Embed\Http\Crawler implements ClientInterface, RequestFactoryInterface, UriFactoryInterface
15+
{
16+
protected RequestFactoryInterface $requestFactory;
17+
protected UriFactoryInterface $uriFactory;
18+
protected ClientInterface $client;
19+
protected array $defaultHeaders = [
20+
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:73.0) Gecko/20100101 Firefox/73.0',
21+
'Cache-Control' => 'max-age=0',
22+
];
23+
24+
public function __construct(?ClientInterface $client = null, ?RequestFactoryInterface $requestFactory = null, ?UriFactoryInterface $uriFactory = null)
25+
{
26+
$this->client = $client ?: new CurlClient();
27+
$this->requestFactory = $requestFactory ?: FactoryDiscovery::getRequestFactory();
28+
$this->uriFactory = $uriFactory ?: FactoryDiscovery::getUriFactory();
29+
}
30+
31+
public function addDefaultHeaders(array $headers): void
32+
{
33+
$this->defaultHeaders = $headers + $this->defaultHeaders;
34+
}
35+
36+
/**
37+
* @param UriInterface|string $uri The URI associated with the request.
38+
*/
39+
public function createRequest(string $method, $uri): RequestInterface
40+
{
41+
$request = $this->requestFactory->createRequest($method, $uri);
42+
43+
foreach ($this->defaultHeaders as $name => $value) {
44+
$request = $request->withHeader($name, $value);
45+
}
46+
47+
return $request;
48+
}
49+
50+
public function createUri(string $uri = ''): UriInterface
51+
{
52+
return $this->uriFactory->createUri($uri);
53+
}
54+
55+
public function sendRequest(RequestInterface $request): ResponseInterface
56+
{
57+
return $this->client->sendRequest($request);
58+
}
59+
60+
public function sendRequests(RequestInterface ...$requests): array
61+
{
62+
if ($this->client instanceof CurlClient) {
63+
$response = $this->client->sendRequests(...$requests);
64+
65+
logger(print_r($response, true));
66+
67+
return $response;
68+
}
69+
70+
return array_map(
71+
fn ($request) => $this->client->sendRequest($request),
72+
$requests
73+
);
74+
}
75+
76+
public function getResponseUri(ResponseInterface $response): ?UriInterface
77+
{
78+
$location = $response->getHeaderLine('Content-Location');
79+
80+
return $location ? $this->uriFactory->createUri($location) : null;
81+
}
82+
}

app/Markdown/MarkdownServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace App\Markdown;
44

5+
use App\Crawler;
56
use App\OscaroteroEmbedAdapter;
67
use Embed\Embed;
7-
use Embed\Http\Crawler;
88
use Embed\Http\CurlClient;
99
use Illuminate\Support\ServiceProvider;
1010
use League\CommonMark\Environment\Environment;
@@ -22,7 +22,7 @@ public function register(): void
2222
$this->app->singleton(Converter::class, function ($app, array $params = []) {
2323
$client = new CurlClient;
2424
$client->setSettings([
25-
'follow_location' => false,
25+
// 'follow_location' => false,
2626
// 'ignored_errors' => true,
2727
]);
2828

app/OscaroteroEmbedAdapter.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function updateEmbeds(array $embeds): void
3131
{
3232
$extractors = $this->embedLib->getMulti(...\array_map(static fn (Embed $embed) => $embed->getUrl(), $embeds));
3333
foreach ($extractors as $i => $extractor) {
34-
logger(print_r($extractor, true));
3534
if ($extractor->code !== null) {
3635
$embeds[$i]->setEmbedCode($extractor->code->html);
3736
}

0 commit comments

Comments
 (0)