aboutsummaryrefslogtreecommitdiff
path: root/bridges/ASRockNewsBridge.php
blob: 1a3279784a05468f5ce1afa7b73b0ccecd04af39 (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
<?php

class ASRockNewsBridge extends BridgeAbstract
{
    const NAME = 'ASRock News Bridge';
    const URI = 'https://www.asrock.com';
    const DESCRIPTION = 'Returns latest news articles';
    const MAINTAINER = 'VerifiedJoseph';
    const PARAMETERS = [];

    const CACHE_TIMEOUT = 3600; // 1 hour

    public function collectData()
    {
        $html = getSimpleHTMLDOM(self::URI . '/news/index.asp');

        $html = defaultLinkTo($html, self::URI . '/news/');

        foreach ($html->find('div.inner > a') as $index => $a) {
            $item = [];

            $articlePath = $a->href;

            $articlePageHtml = getSimpleHTMLDOMCached($articlePath, self::CACHE_TIMEOUT);

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

            $contents = $articlePageHtml->find('div.Contents', 0);

            $item['uri'] = $articlePath;
            $item['title'] = $contents->find('h3', 0)->innertext;

            $contents->find('h3', 0)->outertext = '';

            $item['content'] = $contents->innertext;
            $item['timestamp'] = $this->extractDate($a->plaintext);

            $img = $a->find('img', 0);
            if ($img) {
                $item['enclosures'][] = $img->src;
            }

            $this->items[] = $item;

            if (count($this->items) >= 10) {
                break;
            }
        }
    }

    private function extractDate($text)
    {
        $dateRegex = '/^([0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2})/';

        $text = trim($text);

        if (preg_match($dateRegex, $text, $matches)) {
            return $matches[1];
        }

        return '';
    }
}