summaryrefslogtreecommitdiff
path: root/providers/Facebook.php
blob: a37af2767e8e6f66f60e82a6fa4fd5fcd7c62db4 (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
72
73
74
75
<?php

namespace Providers;

require_once('Provider.php');
require_once('Item.php');
require_once('../lib/facebook-graph-sdk/src/Facebook/autoload.php');

class Facebook extends \Providers\Provider {

    protected $_cacheTimeout = '10 seconds';
    private $_api;

    public function __construct($feed, $options=[]) {
        parent::__construct($feed, $options);
        $config = json_decode(file_get_contents('../config/facebook.json'), TRUE);
        $this->_api = new \Facebook\Facebook($config);
    }

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

    protected function _fetchItems() {
        $request = new \Facebook\FacebookRequest(
            $this->_api->getApp(),
            $this->_api->getApp()->getAccessToken(),
            'GET',
            sprintf('/%s/posts', $this->_feed)
        );
        $data = $this->_api->getClient()->sendRequest($request)->getDecodedBody()['data'];
        return $data;
    }

    protected function _spamFilter($items) {
      return $items;
    }

    protected function _mapItems($content) {
        return array_map(
            function($i) {
                $item = new Item();
                $item->ID = $i['id'];
                $item->Title = str_replace(
                    "\n", ' ',
                    isset($i['story']) ? $i['story'] : (
                        isset($i['message']) ? $i['message'] : $i['id']
                    )
                );
                $item->Link = sprintf(
                    'https://facebook.com/%s',
                    $i['id']
                );
                $item->Text = nl2br(
                    isset($i['message']) ? $i['message'] : (
                        isset($i['story']) ? $i['story'] : $i['id']
                    )
                );
                $item->Time = strtotime($i['created_time']);
                return $item;
            }, $content
        );
    }

    protected function _sortContent($content) {
        return $content;
    }

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

}

?>