Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-11339: Added youtube no-cookie support #898

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ source_configuration:
providers:
- YouTube
- 'Icareus Suite'
- 'YouTube No-Cookie'
field_map: { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
uuid: fe53d3c6-950e-47c9-90f8-728571b5ba2d
langcode: en
status: true
dependencies: { }
id: youtube_no_cookie
label: 'YouTube No-Cookie'
provider_url: 'https://www.youtube-nocookie.com/'
endpoints:
-
schemes:
- 'https://*.youtube-nocookie.com/watch*'
- 'https://*.youtube-nocookie.com/v/*'
- 'https://*.youtube-nocookie.com/playlist?list=*'
- 'https://youtube-nocookie.com/playlist?list=*'
- 'https://*.youtube-nocookie.com/shorts*'
- 'https://youtube-nocookie.com/shorts*'
- 'https://*.youtube-nocookie.com/embed/*'
- 'https://*.youtube-nocookie.com/live*'
- 'https://youtube-nocookie.com/live*'
url: 'https://www.youtube-nocookie.com/oembed'
discovery: true
formats:
json: true
xml: false
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ function helfi_media_remote_video_install($is_syncing) : void {
}

/**
* UHF-9088 Updated translations for media remote video.
* UHF-11339 Added youtube no-cookie support.
*/
function helfi_media_remote_video_update_9001(): void {
function helfi_media_remote_video_update_9002(): void {
\Drupal::service('helfi_platform_config.config_update_helper')
->update('helfi_media_remote_video');
}
77 changes: 53 additions & 24 deletions modules/helfi_media_remote_video/helfi_media_remote_video.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare(strict_types=1);
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\helfi_media_remote_video\Entity\RemoteVideo;
use Drupal\media\OEmbed\Provider;
use Drupal\media\OEmbed\Resource;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\UrlResolverInterface;
Expand All @@ -20,6 +21,7 @@ use Drupal\media\OEmbed\UrlResolverInterface;
function helfi_media_remote_video_media_source_info_alter(array &$sources): void {
// Add Helsinki-kanava (Icareus Suite) as an available provider.
$sources['oembed:video']['providers'][] = 'Icareus Suite';
$sources['oembed:video']['providers'][] = 'YouTube No-Cookie';
}

/**
Expand Down Expand Up @@ -192,39 +194,48 @@ function _helfi_media_remote_video_remote_video_url_handler(string $video_url):
$provider = $url_resolver->getProviderByUrl($video_url);
$helsinki_kanava_url_pattern = 'https://www.helsinkikanava.fi/*/web/helsinkikanava/player/vod?assetId=*';

// Handle only Helsinki-kanava videos (Icareus Suite).
if ($provider->getName() !== 'Icareus Suite') {
// Handle only Helsinki-kanava videos (Icareus Suite) and Youtube videos.
if ($provider->getName() !== 'Icareus Suite' && $provider->getName() !== 'YouTube') {
return FALSE;
}

// Try to convert the URL if it's of form 'player/event/view'.
if (str_contains($video_url, 'player/event/view')) {
preg_match('/helsinkikanava.fi\/((?i)[a-z]{2})/', $video_url, $language_matches);
// Default to 'fi' if no match.
$lang_code = $language_matches[1] ?? 'fi';
preg_match('/assetId=(\d+)/', $video_url, $asset_id_matches);
$asset_id = $asset_id_matches[1] ?? NULL;

if (empty($asset_id)) {
return FALSE;
if ($provider->getName() === 'YouTube') {
if (str_contains($video_url, 'youtube.com') && !str_contains($video_url, 'no-cookie')) {
$converted_url = str_replace('youtube.com', 'youtube-nocookie.com', $video_url);
}

// Assemble the converted URL.
$url_parts = explode('*', $helsinki_kanava_url_pattern);
$converted_url = $url_parts[0] . $lang_code . $url_parts[1] . $asset_id;
}

// Try to convert the URL if it's of form 'web/helsinkikanava/player/webcast'.
if (str_contains($video_url, '/web/helsinkikanava/player/webcast')) {
preg_match('/assetId=(\d+)/', $video_url, $asset_id_matches);
$asset_id = $asset_id_matches[1];
if ($provider->getName() === 'Icareus Suite') {
// Try to convert the URL if it's of form 'player/event/view'.
if (str_contains($video_url, 'player/event/view')) {
preg_match('/helsinkikanava.fi\/((?i)[a-z]{2})/', $video_url, $language_matches);
// Default to 'fi' if no match.
$lang_code = $language_matches[1] ?? 'fi';
preg_match('/assetId=(\d+)/', $video_url, $asset_id_matches);
$asset_id = $asset_id_matches[1] ?? NULL;

if (empty($asset_id)) {
return FALSE;
}

if (empty($asset_id)) {
throw new Exception('URL is missing asset ID parameter.');
// Assemble the converted URL.
$url_parts = explode('*', $helsinki_kanava_url_pattern);
$converted_url = $url_parts[0] . $lang_code . $url_parts[1] . $asset_id;
}

$url_parts = explode('*', $helsinki_kanava_url_pattern);
$converted_url = $url_parts[0] . 'fi' . $url_parts[1] . $asset_id;
// Try to convert the URL if it's of form
// 'web/helsinkikanava/player/webcast'.
if (str_contains($video_url, '/web/helsinkikanava/player/webcast')) {
preg_match('/assetId=(\d+)/', $video_url, $asset_id_matches);
$asset_id = $asset_id_matches[1];

if (empty($asset_id)) {
throw new Exception('URL is missing asset ID parameter.');
}

$url_parts = explode('*', $helsinki_kanava_url_pattern);
$converted_url = $url_parts[0] . 'fi' . $url_parts[1] . $asset_id;
}
}

// Return the converted URL.
Expand Down Expand Up @@ -254,3 +265,21 @@ function helfi_media_remote_video_entity_bundle_info_alter(array &$bundles): voi
$bundles['media']['remote_video']['class'] = RemoteVideo::class;
}
}

/**
* Alters an oEmbed resource URL before it is fetched.
*
* @param array $parsed_url
* A parsed URL, as returned by \Drupal\Component\Utility\UrlHelper::parse().
* @param \Drupal\media\OEmbed\Provider $provider
* The oEmbed provider for the resource.
*
* @see \Drupal\media\OEmbed\UrlResolverInterface::getResourceUrl()
*/
function helfi_media_remote_video_oembed_resource_url_alter(array &$parsed_url, Provider $provider) {
// Always serve YouTube videos from youtube-nocookie.com.
if ($provider->getName() === 'YouTube') {
$parsed_url['path'] = str_replace('youtube.com', 'youtube-nocookie.com', $parsed_url['path']);
$parsed_url['query']['url'] = str_replace('youtube.com', 'youtube-nocookie.com', $parsed_url['query']['url']);
}
}