aboutsummaryrefslogtreecommitdiff
path: root/bridges/PirateCommunityBridge.php
blob: 5a617b0429dc8ffbd75f18d917d36a75452e9871 (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
<?php

class PirateCommunityBridge extends BridgeAbstract
{
    const NAME = 'Pirate-Community Bridge';
    const URI = 'https://raymanpc.com/';
    const CACHE_TIMEOUT = 300; // 5min
    const DESCRIPTION = 'Returns replies to topics';
    const MAINTAINER = 'Roliga';
    const PARAMETERS = [ [
        't' => [
            'name' => 'Topic ID',
            'type' => 'number',
            'exampleValue' => '12651',
            'title' => 'Topic ID from topic URL. If the URL contains t=12 the ID is 12.',
            'required' => true
        ]]];

    private $feedName = '';

    public function detectParameters($url)
    {
        $parsed_url = parse_url($url);

        $host = $parsed_url['host'] ?? null;

        if ($host !== 'raymanpc.com') {
            return null;
        }

        parse_str($parsed_url['query'], $parsed_query);

        if (
            $parsed_url['path'] === '/forum/viewtopic.php'
            && array_key_exists('t', $parsed_query)
        ) {
            return ['t' => $parsed_query['t']];
        }

        return null;
    }

    public function getName()
    {
        if (!empty($this->feedName)) {
            return $this->feedName;
        }

        return parent::getName();
    }

    public function getURI()
    {
        if (!is_null($this->getInput('t'))) {
            return self::URI
                . 'forum/viewtopic.php?t='
                . $this->getInput('t')
                . '&sd=d'; // sort posts decending by ate so first page has latest posts
        }

        return parent::getURI();
    }

    public function collectData()
    {
        $html = getSimpleHTMLDOM($this->getURI());

        $this->feedName = $html->find('head title', 0)->plaintext;

        foreach ($html->find('.post') as $reply) {
            $item = [];

            $item['uri'] = $this->getURI()
                . $reply->find('h3 a', 0)->getAttribute('href');

            $item['title'] = $reply->find('h3 a', 0)->plaintext;

            $author_html = $reply->find('.author', 0);
            // author_html contains the timestamp as text directly inside it,
            // so delete all other child elements
            foreach ($author_html->children as $child) {
                $child->outertext = '';
            }
            // Timestamps are always in UTC+1
            $item['timestamp'] = trim($author_html->innertext) . ' +01:00';

            $item['author'] = $reply
                ->find('.username, .username-coloured', 0)
                ->plaintext;

            $item['content'] = defaultLinkTo(
                $reply->find('.content', 0)->innertext,
                $this->getURI()
            );

            $item['enclosures'] = [];
            foreach ($reply->find('.attachbox img.postimage') as $img) {
                $item['enclosures'][] = urljoin($this->getURI(), $img->src);
            }

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