forked from leftypol/leftypol
OembedExtractor.php: add extractor
This commit is contained in:
parent
0e9c9de5c6
commit
231fcb9ca9
1 changed files with 70 additions and 0 deletions
70
inc/Services/Embed/OembedExtractor.php
Normal file
70
inc/Services/Embed/OembedExtractor.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
namespace Vichan\Services\Embed;
|
||||||
|
|
||||||
|
use Vichan\Data\Driver\{CacheDriver, HttpDriver};
|
||||||
|
use Vichan\Data\OembedResponse;
|
||||||
|
|
||||||
|
|
||||||
|
class OembedExtractor {
|
||||||
|
private const DEFAULT_CACHE_TIMEOUT = 3600; // 1 hour.
|
||||||
|
|
||||||
|
private CacheDriver $cache;
|
||||||
|
private HttpDriver $http;
|
||||||
|
private int $provider_timeout;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(CacheDriver $cache, HttpDriver $http, int $provider_timeout) {
|
||||||
|
$this->cache = $cache;
|
||||||
|
$this->http = $http;
|
||||||
|
$this->provider_timeout = $provider_timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the oembed data from the given provider with the given url.
|
||||||
|
*
|
||||||
|
* @param string $identifier Opaque identifier for caching, must be unique for each $url-$provider combination.
|
||||||
|
* @return OembedResponse The serialized remove response. May be cached.
|
||||||
|
*/
|
||||||
|
public function fetch(string $provider_url, string $url): OembedResponse {
|
||||||
|
$ret = $this->cache->get("oembed_embedder_$provider_url$url");
|
||||||
|
if ($ret === null) {
|
||||||
|
$body = $this->http->requestGet(
|
||||||
|
$provider_url,
|
||||||
|
[
|
||||||
|
'url' => $url,
|
||||||
|
'format' => 'json'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'Content-Type: application/json'
|
||||||
|
],
|
||||||
|
$this->provider_timeout
|
||||||
|
);
|
||||||
|
$json = \json_decode($body, null, 512, \JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
if (!isset($json['title'])) {
|
||||||
|
throw new \RuntimeException("Missing type in response from $provider_url");
|
||||||
|
}
|
||||||
|
$ret = [
|
||||||
|
'type' => $json['type'],
|
||||||
|
'title' => $json['title'],
|
||||||
|
'cache_age' => $json['cache_age'],
|
||||||
|
'thumbnail_url' => $json['thumbnail_url'],
|
||||||
|
'thumbnail_width' => $json['thumbnail_width'],
|
||||||
|
'thumbnail_height' => $json['thumbnail_heigh']
|
||||||
|
];
|
||||||
|
|
||||||
|
$cache_timeout = $ret['cache_age'] ?? self::DEFAULT_CACHE_TIMEOUT;
|
||||||
|
|
||||||
|
$this->cache->set("oembed_embedder_$url$provider_url", $ret, $cache_timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
$resp = new OembedResponse();
|
||||||
|
$resp->type = $ret['title'];
|
||||||
|
$resp->title = $ret['title'] ?? null;
|
||||||
|
$resp->cache_age = $ret['cache_age'] ?? null;
|
||||||
|
$resp->thumbnail_url = $ret['thumbnail_url'] ?? null;
|
||||||
|
$resp->thumbnail_width = $ret['thumbnail_width'] ?? null;
|
||||||
|
$resp->thumbnail_height = $ret['thumbnail_height'] ?? null;
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue