aboutsummaryrefslogtreecommitdiff
path: root/bridges/ScribbleHubBridge.php
blob: b4f7beaa13cb39eee0c1e43087c787413ebb5567 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

class ScribbleHubBridge extends FeedExpander
{
    const MAINTAINER = 'phantop';
    const NAME = 'Scribble Hub';
    const URI = 'https://scribblehub.com/';
    const DESCRIPTION = 'Returns chapters from Scribble Hub.';
    const PARAMETERS = [
        'All' => [],
        'Author' => [
            'uid' => [
                'name' => 'uid',
                'required' => true,
                // Example: miriamrobern's stories
                'exampleValue' => '149271',
            ],
        ],
        'Series' => [
            'sid' => [
                'name' => 'sid',
                'required' => true,
                // Example: latest chapters from Uskweirs
                'exampleValue' => '965299',
            ],
        ],
        'List' => [
            'url' => [
                'name' => 'url',
                'required' => true,
                // Example: latest stories with the 'Transgender' tag
                'exampleValue' => 'https://www.scribblehub.com/series-finder/?sf=1&gi=6&tgi=1088&sort=dateadded',
            ],
        ]
    ];

    public function getIcon()
    {
        return self::URI . 'favicon.ico';
    }

    public function collectData()
    {
        $url = 'https://rssscribblehub.com/rssfeed.php?type=';
        if ($this->queriedContext === 'List') {
            $this->collectList($this->getURI());
            return;
        }
        if ($this->queriedContext === 'Author') {
            $url = $url . 'author&uid=' . $this->getInput('uid');
        } else { //All and Series use the same source feed
            $url = $url . 'main';
        }
        $this->collectExpandableDatas($url);
    }

    protected $author = '';

    private function collectList($url)
    {
        $html = getSimpleHTMLDOMCached($url);
        foreach ($html->find('.search_main_box') as $element) {
            $item = [];

            $title = $element->find('.search_title a', 0);
            $item['title'] = $title->plaintext;
            $item['uri'] = $title->href;

            $strdate = $element->find('[title="Last Updated"]', 0)->plaintext;
            $item['timestamp'] = strtotime($strdate);
            $item['uid'] = $item['uri'];

            $details = getSimpleHTMLDOMCached($item['uri']);
            $item['enclosures'][] = $details->find('.fic_image img', 0)->src;
            $item['content'] = $details->find('.wi_fic_desc', 0);

            foreach ($details->find('.fic_genre') as $tag) {
                $item['categories'][] = $tag->plaintext;
            }
            foreach ($details->find('.stag') as $tag) {
                $item['categories'][] = $tag->plaintext;
            }

            $read_url = $details->find('.read_buttons a', 0)->href;
            $read_html = getSimpleHTMLDOMCached($read_url);
            $item['content'] .= '<hr><h3>';
            $item['content'] .= $read_html->find('.chapter-title', 0);
            $item['content'] .= '</h3>';
            $item['content'] .= $read_html->find('#chp_raw', 0);

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

    protected function parseItem(array $item)
    {
        //For series, filter out other series from 'All' feed
        if (
            $this->queriedContext === 'Series'
            && preg_match('/read\/' . $this->getInput('sid') . '-/', $item['uri']) !== 1
        ) {
            return [];
        }

        if ($this->queriedContext === 'Author') {
            $this->author = $item['author'];
        }

        $item['comments'] = $item['uri'] . '#comments';
        $item['uid'] = $item['uri'];

        try {
            $dom = getSimpleHTMLDOMCached($item['uri']);
        } catch (HttpException $e) {
            // 403 Forbidden, This means we got anti-bot response
            if ($e->getCode() === 403 || $e->getCode() === 429) {
                return $item;
            }
            throw $e;
        }

        $dom = defaultLinkTo($dom, self::URI);

        //Retrieve full description from page contents
        $item['content'] = $dom->find('#chp_raw', 0);

        //Retrieve image for thumbnail
        $item_image = $dom->find('.s_novel_img > img', 0)->src;
        $item['enclosures'] = [$item_image];

        //Restore lost categories
        $item_story = html_entity_decode($dom->find('.chp_byauthor > a', 0)->innertext);
        $item_sid   = $dom->find('#mysid', 0)->value;
        $item['categories'] = [$item_story, $item_sid];

        //Generate UID
        $item_pid = $dom->find('#mypostid', 0)->value;

        return $item;
    }

    public function getName()
    {
        $name = parent::getName() . " $this->queriedContext";
        switch ($this->queriedContext) {
            case 'Author':
                $title = $this->author;
                break;
            case 'Series':
                try {
                    $page = getSimpleHTMLDOMCached(self::URI . 'series/' . $this->getInput('sid') . '/a');
                } catch (HttpException $e) {
                    // 403 Forbidden, This means we got anti-bot response
                    if ($e->getCode() === 403) {
                        return $name;
                    }
                    throw $e;
                }
                $title = html_entity_decode($page->find('.fic_title', 0)->plaintext);
                break;
            case 'List':
                $page = getSimpleHTMLDOMCached($this->getURI());
                $title = $page->find('head > title', 0)->plaintext;
                $title = explode(' |', $title)[0];
                break;
        }
        if (isset($title)) {
            $name .= " - $title";
        }
        return $name;
    }

    public function getURI()
    {
        $uri = parent::getURI();
        switch ($this->queriedContext) {
            case 'Author':
                $uri = self::URI . 'profile/' . $this->getInput('uid');
                break;
            case 'Series':
                $uri = self::URI . 'series/' . $this->getInput('sid') . '/a';
                break;
            case 'List':
                $uri = $this->getInput('url');
                break;
        }
        return $uri;
    }
}