aboutsummaryrefslogtreecommitdiff
path: root/bridges/YouTubeFeedExpanderBridge.php
blob: 953674ec5db4f7fbe92cbca013b61c8c42741002 (plain) (blame)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

class YouTubeFeedExpanderBridge extends FeedExpander
{
    const NAME = 'YouTube Feed Expander';
    const MAINTAINER = 'phantop';
    const URI = 'https://www.youtube.com/';
    const DESCRIPTION = 'Returns the latest videos from a YouTube channel';
    const PARAMETERS = [[
        'channel' => [
            'name' => 'Channel ID',
            'required' => true,
            // Example: vinesauce
            'exampleValue' => 'UCzORJV8l3FWY4cFO8ot-F2w',
        ],
        'embed' => [
            'name' => 'Add embed to entry',
            'type' => 'checkbox',
            'required' => false,
            'title' => 'Add embed to entry',
            'defaultValue' => 'checked',
        ],
        'embedurl' => [
            'name' => 'Use embed page as entry url',
            'type' => 'checkbox',
            'required' => false,
            'title' => 'Use embed page as entry url',
        ],
        'nocookie' => [
            'name' => 'Use nocookie embed page',
            'type' => 'checkbox',
            'required' => false,
            'title' => 'Use nocookie embed page'
        ],
    ]];

    public function getIcon()
    {
        if ($this->getInput('channel') != null) {
            $html = getSimpleHTMLDOMCached($this->getURI());
            $scriptRegex = '/var ytInitialData = (.*?);<\/script>/';
            $result = preg_match($scriptRegex, $html, $matches);
            if (isset($matches[1])) {
                $json = json_decode($matches[1]);
                return $json->metadata->channelMetadataRenderer->avatar->thumbnails[0]->url;
            }
        }
        return parent::getIcon();
    }

    public function collectData()
    {
        $url = 'https://www.youtube.com/feeds/videos.xml?channel_id=' . $this->getInput('channel');
        $this->collectExpandableDatas($url);
    }

    protected function parseItem(array $item)
    {
        $id = $item['yt']['videoId'];
        $item['comments'] = $item['uri'] . '#comments';
        $item['uid'] = $item['id'];

        $thumbnail = sprintf('https://img.youtube.com/vi/%s/maxresdefault.jpg', $id);
        $item['enclosures'] = [$thumbnail];

        $item['content'] = $item['media']['group']['description'];
        $item['content'] = str_replace("\n", '<br>', $item['content']);
        unset($item['media']);

        $embedURI = self::URI;
        if ($this->getInput('nocookie')) {
            $embedURI = 'https://www.youtube-nocookie.com/';
        }
        $embed = $embedURI . 'embed/' . $id;
        if ($this->getInput('embed')) {
            $iframe_fmt = '<iframe width="448" height="350" src="%s" title="%s" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'; //phpcs:ignore
            $iframe = sprintf($iframe_fmt, $embed, $item['title']) . '<br>';
            $item['content'] = $iframe . $item['content'];
        }
        if ($this->getInput('embedurl')) {
            $item['uri'] = $embed;
        }

        return $item;
    }
}