Skip to content

Commit

Permalink
spouts/YouTube: add support for playlists
Browse files Browse the repository at this point in the history
Fixes: #1254
  • Loading branch information
jtojnar committed Apr 27, 2021
1 parent 4c882da commit 4a65f2a
Show file tree
Hide file tree
Showing 5 changed files with 398 additions and 20 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Added option `reading_speed_wpm` for showing estimated reading time. ([#1232](https://github.com/fossar/selfoss/pull/1232))
- Search query is now part of URL. ([#1216](https://github.com/fossar/selfoss/pull/1216))
- Search will be carried out using regular expressions when the search query is wrapped in forward slashes, e.g. `/regex/`. The expression syntax is database specific. ([#1205](https://github.com/fossar/selfoss/pull/1205))
- YouTube spout now supports following playlists. ([#1260](https://github.com/fossar/selfoss/pull/1260))

### Bug fixes
- Reddit spout allows wider range of URLs, including absolute URLs and searches ([#1033](https://github.com/fossar/selfoss/pull/1033))
Expand Down
2 changes: 1 addition & 1 deletion docs/api-description.json
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@
"type": "string"
},
"channel": {
"description": "Channel in YouTube spout",
"description": "YouTube URL or user name for YouTube spout",
"type": "string"
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/spouts/youtube/youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
*/
class youtube extends \spouts\rss\feed {
/** @var string name of source */
public $name = 'YouTube: channel';
public $name = 'YouTube';

/** @var string description of this source type */
public $description = 'Fetch posts from a YouTube channel.';
public $description = 'Follow videos from a YouTube channel or a playlist.';

/** @var array configurable parameters */
public $params = [
'channel' => [
'title' => 'Channel',
'title' => 'URL or username',
'type' => 'text',
'default' => '',
'required' => true,
Expand All @@ -34,25 +34,25 @@ public function load(array $params) {
}

public function getXmlUrl(array $params) {
$channel = $params['channel'];
if (preg_match('(^https?://www.youtube.com/channel/([a-zA-Z0-9_-]+)$)', $params['channel'], $matched)) {
$channel = $matched[1];
$channel_type = 'channel_id';
} elseif (preg_match('(^https?://www.youtube.com/user/([a-zA-Z0-9_]+)$)', $params['channel'], $matched)) {
$channel = $matched[1];
$channel_type = 'username';
} elseif (preg_match('(^https?://www.youtube.com/([a-zA-Z0-9_]+)$)', $params['channel'], $matched)) {
$channel = $matched[1];
$channel_type = 'username';
$urlOrUsername = $params['channel'];
if (preg_match('(^https?://www.youtube.com/channel/([a-zA-Z0-9_-]+)$)', $urlOrUsername, $matched)) {
$id = $matched[1];
$feed_type = 'channel_id';
} elseif (preg_match('(^https?://www.youtube.com/user/([a-zA-Z0-9_]+)$)', $urlOrUsername, $matched)) {
$id = $matched[1];
$feed_type = 'user';
} elseif (preg_match('(^https?://www.youtube.com/([a-zA-Z0-9_]+)$)', $urlOrUsername, $matched)) {
$id = $matched[1];
$feed_type = 'user';
} elseif (preg_match('(^https?://www.youtube.com/playlist\?list=([a-zA-Z0-9_]+)$)', $urlOrUsername, $matched)) {
$id = $matched[1];
$feed_type = 'playlist_id';
} else {
$channel_type = 'username';
$id = $urlOrUsername;
$feed_type = 'user';
}

if ($channel_type === 'username') {
return 'https://www.youtube.com/feeds/videos.xml?user=' . $channel;
} else {
return 'https://www.youtube.com/feeds/videos.xml?channel_id=' . $channel;
}
return 'https://www.youtube.com/feeds/videos.xml?' . $feed_type . '=' . $id;
}

public function getThumbnail() {
Expand Down
5 changes: 5 additions & 0 deletions tests/Spouts/YouTubeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public function dataProvider(): array {
'feedTitle' => 'Zogg from Betelgeuse',
'firstItemTitle' => 'Earthlings 101 - Channel Ad',
],
[
'url' => 'https://www.youtube.com/playlist?list=PLKhDkilF5o6_pFucn5JHd6xy7muHLK6pS',
'feedTitle' => 'BeeKeeping',
'firstItemTitle' => 'Year of BeeKeeping Episode 15, Finding Queen',
],
];
}
}
Loading

0 comments on commit 4a65f2a

Please sign in to comment.