rss-bridge icon indicating copy to clipboard operation
rss-bridge copied to clipboard

Bridge Requst for Westfälische Nachrichten

Open redcluster opened this issue 2 years ago • 3 comments

Bridge request

General information

  • Host URI for the bridge (i.e. https://github.com): https://www.wn.de

  • Which information would you like to see? Title, Date, Text, Pictures

  • How should the information be displayed/formatted? Text ordered as on website

  • Which of the following parameters do you expect?

    • [X] Title
    • [X] URI (link to the original article)
    • [X] Author
    • [X] Timestamp
    • [X] Content (the content of the article)
    • [X] Enclosures (pictures, videos, etc...)
    • [ ] Categories (categories, tags, etc...)

Options

  • [ ] Limit number of returned items
    • Default limit: 5
  • [ ] Load full articles
    • Cache articles (articles are stored in a local cache on first request): yes
    • Cache timeout (max = 24 hours): 24 hours
  • [X] Balance requests (RSS-Bridge uses cached versions to reduce bandwith usage)
    • Timeout (default = 5 minutes, max = 24 hours): 5 minutes

Code

Please review my code below. It should work but I'm not sure if there are any optimizations because I'm new to writing bridges.

<?php
class WestfaelischeNachrichtenBridge extends FeedExpander {
        const MAINTAINER = 'redcluster';
        const NAME = 'Westfälische Nachrichten Bridge';
        const URI = 'https://wn.de/';
        const CACHE_TIMEOUT = 3600; // 1h
        const DESCRIPTION = 'Returns the full articles instead of only the intro';
        const PARAMETERS = array(array(
                'category' => array(
                        'name' => 'Category',
                        'type' => 'list',
                        'values' => array(
                                'Alle Nachrichten'
                                => 'https://www.wn.de/rss/feed',
                                'Ascheberg'
                                => 'https://www.wn.de/rss/feed?subcategory=/muensterland/kreis-coesfeld/ascheberg',
                                'Drensteinfurt'
                                => 'https://www.wn.de/rss/feed?subcategory=/muensterland/kreis-warendorf/drensteinfurt'
                        )
                ),
                'limit' => array(
                        'name' => 'Limit',
                        'type' => 'number',
                        'required' => false,
                        'title' => 'Specify number of full articles to return',
                        'defaultValue' => 5
                )
        ));
        const LIMIT = 5;

        public function collectData() {
                $this->collectExpandableDatas(
                        $this->getInput('category'),
                        $this->getInput('limit') ?: static::LIMIT
                );
        }

        protected function parseItem($feedItem) {
                $item = parent::parseItem($feedItem);
                $uri = $item['uri'];

                $article = getSimpleHTMLDOMCached($uri)
                        or returnServerError('Could not open article: ' . $uri);

                if ($article) {
                        $article = defaultLinkTo($article, $uri);
                        $item = $this->addArticleToItem($item, $article);
                }

                return $item;
        }

        private function addArticleToItem($item, $article) {
                if($author = $article->find('p[class="fp-article-heading__author"]', 0))
                        $item['author'] = $author->plaintext;

                $city = $article->find('p[class="fp-article-heading__city"]', 0)->innertext;
                $excerpt = $article->find('p[class="fp-article-heading__excerpt"]', 0)->innertext;
                $content = $article->find('p[class="fp-paragraph"]', 0)->innertext;

                $pictureUrl = $item['enclosures'][0];
                $picture = "<img src=\"" . $pictureUrl . "\">";

                $content = "";
                foreach($article->find('p[class="fp-paragraph"]') as $element) {
                         $content .= $element->innertext . "\n\n";
                }

                $item['content'] = $city." - ".$excerpt." \n".$picture." \n".$content;
                $item['uid'] =  $item['title'];

                return $item;
        }
}

redcluster avatar Jan 25 '23 19:01 redcluster

Looks good and tested. Fixed up code:

<?php

class WestfaelischeNachrichtenBridge extends FeedExpander
{
    const MAINTAINER = 'redcluster';
    const NAME = 'Westfälische Nachrichten Bridge';
    const URI = 'https://wn.de/';
    const CACHE_TIMEOUT = 3600; // 1h
    const DESCRIPTION = 'Returns the full articles instead of only the intro';
    const PARAMETERS = [
        [
            'category' => [
                'name' => 'Category',
                'type' => 'list',
                'values' => [
                    'Alle Nachrichten' => 'https://www.wn.de/rss/feed',
                    'Ascheberg' => 'https://www.wn.de/rss/feed?subcategory=/muensterland/kreis-coesfeld/ascheberg',
                    'Drensteinfurt' => 'https://www.wn.de/rss/feed?subcategory=/muensterland/kreis-warendorf/drensteinfurt',
                ],
            ],
            'limit' => [
                'name' => 'Limit',
                'type' => 'number',
                'required' => false,
                'title' => 'Specify number of full articles to return',
                'defaultValue' => 5,
            ]
        ]
    ];

    const LIMIT = 5;

    public function collectData()
    {
        $this->collectExpandableDatas(
            $this->getInput('category'),
            $this->getInput('limit') ?: static::LIMIT
        );
    }

    protected function parseItem($feedItem)
    {
        $item = parent::parseItem($feedItem);
        $uri = $item['uri'];

        $article = getSimpleHTMLDOMCached($uri)
                or returnServerError('Could not open article: ' . $uri);

        if ($article) {
            $article = defaultLinkTo($article, $uri);
            $item = $this->addArticleToItem($item, $article);
        }

        return $item;
    }

    private function addArticleToItem($item, $article)
    {
        if ($author = $article->find('p[class="fp-article-heading__author"]', 0)) {
            $item['author'] = $author->plaintext;
        }

        $city = $article->find('p[class="fp-article-heading__city"]', 0)->innertext;
        $excerpt = $article->find('p[class="fp-article-heading__excerpt"]', 0)->innertext;

        // This var is unused!
        $content = $article->find('p[class="fp-paragraph"]', 0)->innertext;

        $pictureUrl = $item['enclosures'][0];
        $picture = '<img src="' . $pictureUrl . '">';

        $content = '';
        foreach ($article->find('p[class="fp-paragraph"]') as $element) {
            $content .= $element->innertext . "\n\n";
        }

        $item['content'] = $city . ' - ' . $excerpt . " \n" . $picture . " \n" . $content;
        $item['uid'] = $item['title'];

        return $item;
    }
}

dvikan avatar Jan 27 '23 02:01 dvikan

Thank you! What is the process to move the code into an official Bridge?

redcluster avatar Jan 31 '23 19:01 redcluster

Learn how to create a pull request.

dvikan avatar Jan 31 '23 21:01 dvikan