aboutsummaryrefslogtreecommitdiff
path: root/bridges/CubariProxyBridge.php
blob: 8afd2275c683b29d791d5189045568caeecb3810 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php

class CubariProxyBridge extends BridgeAbstract
{
    const NAME = 'Cubari Proxy';
    const MAINTAINER = 'phantop';
    const URI = 'https://cubari.moe';
    const DESCRIPTION = 'Returns chapters from Cubari.';
    const PARAMETERS = [[
        'service' => [
            'name' => 'Content service',
            'type' => 'list',
            'defaultValue' => 'mangadex',
            'values' => [
                'MangAventure' => 'mangadventure',
                'MangaDex' => 'mangadex',
                'MangaKatana' => 'mangakatana',
                'MangaSee' => 'mangasee',
            ]
        ],
        'series' => [
            'name' => 'Series ID/Name',
            'exampleValue' => '8c1d7d0c-e0b7-4170-941d-29f652c3c19d', # KnH
            'required' => true,
        ],
        'fetch' => [
            'name' => 'Fetch chapter page images',
            'type' => 'list',
            'title' => 'Places chapter images in feed contents. Entries will consume more bandwidth.',
            'defaultValue' => 'c',
            'values' => [
                'None' => 'n',
                'Content' => 'c',
                'Enclosure' => 'e'
            ]
        ],
        'limit' => self::LIMIT
    ]];

    private $title;

    public function collectData()
    {
        $limit = $this->getInput('limit') ?? 10;

        $url = parent::getURI() . '/read/api/' . $this->getInput('service') . '/series/' . $this->getInput('series');
        $json = Json::decode(getContents($url));
        $this->title = $json['title'];

        $chapters = $json['chapters'];
        krsort($chapters);

        $count = 0;
        foreach ($chapters as $number => $element) {
            $item = [];
            $item['uri'] = $this->getURI() . '/' . $number;

            if ($element['title']) {
                $item['title'] = $number . ' - ' . $element['title'];
            } else {
                $item['title'] = 'Volume ' . $element['volume'] . ' Chapter ' . $number;
            }

            $group = '1';
            if (isset($element['release_date'])) {
                $dates = $element['release_date'];
                $date = max($dates);
                $item['timestamp'] = $date;
                $group = array_keys($dates, $date)[0];
            }
            $page = $element['groups'][$group];
            $item['author'] = $json['groups'][$group];
            $api = parent::getURI() . $page;
            $item['uid'] = $page;
            $item['comments'] = $api;

            if ($this->getInput('fetch') != 'n') {
                $pages = [];
                try {
                    $jsonp = getContents($api);
                    $pages = Json::decode($jsonp);
                } catch (HttpException $e) {
                    // allow error 500, as it's effectively a 429
                    if ($e->getCode() != 500) {
                        throw $e;
                    }
                }
                if ($this->getInput('fetch') == 'e') {
                    $item['enclosures'] = $pages;
                }
                if ($this->getInput('fetch') == 'c') {
                    $item['content'] = '';
                    foreach ($pages as $img) {
                        $item['content'] .= '<img src="' . $img . '"/>';
                    }
                }
            }

            if ($count++ == $limit) {
                break;
            }

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

    public function getName()
    {
        $name = parent::getName();
        if (isset($this->title)) {
            $name .= ' - ' . $this->title;
        }
        return $name;
    }

    public function getURI()
    {
        $uri = parent::getURI();
        if ($this->getInput('service')) {
            $uri .= '/read/' . $this->getInput('service') . '/' . $this->getInput('series');
        }
        return $uri;
    }

    public function getIcon()
    {
        return parent::getURI() . '/static/favicon.png';
    }
}