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

class StorytelBridge extends BridgeAbstract
{
    const NAME = 'Storytel List Bridge';
    const URI = 'https://www.storytel.com/tr';
    const DESCRIPTION = 'Fetches books from a Storytel list, including title, author, and cover image.';
    const MAINTAINER = 'Okbaydere';
    const PARAMETERS = [
        'List' => [
            'url' => [
                'name' => 'Storytel List URL',
                'required' => true,
                'exampleValue' => 'https://www.storytel.com/tr/lists/23d09e0bd8fe4d998d1832ddbfa18166',
            ],
        ],
    ];

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

        if (!preg_match('/^https:\/\/www\.storytel\.com/', $url)) {
            returnServerError('Invalid URL: Only Storytel URLs are allowed.');
        }

        $html = getSimpleHTMLDOM($url);

        foreach ($html->find('li.sc-4615116a-1') as $element) {
            $item = [];

            $titleElement = $element->find('span.sc-b1963858-0.hoTsmF', 0);
            $item['title'] = $titleElement ? $titleElement->plaintext : 'No title';

            $authorElement = $element->find('span.sc-b1963858-0.ghYMwH', 0);
            $item['author'] = $authorElement ? $authorElement->plaintext : 'Unknown author';

            $imgElement = $element->find('img.sc-da400893-5', 0);
            $coverUrl = $imgElement ? $imgElement->getAttribute('srcset') : '';
            if ($coverUrl) {
                $coverUrls = explode(', ', $coverUrl);
                $bestCoverUrl = trim(end($coverUrls));
                $item['content'] = '<img src="' . preg_replace('/\?.*/', '', $bestCoverUrl) . '"/>';
            }

            $linkElement = $element->find('a', 0);
            $item['uri'] = $linkElement ? 'https://www.storytel.com' . $linkElement->getAttribute('href') : $url;

            $item['content'] .= '<p>Author: ' . $item['author'] . '</p>';
            $item['content'] .= '<p><a href="' . $item['uri'] . '">More details</a></p>';

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