Skip to content

Commit

Permalink
Handle "free with ads movie" videos
Browse files Browse the repository at this point in the history
Closes #728
  • Loading branch information
Tyrrrz committed Aug 18, 2023
1 parent 008ae39 commit 987be5b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
18 changes: 18 additions & 0 deletions YoutubeExplode.Tests/ChannelSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public async Task I_can_get_the_metadata_of_a_channel_by_handle()
channel.Thumbnails.Should().NotBeEmpty();
}

[Theory]
[InlineData(ChannelIds.Normal)]
[InlineData(ChannelIds.Movies)]
public async Task I_can_get_the_metadata_of_any_available_channel(string channelId)
{
// Arrange
var youtube = new YoutubeClient();

// Act
var channel = await youtube.Channels.GetAsync(channelId);

// Assert
channel.Id.Value.Should().Be(channelId);
channel.Url.Should().NotBeNullOrWhiteSpace();
channel.Title.Should().NotBeNullOrWhiteSpace();
channel.Thumbnails.Should().NotBeEmpty();
}

[Fact]
public async Task I_can_get_videos_uploaded_by_a_channel()
{
Expand Down
1 change: 1 addition & 0 deletions YoutubeExplode.Tests/TestData/ChannelIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
internal static class ChannelIds
{
public const string Normal = "UCX6OQ3DkcsbYNE6H8uQQuVA";
public const string Movies = "UCuVPpxrm2VAgpH3Ktln4HXg";
}
22 changes: 20 additions & 2 deletions YoutubeExplode/Channels/ChannelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,26 @@ private Channel Get(ChannelPage channelPage)
/// </summary>
public async ValueTask<Channel> GetAsync(
ChannelId channelId,
CancellationToken cancellationToken = default) =>
Get(await _controller.GetChannelPageAsync(channelId, cancellationToken));
CancellationToken cancellationToken = default)
{
// Special case for the "Movies & TV" channel, which has a custom page
if (channelId == "UCuVPpxrm2VAgpH3Ktln4HXg")
{
return new Channel(
"UCuVPpxrm2VAgpH3Ktln4HXg",
"Movies & TV",
new[]
{
new Thumbnail(
"https://www.gstatic.com/youtube/img/tvfilm/clapperboard_profile.png",
new Resolution(1024, 1024)
)
}
);
}

return Get(await _controller.GetChannelPageAsync(channelId, cancellationToken));
}

/// <summary>
/// Gets the metadata associated with the channel of the specified user.
Expand Down
25 changes: 19 additions & 6 deletions YoutubeExplode/Videos/Streams/StreamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,26 @@ private async IAsyncEnumerable<IStreamInfo> GetStreamInfosAsync(
// Extract streams from the DASH manifest
if (!string.IsNullOrWhiteSpace(playerResponse.DashManifestUrl))
{
var dashManifest = await _controller.GetDashManifestAsync(
playerResponse.DashManifestUrl,
cancellationToken
);
var dashManifest = default(DashManifest?);

await foreach (var streamInfo in GetStreamInfosAsync(dashManifest.Streams, cancellationToken))
yield return streamInfo;
try
{
dashManifest = await _controller.GetDashManifestAsync(
playerResponse.DashManifestUrl,
cancellationToken
);
}
// Some DASH manifest URLs return 404 for whatever reason
// https://github.com/Tyrrrz/YoutubeExplode/issues/728
catch (HttpRequestException)
{
}

if (dashManifest is not null)
{
await foreach (var streamInfo in GetStreamInfosAsync(dashManifest.Streams, cancellationToken))
yield return streamInfo;
}
}
}

Expand Down

0 comments on commit 987be5b

Please sign in to comment.