aboutsummaryrefslogtreecommitdiff
path: root/bridges/FunkBridge.php
blob: e4935ffb9e8700c28a40bb72d29707f4af56038d (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
87
88
89
90
91
<?php

class FunkBridge extends BridgeAbstract
{
    const MAINTAINER = 'µKöff';
    const NAME = 'Funk';
    const URI = 'https://www.funk.net/';
    const DESCRIPTION = 'Videos per channel of German public video-on-demand service Funk';

    const PARAMETERS = [
        'Channel' => [
            'channel' => [
                'name' => 'Slug',
                'type' => 'text',
                'required' => true,
                'exampleValue' => 'game-two-856'
            ],
            'max' => [
                'name' => 'Maximum',
                'type' => 'number',
                'defaultValue' => '-1'
            ]
        ]
    ];

    public function collectData()
    {
        switch ($this->queriedContext) {
            case 'Channel':
                $url = static::URI . 'data/videos/byChannelAlias/' . $this->getInput('channel') . '/';
                if (!empty($this->getInput('max')) && $this->getInput('max') >= 0) {
                    $url .= '?size=' . $this->getInput('max');
                }

                $jsonString = getContents($url);
                $json = json_decode($jsonString, true);

                foreach ($json['list'] as $element) {
                    $this->items[] = $this->collectArticle($element);
                }
                break;
            default:
                returnServerError('Unknown context!');
        }
    }

    private function collectArticle($element)
    {
        $item = [];
        $item['uri'] = static::URI . 'channel/' . $element['channelAlias'] . '/' . $element['alias'];
        $item['title'] = $element['title'];
        $item['timestamp'] = $element['publicationDate'];
        $item['author'] = str_replace('-' . $element['channelId'], '', $element['channelAlias']);
        $item['content'] = $element['shortDescription'];
        $item['enclosures'] = [
            'https://www.funk.net/api/v4.0/thumbnails/' . $element['imageLandscape']
        ];
        $item['uid'] = $element['entityId'];
        return $item;
    }

    public function detectParameters($url)
    {
        $regex = '/^https?:\/\/(?:www\.)?funk\.net\/channel\/([^\/]+).*$/';
        if (preg_match($regex, $url, $urlMatches) > 0) {
            return [
                'context' => 'Channel',
                'channel' => $urlMatches[1]
            ];
        } else {
            return null;
        }
    }

    public function getIcon()
    {
        return 'https://www.funk.net/img/favicons/favicon-192x192.png';
    }

    public function getName()
    {
        switch ($this->queriedContext) {
            case 'Channel':
                if (!empty($this->getInput('channel'))) {
                    return $this->getInput('channel');
                }
                break;
        }
        return parent::getName();
    }
}