aboutsummaryrefslogtreecommitdiff
path: root/bridges/NordbayernBridge.php
blob: 94319e2bd4b7b73764d7cea43941068eee7389fc (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php

class NordbayernBridge extends BridgeAbstract
{
    const MAINTAINER = 'schabi.org';
    const NAME = 'Nordbayern';
    const CACHE_TIMEOUT = 3600;
    const URI = 'https://www.nordbayern.de';
    const DESCRIPTION = 'Bridge for Bavarian regional news site nordbayern.de';
    const PARAMETERS = [ [
        'region' => [
            'name' => 'region',
            'type' => 'list',
            'exampleValue' => 'Nürnberg',
            'title' => 'Select a region',
            'values' => [
                'Ansbach' => 'ansbach',
                'Bamberg' => 'bamberg',
                'Bayreuth' => 'bayreuth',
                'Erlangen' => 'erlangen',
                'Forchheim' => 'forchheim',
                'Fürth' => 'fuerth',
                'Gunzenhausen' => 'gunzenhausen',
                'Herzogenaurach' => 'herzogenaurach',
                'Höchstadt' => 'hoechstadt',
                'Neumarkt' => 'neumarkt',
                'Neustadt/Aisch-Bad Windsheim' => 'neustadt-aisch-bad-windsheim',
                'Nürnberg' => 'nuernberg',
                'Nürnberger Land' => 'nuernberger-land',
                'Regensburg' => 'regensburg',
                'Roth' => 'roth',
                'Schwabach' => 'schwabach',
                'Weißenburg' => 'weissenburg'
            ]
        ],
        'policeReports' => [
            'name' => 'Police Reports',
            'type' => 'checkbox',
            'exampleValue' => 'checked',
            'title' => 'Include Police Reports',
        ],
        'hideNNPlus' => [
            'name' => 'Hide NN+ articles',
            'type' => 'checkbox',
            'exampleValue' => 'unchecked',
            'title' => 'Hide all paywall articles on NN'
        ],
        'hideDPA' => [
        'name' => 'Hide dpa articles',
        'type' => 'checkbox',
        'exampleValue' => 'unchecked',
        'title' => 'Hide external articles from dpa'
        ]
    ]];

    public function collectData()
    {
        $region = $this->getInput('region');
        if ($region === 'rothenburg-o-d-t') {
            $region = 'rothenburg-ob-der-tauber';
        }
        $url = self::URI . '/region/' . $region;
        $listSite = getSimpleHTMLDOM($url);

        $this->handleNewsblock($listSite);
    }


    private function getValidImage($picture)
    {
        $img = $picture->find('img', 0);
        if ($img) {
            $imgUrl = $img->src;
            if (!preg_match('#/logo-.*\.png#', $imgUrl)) {
                return '<br><img src="' . $imgUrl . '">';
            }
        }
        return '';
    }

    private function getUseFullContent($rawContent)
    {
        $content = '';
        foreach ($rawContent->children as $element) {
            if (
                ($element->tag === 'p' || $element->tag === 'h3') &&
                $element->class !== 'article__teaser'
            ) {
                $content .= $element;
            } elseif ($element->tag === 'main') {
                $content .= $this->getUseFullContent($element->find('article', 0));
            } elseif ($element->tag === 'header') {
                $content .= $this->getUseFullContent($element);
            } elseif (
                $element->tag === 'div' &&
                !str_contains($element->class, 'article__infobox') &&
                !str_contains($element->class, 'authorinfo')
            ) {
                $content .= $this->getUseFullContent($element);
            } elseif (
                $element->tag === 'section' &&
                (str_contains($element->class, 'article__richtext') ||
                    str_contains($element->class, 'article__context'))
            ) {
                $content .= $this->getUseFullContent($element);
            } elseif ($element->tag === 'picture') {
                $content .= $this->getValidImage($element);
            } elseif ($element->tag === 'ul') {
                $content .= $element;
            }
        }
        return $content;
    }

    private function getTeaser($content)
    {
        $teaser = $content->find('p[class=article__teaser]', 0);
        if ($teaser === null) {
            return '';
        }
        $teaser = $teaser->plaintext;
        $teaser = preg_replace('/[ ]{2,}/', ' ', $teaser);
        $teaser = '<p class="article__teaser">' . $teaser . '</p>';
        return $teaser;
    }

    private function getArticle($link)
    {
        $item = [];
        $article = getSimpleHTMLDOM($link);
        defaultLinkTo($article, self::URI);
        $content = $article->find('article[id=article]', 0);
        $item['uri'] = $link;

        $author = $article->find('.article__author', 1);
        if ($author !== null) {
            $item['author'] = trim($author->plaintext);
        }

        $createdAt = $article->find('[class=article__release]', 0);
        if ($createdAt) {
            $item['timestamp'] = strtotime(str_replace('Uhr', '', $createdAt->plaintext));
        }

        if ($article->find('h2', 0) === null) {
            $item['title'] = $article->find('h3', 0)->innertext;
        } else {
            $item['title'] = $article->find('h2', 0)->innertext;
        }
        $item['content'] = '';

        if ($article->find('section[class*=article__richtext]', 0) === null) {
            $content = $article->find('div[class*=modul__teaser]', 0)
                           ->find('p', 0);
            $item['content'] .= $content;
        } else {
            $content = $article->find('article', 0);
            // change order of article teaser in order to show it on top
            // of the title image. If we didn't do this some rss programs
            // would show the subtitle of the title image as teaser instead
            // of the actuall article teaser.
            $item['content'] .= $this->getTeaser($content);
            $item['content'] .= $this->getUseFullContent($content);
        }

        $categories = $article->find('[class=themen]', 0);
        if ($categories) {
            $item['categories'] = [];
            foreach ($categories->find('a') as $category) {
                $item['categories'][] = $category->innertext;
            }
        }

        $article->clear();
        return $item;
    }

    private function handleNewsblock($listSite)
    {
        $main = $listSite->find('main', 0);
        foreach ($main->find('article') as $article) {
            $url = $article->find('a', 0)->href;
            $url = urljoin(self::URI, $url);
            // exclude nn+ articles if desired
            if (
                $this->getInput('hideNNPlus') &&
                str_contains($url, 'www.nn.de')
            ) {
                continue;
            }

            $item = $this->getArticle($url);

            // exclude police reports if desired
            if (
                !$this->getInput('policeReports') &&
                str_contains($item['content'], 'Hier geht es zu allen aktuellen Polizeimeldungen.')
            ) {
                continue;
            }

            // exclude dpa articles
            if (
                $this->getInput('hideDPA') &&
                str_contains($item['author'], 'dpa')
            ) {
                continue;
            }

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