summaryrefslogtreecommitdiff
path: root/providers/Facebook.php
blob: b1a62911e81821cfbf4136e8d95fe8a6491f144c (plain)
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
<?php

namespace Providers;

require_once('HtmlFeed.php');
require_once('Item.php');

class Facebook extends \Providers\HtmlFeed {

    protected $_cacheTimeout = '5 minutes';

    protected function _getCachePath() {
        return '../cache/facebook.%s';
    }

    protected function _getFeedUrl($feed) {
        return sprintf('https://m.facebook.com/%s', $feed);
    }

    protected function _parseFeedContent($tree) {
        $items = [];
        foreach ($tree->find('div[data-ft]') as $div) {
            $data = json_decode($div->attr()['data-ft'], TRUE);
            if (isset($data['mf_story_key'])) {
                $pIns = $data['page_insights'][$data['page_id']];
                $key = $data['mf_story_key'];
                $texts = [];
                foreach ($div->find('h3') as $h3) {
                    if (!$h3->find(sprintf('a[href^="/%s/"]', $this->_feed))->count() || $h3->find('a')->count() > 1) {
                        $texts[] = trim($h3->text());
                    }
                }
                foreach ($div->find('div>span p') as $p) {
                    $texts[] = trim(strip_tags($p->html()));
                }
                $items[$key] = [
                    'id' => $key,
                    'content' => $div->html(),
                    'texts' => $texts,
                    'time' => $pIns['post_context']['publish_time']
                ];
            }
        }
        return array_values($items);
    }

    protected function _mapItems($content) {
        return array_map(
            function ($obj) {
                $item = new Item();
                $item->ID = $obj['id'];
                $item->Link = sprintf(
                    'https://facebook.com/%s',
                    $obj['id']
                );
                $item->Title = $obj['texts'][0];
                $item->Text = implode('<br />', $obj['texts']);
                $item->Time = $obj['time'];
                return $item;
            },
            $content
        );
    }

    public function title() {
        return sprintf("%s's Facebook page posts", $this->_feed);
    }

}

?>