Skip to content

Commit

Permalink
Rework and improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Sep 1, 2023
1 parent cdb3908 commit 92a3df2
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions SoundCloudExplode/Search/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
if (limit is < Constants.MinLimit or > Constants.MaxLimit)
throw new SoundcloudExplodeException($"Limit must be between {Constants.MinLimit} and {Constants.MaxLimit}");

var encounteredUrls = new HashSet<string>(StringComparer.Ordinal);
var continuationOffset = offset;

var results = new List<ISearchResult>();
Expand All @@ -69,6 +70,8 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(

while(true)
{
results.Clear();

// Any result
var url = $"https://api-v2.soundcloud.com/search{searchFilterStr}?q={Uri.EscapeDataString(searchQuery)}&client_id={_endpoint.ClientId}&limit={limit}&offset={continuationOffset}";

Expand All @@ -82,6 +85,10 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
if (permalinkUrl is null || !Uri.IsWellFormedUriString(permalinkUrl, UriKind.Absolute))
continue;

// Don't yield the same result twice
if (!encounteredUrls.Add(permalinkUrl))
continue;

var permalinkUri = new Uri(permalinkUrl);

// User result
Expand Down Expand Up @@ -109,6 +116,9 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
}
}

if (results.Count == 0)
break;

yield return Batch.Create(results);

continuationOffset += results.Count;
Expand All @@ -125,6 +135,14 @@ public IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
CancellationToken cancellationToken = default) =>
GetResultBatchesAsync(searchQuery, SearchFilter.None, offset, limit, cancellationToken);

/// <summary>
/// Enumerates batches of search results returned by the specified query.
/// </summary>
public IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
string searchQuery,
CancellationToken cancellationToken) =>
GetResultBatchesAsync(searchQuery, SearchFilter.None, cancellationToken: cancellationToken);

/// <summary>
/// Enumerates search results returned by the specified query.
/// </summary>
Expand All @@ -135,6 +153,14 @@ public IAsyncEnumerable<ISearchResult> GetResultsAsync(
CancellationToken cancellationToken = default) =>
GetResultBatchesAsync(searchQuery, offset, limit, cancellationToken).FlattenAsync();

/// <summary>
/// Enumerates search results returned by the specified query.
/// </summary>
public IAsyncEnumerable<ISearchResult> GetResultsAsync(
string searchQuery,
CancellationToken cancellationToken) =>
GetResultBatchesAsync(searchQuery, cancellationToken).FlattenAsync();

/// <summary>
/// Enumerates playlist search results returned by the specified query.
/// </summary>
Expand All @@ -147,6 +173,16 @@ public IAsyncEnumerable<PlaylistSearchResult> GetPlaylistsAsync(
.FlattenAsync()
.OfTypeAsync<PlaylistSearchResult>();

/// <summary>
/// Enumerates playlist search results returned by the specified query.
/// </summary>
public IAsyncEnumerable<PlaylistSearchResult> GetPlaylistsAsync(
string searchQuery,
CancellationToken cancellationToken) =>
GetResultBatchesAsync(searchQuery, SearchFilter.Playlist, cancellationToken: cancellationToken)
.FlattenAsync()
.OfTypeAsync<PlaylistSearchResult>();

/// <summary>
/// Enumerates playlists without albums search results returned by the specified query.
/// </summary>
Expand All @@ -159,6 +195,16 @@ public IAsyncEnumerable<PlaylistSearchResult> GetPlaylistsWithoutAlbumsAsync(
.FlattenAsync()
.OfTypeAsync<PlaylistSearchResult>();

/// <summary>
/// Enumerates playlists without albums search results returned by the specified query.
/// </summary>
public IAsyncEnumerable<PlaylistSearchResult> GetPlaylistsWithoutAlbumsAsync(
string searchQuery,
CancellationToken cancellationToken) =>
GetResultBatchesAsync(searchQuery, SearchFilter.PlaylistWithoutAlbums, cancellationToken: cancellationToken)
.FlattenAsync()
.OfTypeAsync<PlaylistSearchResult>();

/// <summary>
/// Enumerates track search results returned by the specified query.
/// </summary>
Expand All @@ -171,6 +217,16 @@ public IAsyncEnumerable<TrackSearchResult> GetTracksAsync(
.FlattenAsync()
.OfTypeAsync<TrackSearchResult>();

/// <summary>
/// Enumerates track search results returned by the specified query.
/// </summary>
public IAsyncEnumerable<TrackSearchResult> GetTracksAsync(
string searchQuery,
CancellationToken cancellationToken) =>
GetResultBatchesAsync(searchQuery, SearchFilter.Track, cancellationToken: cancellationToken)
.FlattenAsync()
.OfTypeAsync<TrackSearchResult>();

/// <summary>
/// Enumerates user search results returned by the specified query.
/// </summary>
Expand All @@ -183,6 +239,16 @@ public IAsyncEnumerable<UserSearchResult> GetUsersAsync(
.FlattenAsync()
.OfTypeAsync<UserSearchResult>();

/// <summary>
/// Enumerates user search results returned by the specified query.
/// </summary>
public IAsyncEnumerable<UserSearchResult> GetUsersAsync(
string searchQuery,
CancellationToken cancellationToken) =>
GetResultBatchesAsync(searchQuery, SearchFilter.User, cancellationToken: cancellationToken)
.FlattenAsync()
.OfTypeAsync<UserSearchResult>();

/// <summary>
/// Enumerates album search results returned by the specified query.
/// </summary>
Expand All @@ -194,4 +260,14 @@ public IAsyncEnumerable<PlaylistSearchResult> GetAlbumsAsync(
GetResultBatchesAsync(searchQuery, SearchFilter.Album, offset, limit, cancellationToken)
.FlattenAsync()
.OfTypeAsync<PlaylistSearchResult>();

/// <summary>
/// Enumerates album search results returned by the specified query.
/// </summary>
public IAsyncEnumerable<PlaylistSearchResult> GetAlbumsAsync(
string searchQuery,
CancellationToken cancellationToken) =>
GetResultBatchesAsync(searchQuery, SearchFilter.Album, cancellationToken: cancellationToken)
.FlattenAsync()
.OfTypeAsync<PlaylistSearchResult>();
}

0 comments on commit 92a3df2

Please sign in to comment.