summaryrefslogtreecommitdiff
path: root/providers/Youtube.php
blob: b84ef77ed32f3652f8072924a5555243a90ddb7c (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
<?php

namespace Providers;

require_once('Rss.php');
require_once('../lib/php-youtube-api/src/autoload.php');

class Youtube extends Rss {

    const DEFAULT_MIN_DURATION = 61;

    public function __construct($feed, $options=[]) {
        parent::__construct($feed, $options);
        $config = json_decode(file_get_contents('../config/youtube.json'), TRUE);
        $this->_youtube = new \Madcoda\Youtube\Youtube([
            'key' => $config['key']
        ]);
    }

    protected function _getFeedUrl($feed) {
        return 'https://www.youtube.com/feeds/videos.xml?channel_id=' . $feed;
    }

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

    private function _getVideoMetadata($ids) {
        $cacheFile = sprintf($this->_getCachePath() . '.metadata', $this->_feed);
        $cacheData = [];
        if (file_exists($cacheFile)) {
            $cacheData = unserialize(file_get_contents($cacheFile));
        }
        if ($cacheData) {
            $oldIDs = array_keys($cacheData);
        } else {
            $oldIDs = [];
        }
        $newIDs = array_diff($ids, $oldIDs);
        if ($newIDs) {
            $metadata = $this->_youtube->getVideosInfo($newIDs);
            foreach ($metadata as $video) {
                $cacheData[$video->id] = $video;
            }
        }
        file_put_contents($cacheFile, serialize($cacheData));
        return array_filter($cacheData, function($item) use($ids) {
            return in_array($item->id, $ids);
        });
    }

    private function _getVideoDurations($ids) {
        $metadata = $this->_getVideoMetadata($ids);
        $return = [];
        foreach ($ids as $id) {
            $duration = new \DateInterval($metadata[$id]->contentDetails->duration);
            $return[$id] = $duration->h * 3600 + $duration->i * 60 + $duration->s;
        }
        return $return;
    }

    private function _getVideoID($item) {
        return preg_replace('/^yt:video:/', '', $item->ID);
    }

    protected function _filterItemContent($items) {
        $items = parent::_filterItemContent($items);

        if (array_key_exists('duration', $this->_options)) {
            $duration = intval($this->_options['duration']);
            $videoIDs = array_map(array($this, '_getVideoID'), $items);
            $durations = $this->_getVideoDurations($videoIDs);
            $minDuration = self::DEFAULT_MIN_DURATION;
            if (is_numeric($this->_options['duration'])) {
                $minDuration = intval($this->_options['duration']);
            }
            $items = array_filter($items, function($item) use($minDuration, $durations) {
                $videoID = $this->_getVideoID($item);
                return $durations[$videoID] >= $minDuration;
            });
        }

        return $items;
    }

}

?>