File: update-oembed.php

package info (click to toggle)
php-embed 4.4.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: php: 40,309; makefile: 23
file content (54 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
declare(strict_types = 1);

require dirname(__DIR__).'/vendor/autoload.php';

use Brick\VarExporter\VarExporter;

//Fetch endpoint from https://oembed.com
$providers = json_decode(file_get_contents('https://oembed.com/providers.json'));
$endpoints = [];

foreach ($providers as $provider) {
    $name = $provider->provider_name;
    $url = $provider->provider_url;

    foreach ($provider->endpoints as $endpoint) {
        saveEndpoint($endpoint->url, $endpoint->schemes ?? ["$url*"]);
    }
}

//Export the endpoints
file_put_contents(
    dirname(__DIR__).'/src/resources/oembed.php',
    sprintf("<?php\ndeclare(strict_types = 1);\n\nreturn %s;\n", VarExporter::export($endpoints, VarExporter::TRAILING_COMMA_IN_ARRAY))
);

echo 'Endpoints saved'.PHP_EOL;

function saveEndpoint(string $url, array $schemes): void
{
    global $endpoints;

    $url = str_replace('{format}', 'json', $url);

    if (!isset($endpoints[$url])) {
        $endpoints[$url] = [];
    }

    foreach ($schemes as $scheme) {
        $pattern = getPattern($scheme);

        if (!in_array($pattern, $endpoints[$url])) {
            $endpoints[$url][] = $pattern;
        }
    }
}

function getPattern(string $pattern): string
{
    $pattern = str_replace(['http://', 'https://'], '', $pattern);
    $pattern = str_replace('\\*', '.*', preg_quote($pattern, '|'));

    return "|^https?://{$pattern}$|i";
}