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

namespace Providers;

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

class Facebook extends \Providers\Provider {

    protected $_cacheTimeout = '300 years';

    public function __construct($feed, $options) {
        parent::__construct($feed, $options);
        if (isset($this->_options['force'])) {
            $this->_cacheTimeout = '1 second';
        }
    }

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

    protected function _fetchItems() {
        $jsonContent = [];
        exec('direnv exec ' .
             implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', 'bin', 'fb-scrape']) . ' ' .
             'python ' . implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', 'bin', 'fb-scrape', 'get-fb-content.py']) . ' ' .
             escapeshellarg($this->_feed), $jsonContent);
        $cacheFile = sprintf($this->_getCachePath(), $this->_feed);
        if (file_exists($cacheFile)) {
            $cache = unserialize($this->_getCache($cacheFile));
        }
        else {
            $cache = [];
        }
        $fetched = json_decode(implode(PHP_EOL, $jsonContent), TRUE);
        $cacheIDs = array_map(function($obj) {
            return $obj['id'];
        }, $cache);
        foreach ($fetched as $fetchedItem) {
            if (!in_array($fetchedItem['id'], $cacheIDs)) {
                $cache[] = $fetchedItem;
            }
        }
        return $cache;
    }

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

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

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

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

}

?>