📄
YouTubeSearchProvider.cs
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
using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Json; using System.Runtime.CompilerServices; using System.Threading; using Microsoft.Extensions.Options; using MSearch.Domain; namespace MSearch.SearchProviders.YouTube; internal sealed class YouTubeSearchProvider(IOptions<YouTubeOptions> options, HttpClient httpClient) : ISearchProvider { private readonly string apiKey = options.Value.ApiKey; public async IAsyncEnumerable<SearchResult> Search( SearchQuery query, [EnumeratorCancellation] CancellationToken cancellationToken ) { var response = await httpClient.GetFromJsonAsync( $"https://www.googleapis.com/youtube/v3/search?key={apiKey}&part=snippet&q={Uri.EscapeDataString(query.Term)}", YouTubeJsonSerializerContext.Default.YouTubeSearchResponse, cancellationToken ); if (response is null) { yield break; } foreach (var item in response.Items) { if (Map(item) is { } result) { yield return result; } } } private static SearchResult? Map(YouTubeItem item) => Map(item.Id) is Uri url ? new(item.Snippet.Title, item.Snippet.Description, url) : null; private static Uri? Map(YouTubeId id) => id switch { YouTubeId.Channel(var channelId) => new($"https://www.youtube.com/channel/{channelId}"), YouTubeId.Video(var videoId) => new($"https://www.youtube.com/video/{videoId}"), YouTubeId => null, }; }