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

namespace Providers;

require_once('Provider.php');
require_once('Item.php');
require_once('../lib/querypath/src/qp.php');

class Facebook extends \Providers\Provider {

    protected $_cacheTimeout = '5 minutes';
    //protected $_cacheTimeout = '1 second';
    private $_encoding;

    public function __construct($feed, $options=[]) {
        parent::__construct($feed, $options);
    }

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

    private function __getWWWContent() {
        $header = array();
        $header[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
        $header[] = 'Cache-Control: max-age=0';
        $header[] = 'Connection: keep-alive';
        $header[] = 'Keep-Alive: 300';
        $header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
        $header[] = 'Pragma: ';
        $ch = curl_init(sprintf('https://m.facebook.com/%s/', $this->_feed));
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        $page = curl_exec($ch);
        curl_close ($ch);
        $this->_encoding = mb_detect_encoding($page);
        return $page;
    }

    protected function _fetchItems() {
        $items = [];
        $page = $this->__getWWWContent();
        $tree = htmlqp($page, NULL, ['convert_from_encoding' => $this->_encoding, 'convert_to_encoding' => $this->_encoding]);
        $parents = [];
        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 _spamFilter($items) {
      return $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
        );
    }

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

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

}

?>