summaryrefslogtreecommitdiff
path: root/providers/Substack.php
blob: 83a6e06f8f820f4951967c2f5448631d94ebc5f8 (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
<?php

namespace Providers;

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

class Substack extends \Providers\JsonFeed {

    protected function _getFeedUrl($feed) {
        return sprintf('https://%s.substack.com/api/v1/posts', $feed);
    }

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

    protected function _parseFeedContent($feed) {
        return $feed;
    }

    protected function _mapItems($content) {
        $items = [];
        foreach ($content as $item) {
            $itemObject = new Item();
            $itemObject->ID = sprintf($this->feedUrl . $item->id);
            $itemObject->Title = $item->title;
            if ($item->subtitle) {
                $itemObject->Title .= ' | ' . $item->subtitle;
            }
            $itemObject->Time = $item->post_date;
            $itemObject->Text = $item->truncated_body_text;
            $itemObject->Link = $item->canonical_url;
            $itemObject->Author = $item->publishedBylines[0]->name;
            $items[] = $itemObject;
        }
        return $items;
    }

    public function title() {
        $cacheFile = sprintf($this->_getCachePath() . '.title', $this->_feed);
        if (!file_exists($cacheFile)) {
            $this->_fetchItems();
        }
        if ($this->_feedJson) {
            $title = $this->_feedJson[0]->publishedBylines[0]->publicationUsers[0]->publication->name;
            file_put_contents($cacheFile, $title);
            return $title;
        } else {
            return file_get_contents($cacheFile);
        }
    }

}

?>