Skip to content

Commit f20ea84

Browse files
committed
chore: (Tube Player) move logic to service
1 parent 9e4d75c commit f20ea84

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

reference/TubePlayer/TubePlayer/Business/IYoutubeService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ namespace TubePlayer.Business;
33
public interface IYoutubeService
44
{
55
Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextPageToken, uint maxResult, CancellationToken ct);
6+
7+
Task<string?> GetVideoSourceUrl(string videoId, CancellationToken ct);
68
}

reference/TubePlayer/TubePlayer/Business/YoutubeService.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ namespace TubePlayer.Business;
33
public class YoutubeService : IYoutubeService
44
{
55
private readonly IYoutubeEndpoint _client;
6+
private readonly IYoutubePlayerEndpoint _playerClient;
67

7-
public YoutubeService(IYoutubeEndpoint client)
8+
public YoutubeService(IYoutubeEndpoint client, IYoutubePlayerEndpoint playerClient)
89
{
910
_client = client;
11+
_playerClient = playerClient;
1012
}
1113

1214
public async Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextPageToken, uint maxResult, CancellationToken ct)
@@ -67,4 +69,39 @@ public async Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextP
6769

6870
return new(videoSet.ToImmutableList(), resultData?.NextPageToken ?? string.Empty);
6971
}
72+
73+
74+
public async Task<string?> GetVideoSourceUrl(string videoId, CancellationToken ct)
75+
{
76+
var streamVideo = $$"""
77+
{
78+
"videoId": "{{videoId}}",
79+
"context": {
80+
"client": {
81+
"clientName": "ANDROID_TESTSUITE",
82+
"clientVersion": "1.9",
83+
"androidSdkVersion": 30,
84+
"hl": "en",
85+
"gl": "US",
86+
"utcOffsetMinutes": 0
87+
}
88+
}
89+
}
90+
""";
91+
92+
// Get the available stream data
93+
var streamData = await _playerClient.GetStreamData(streamVideo, ct);
94+
95+
// Get the video stream with the highest video quality
96+
var streamWithHighestVideoQuality = streamData.Content?.
97+
StreamingData?
98+
.Formats?
99+
.OrderByDescending(s => s.QualityLabel)
100+
.FirstOrDefault();
101+
102+
// Get the stream URL
103+
var streamUrl = streamWithHighestVideoQuality?.Url;
104+
105+
return streamUrl;
106+
}
70107
}

reference/TubePlayer/TubePlayer/Business/YoutubeServiceMock.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public YoutubeServiceMock(ISerializer serializer)
1515
_channels = channelsData.Items!.ToDictionary(channel => channel.Id!, StringComparer.OrdinalIgnoreCase);
1616
}
1717

18+
public Task<string?> GetVideoSourceUrl(string videoId, CancellationToken ct)
19+
{
20+
return Task.FromResult<string?>(null);
21+
}
22+
1823
public Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextPageToken, uint maxResult, CancellationToken ct)
1924
{
2025
var filtered = _details
Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
11
using Windows.Media.Core;
2-
using TubePlayer.Services;
32

43
namespace TubePlayer.Presentation;
54

6-
public partial record VideoDetailsModel(YoutubeVideo Video, IYoutubePlayerEndpoint YoutubeClient)
5+
public partial record VideoDetailsModel(YoutubeVideo Video, IYoutubeService YoutubeService)
76
{
87
public IFeed<MediaSource> VideoSource => Feed.Async(GetVideoSource);
98

109
private async ValueTask<MediaSource> GetVideoSource(CancellationToken ct)
1110
{
12-
var streamVideo = $$"""
13-
{
14-
"videoId": "{{Video.Id}}",
15-
"context": {
16-
"client": {
17-
"clientName": "ANDROID_TESTSUITE",
18-
"clientVersion": "1.9",
19-
"androidSdkVersion": 30,
20-
"hl": "en",
21-
"gl": "US",
22-
"utcOffsetMinutes": 0
23-
}
24-
}
25-
}
26-
""";
27-
28-
// Get the available stream data
29-
var streamData = await YoutubeClient.GetStreamData(streamVideo, ct);
30-
31-
// Get the video stream with the highest video quality
32-
var streamWithHighestVideoQuality = streamData.Content?.StreamingData?.Formats?.OrderByDescending(s => s.QualityLabel).FirstOrDefault() ??
33-
throw new InvalidOperationException("Input stream collection is empty.");
34-
35-
// Get the stream URL
36-
var streamUrl = streamWithHighestVideoQuality.Url;
11+
var streamUrl = await YoutubeService.GetVideoSourceUrl(Video?.Id, ct)
12+
?? throw new InvalidOperationException("Input stream collection is empty.");
3713

3814
// Return the MediaSource using the stream URL
39-
return MediaSource.CreateFromUri(new Uri(streamUrl!));
15+
return MediaSource.CreateFromUri(new Uri(streamUrl));
4016
}
4117
}

0 commit comments

Comments
 (0)