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
108
109
110
111
112
113
114
|
<?php
namespace Providers;
require_once('Rss.php');
require_once('../lib/php-youtube-api/src/autoload.php');
class Youtube extends Rss {
const DEFAULT_MIN_DURATION = 60;
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';
}
protected function _mapRssItem($item) {
$itemObj = parent::_mapRssItem($item);
if (array_key_exists('descriptionlink', $this->_options)) {
$linkConfig = explode(':', $this->_options['descriptionlink']);
$linkDomain = NULL;
if (!is_numeric($linkConfig[0])) {
$linkDomain = array_shift($linkConfig);
if (!$linkConfig) {
$linkConfig = ['1'];
}
}
$linkIndex = intval($linkConfig[0]) - 1;
$textLinks = [];
if (preg_match_all('#(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-&?=%.]+#', $itemObj->Text, $textLinks)) {
$textLinks = $textLinks[0];
if ($linkDomain) {
$textLinks = array_values(array_filter($textLinks, function($link) use($linkDomain) {
if($linkParts = parse_url($link)) {
return $linkParts['host'] == $linkDomain;
}
}));
}
if (count($textLinks) > $linkIndex) {
$itemObj->Link = $textLinks[$linkIndex];
}
}
}
return $itemObj;
}
private function _getVideoMetadata($ids) {
$cacheFile = sprintf($this->_getCachePath() . '.metadata', $this->_feed);
$cacheData = [];
if (file_exists($cacheFile)) {
$cacheData = unserialize(file_get_contents($cacheFile));
}
$oldIDs = array_keys($cacheData);
$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;
}
}
?>
|