aboutsummaryrefslogtreecommitdiff
path: root/bridges/WikiLeaksBridge.php
blob: 081446883ec514fc7a518db13229605341524fc5 (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
<?php

class WikiLeaksBridge extends BridgeAbstract
{
    const NAME = 'WikiLeaks';
    const URI = 'https://wikileaks.org';
    const DESCRIPTION = 'Returns the latest news or articles from WikiLeaks';
    const MAINTAINER = 'logmanoriginal';
    const PARAMETERS = [
        [
            'category' => [
                'name' => 'Category',
                'type' => 'list',
                'title' => 'Select your category',
                'values' => [
                    'News' => '-News-',
                    'Leaks' => [
                        'All' => '-Leaks-',
                        'Intelligence' => '+-Intelligence-+',
                        'Global Economy' => '+-Global-Economy-+',
                        'International Politics' => '+-International-Politics-+',
                        'Corporations' => '+-Corporations-+',
                        'Government' => '+-Government-+',
                        'War & Military' => '+-War-Military-+'
                    ]
                ],
                'defaultValue' => 'news'
            ],
            'teaser' => [
                'name' => 'Show teaser',
                'type' => 'checkbox',
                'title' => 'If checked feeds will display the teaser',
                'defaultValue' => 'checked'
            ]
        ]
    ];

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

        // News are presented differently
        switch ($this->getInput('category')) {
            case '-News-':
                $this->loadNewsItems($html);
                break;
            default:
                $this->loadLeakItems($html);
        }
    }

    public function getURI()
    {
        if (!is_null($this->getInput('category'))) {
            return static::URI . '/' . $this->getInput('category') . '.html';
        }

        return parent::getURI();
    }

    public function getName()
    {
        if (!is_null($this->getInput('category'))) {
            $category = array_search(
                $this->getInput('category'),
                static::PARAMETERS[0]['category']['values']
            );

            if ($category === false) {
                $category = array_search(
                    $this->getInput('category'),
                    static::PARAMETERS[0]['category']['values']['Leaks']
                );
            }

            return $category . ' - ' . static::NAME;
        }

        return parent::getName();
    }

    private function loadNewsItems($html)
    {
        $articles = $html->find('div.news-articles ul li');

        if (is_null($articles) || count($articles) === 0) {
            return;
        }

        foreach ($articles as $article) {
            $item = [];

            $item['title'] = $article->find('h3', 0)->plaintext;
            $item['uri'] = static::URI . $article->find('h3 a', 0)->href;
            $item['content'] = $article->find('div.introduction', 0)->plaintext;
            $timestamp = $article->find('div.timestamp', 0);
            if ($timestamp) {
                $item['timestamp'] = strtotime($timestamp->plaintext);
            }
            $this->items[] = $item;
        }
    }

    private function loadLeakItems($html)
    {
        $articles = $html->find('li.tile');

        if (is_null($articles) || count($articles) === 0) {
            return;
        }

        foreach ($articles as $article) {
            $item = [];

            $item['title'] = $article->find('h2', 0)->plaintext;
            $item['uri'] = static::URI . $article->find('a', 0)->href;

            $teaser = static::URI . '/' . $article->find('div.teaser img', 0)->src;

            if ($this->getInput('teaser')) {
                $item['content'] = '<img src="'
                . $teaser
                . '" /><p>'
                . $article->find('div.intro', 0)->plaintext
                . '</p>';
            } else {
                $item['content'] = $article->find('div.intro', 0)->plaintext;
            }

            $item['timestamp'] = strtotime($article->find('div.timestamp', 0)->plaintext);
            $item['enclosures'] = [$teaser];

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