aboutsummaryrefslogtreecommitdiff
path: root/bridges/CeskaTelevizeBridge.php
blob: a996bd115fba05776d0addcd743cb1f2aee8c148 (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
<?php

class CeskaTelevizeBridge extends BridgeAbstract
{
    const NAME = 'Česká televize Bridge';
    const URI = 'https://www.ceskatelevize.cz';
    const CACHE_TIMEOUT = 3600;
    const DESCRIPTION = 'Return newest videos';
    const MAINTAINER = 'kolarcz';

    const PARAMETERS = [
        [
            'url' => [
                'name' => 'url to the show',
                'required' => true,
                'exampleValue' => 'https://www.ceskatelevize.cz/porady/1097181328-udalosti/'
            ]
        ]
    ];

    public function collectData()
    {
        $url = $this->getInput('url');

        $validUrl = '/^(https:\/\/www\.ceskatelevize\.cz\/porady\/\d+-[a-z0-9-]+\/)(bonus\/)?$/';
        if (!preg_match($validUrl, $url, $match)) {
            returnServerError('Invalid url');
        }

        $category = $match[4] ?? 'nove';
        $fixedUrl = "{$match[1]}dily/{$category}/";

        $html = getSimpleHTMLDOM($fixedUrl);

        $this->feedUri = $fixedUrl;
        $this->feedName = str_replace('Přehled dílů — ', '', $this->fixChars($html->find('title', 0)->plaintext));
        if ($category !== 'nove') {
            $this->feedName .= " ({$category})";
        }

        foreach ($html->find('#episodeListSection a[data-testid=card]') as $element) {
            $itemContent = $element->find('p[class^=content-]', 0);
            $itemDate = $element->find('div[class^=playTime-] span, [data-testid=episode-item-broadcast] span', 0);
            $item = [
                'title'     => $this->fixChars($element->find('h3', 0)->plaintext),
                'uri'       => self::URI . $element->getAttribute('href'),
                'content'   => '<img src="' . $element->find('img', 0)->getAttribute('srcset') . '" /><br />' . $this->fixChars($itemContent->plaintext),
                'timestamp' => $this->getUploadTimeFromString($itemDate->plaintext),
            ];

            $this->items[] = $item;
        }
    }

    private function getUploadTimeFromString($string)
    {
        if (strpos($string, 'dnes') !== false) {
            return strtotime('today');
        } elseif (strpos($string, 'včera') !== false) {
            return strtotime('yesterday');
        } elseif (!preg_match('/(\d+).\s(\d+).(\s(\d+))?/', $string, $match)) {
            returnServerError('Could not get date from Česká televize string');
        }

        $date = sprintf('%04d-%02d-%02d', $match[3] ?? date('Y'), $match[2], $match[1]);
        return strtotime($date);
    }

    private function fixChars($text)
    {
        return html_entity_decode($text, ENT_QUOTES, 'UTF-8');
    }

    public function getURI()
    {
        return $this->feedUri ?? parent::getURI();
    }

    public function getName()
    {
        return $this->feedName ?? parent::getName();
    }
}