aboutsummaryrefslogtreecommitdiff
path: root/bridges/LichessBridge.php
blob: 8b4986776662b6f086384a6710b871dd59b4fab9 (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
<?php

class LichessBridge  extends BridgeAbstract
{
    public function loadMetadatas()
    {
        $this->maintainer = 'AmauryCarrade';
        $this->name = 'Lichess Blog';
        $this->uri = 'http://lichess.org/blog';
        $this->description = 'Returns the 5 newest posts from the Lichess blog (full text)';
        $this->update = "2016-08-06";
    }

    public function collectData(array $param)
    {
        $xml_feed = $this->file_get_html('http://fr.lichess.org/blog.atom') or $this->returnError('Could not retrieve Lichess blog feed.', 404);

        $posts_loaded = 0;
        foreach($xml_feed->find('entry') as $entry)
        {
            if ($posts_loaded < 5)
            {
                $item = new \Item();

                $item->title     = html_entity_decode($entry->find('title', 0)->innertext);
                $item->author    = $entry->find('author', 0)->find('name', 0)->innertext;
                $item->uri       = $entry->find('id', 0)->plaintext;
                $item->timestamp = strtotime($entry->find('published', 0)->plaintext);

                $item->content = $this->retrieve_lichess_post($item->uri);

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

    private function retrieve_lichess_post($blog_post_uri)
    {
        $blog_post_html = $this->file_get_html($blog_post_uri);
        $blog_post_div  = $blog_post_html->find('#lichess_blog', 0);

        $post_chapo   = $blog_post_div->find('.shortlede', 0)->innertext;
        $post_content = $blog_post_div->find('.body', 0)->innertext;

        $content  = '<p><em>' . $post_chapo . '</em></p>';
        $content .= '<div>' . $post_content . '</div>';

        return $content;
    }
}