Skip to content

Commit

Permalink
Clean up with new C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Jan 11, 2024
1 parent 5603e06 commit bd5e6ae
Show file tree
Hide file tree
Showing 41 changed files with 249 additions and 608 deletions.
9 changes: 2 additions & 7 deletions YoutubeExplode.Converter/ConversionFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ namespace YoutubeExplode.Converter;
/// Encapsulates conversion media format.
/// </summary>
[Obsolete("Use YoutubeExplode.Videos.Streams.Container instead"), ExcludeFromCodeCoverage]
public readonly struct ConversionFormat
public readonly struct ConversionFormat(string name)
{
/// <summary>
/// Format name.
/// </summary>
public string Name { get; }
public string Name { get; } = name;

/// <summary>
/// Whether this format is a known audio-only format.
/// </summary>
public bool IsAudioOnly => new Container(Name).IsAudioOnly();

/// <summary>
/// Initializes an instance of <see cref="ConversionFormat" />.
/// </summary>
public ConversionFormat(string name) => Name = name;

/// <inheritdoc />
public override string ToString() => Name;
}
55 changes: 22 additions & 33 deletions YoutubeExplode.Converter/ConversionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,39 @@ namespace YoutubeExplode.Converter;
/// <summary>
/// Conversion options.
/// </summary>
public class ConversionRequest
public class ConversionRequest(
string ffmpegCliFilePath,
string outputFilePath,
Container container,
ConversionPreset preset
)
{
/// <summary>
/// Initializes an instance of <see cref="ConversionRequest" />.
/// </summary>
[Obsolete("Use the other constructor overload"), ExcludeFromCodeCoverage]
public ConversionRequest(
string ffmpegCliFilePath,
string outputFilePath,
ConversionFormat format,
ConversionPreset preset
)
: this(ffmpegCliFilePath, outputFilePath, new Container(format.Name), preset) { }

/// <summary>
/// Path to the FFmpeg CLI.
/// </summary>
public string FFmpegCliFilePath { get; }
public string FFmpegCliFilePath { get; } = ffmpegCliFilePath;

/// <summary>
/// Output file path.
/// </summary>
public string OutputFilePath { get; }
public string OutputFilePath { get; } = outputFilePath;

/// <summary>
/// Output container.
/// </summary>
public Container Container { get; }
public Container Container { get; } = container;

/// <summary>
/// Output format.
Expand All @@ -33,33 +50,5 @@ public class ConversionRequest
/// <summary>
/// Encoder preset.
/// </summary>
public ConversionPreset Preset { get; }

/// <summary>
/// Initializes an instance of <see cref="ConversionRequest" />.
/// </summary>
public ConversionRequest(
string ffmpegCliFilePath,
string outputFilePath,
Container container,
ConversionPreset preset
)
{
FFmpegCliFilePath = ffmpegCliFilePath;
OutputFilePath = outputFilePath;
Container = container;
Preset = preset;
}

/// <summary>
/// Initializes an instance of <see cref="ConversionRequest" />.
/// </summary>
[Obsolete("Use the other constructor overload"), ExcludeFromCodeCoverage]
public ConversionRequest(
string ffmpegCliFilePath,
string outputFilePath,
ConversionFormat format,
ConversionPreset preset
)
: this(ffmpegCliFilePath, outputFilePath, new Container(format.Name), preset) { }
public ConversionPreset Preset { get; } = preset;
}
13 changes: 3 additions & 10 deletions YoutubeExplode.Converter/ConversionRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ namespace YoutubeExplode.Converter;
/// <summary>
/// Builder for <see cref="ConversionRequest" />.
/// </summary>
public class ConversionRequestBuilder
public class ConversionRequestBuilder(string outputFilePath)
{
private readonly string _outputFilePath;

private string? _ffmpegCliFilePath;
private Container? _container;
private ConversionPreset _preset;

/// <summary>
/// Initializes an instance of <see cref="ConversionRequestBuilder" />.
/// </summary>
public ConversionRequestBuilder(string outputFilePath) => _outputFilePath = outputFilePath;

private Container GetDefaultContainer() =>
new(Path.GetExtension(_outputFilePath).TrimStart('.').NullIfWhiteSpace() ?? "mp4");
new(Path.GetExtension(outputFilePath).TrimStart('.').NullIfWhiteSpace() ?? "mp4");

/// <summary>
/// Sets the path to the FFmpeg CLI.
Expand Down Expand Up @@ -77,7 +70,7 @@ public ConversionRequestBuilder SetPreset(ConversionPreset preset)
public ConversionRequest Build() =>
new(
_ffmpegCliFilePath ?? FFmpeg.GetFilePath(),
_outputFilePath,
outputFilePath,
_container ?? GetDefaultContainer(),
_preset
);
Expand Down
18 changes: 4 additions & 14 deletions YoutubeExplode/Channels/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,19 @@ namespace YoutubeExplode.Channels;
/// <summary>
/// Metadata associated with a YouTube channel.
/// </summary>
public class Channel : IChannel
public class Channel(ChannelId id, string title, IReadOnlyList<Thumbnail> thumbnails) : IChannel
{
/// <inheritdoc />
public ChannelId Id { get; }
public ChannelId Id { get; } = id;

/// <inheritdoc />
public string Url => $"https://www.youtube.com/channel/{Id}";

/// <inheritdoc />
public string Title { get; }
public string Title { get; } = title;

/// <inheritdoc />
public IReadOnlyList<Thumbnail> Thumbnails { get; }

/// <summary>
/// Initializes an instance of <see cref="Channel" />.
/// </summary>
public Channel(ChannelId id, string title, IReadOnlyList<Thumbnail> thumbnails)
{
Id = id;
Title = title;
Thumbnails = thumbnails;
}
public IReadOnlyList<Thumbnail> Thumbnails { get; } = thumbnails;

/// <inheritdoc />
[ExcludeFromCodeCoverage]
Expand Down
16 changes: 3 additions & 13 deletions YoutubeExplode/Channels/ChannelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,9 @@ namespace YoutubeExplode.Channels;
/// <summary>
/// Operations related to YouTube channels.
/// </summary>
public class ChannelClient
public class ChannelClient(HttpClient http)
{
private readonly HttpClient _http;
private readonly ChannelController _controller;

/// <summary>
/// Initializes an instance of <see cref="ChannelClient" />.
/// </summary>
public ChannelClient(HttpClient http)
{
_http = http;
_controller = new ChannelController(http);
}
private readonly ChannelController _controller = new(http);

private Channel Get(ChannelPage channelPage)
{
Expand Down Expand Up @@ -118,6 +108,6 @@ public IAsyncEnumerable<PlaylistVideo> GetUploadsAsync(
{
// Replace 'UC' in the channel ID with 'UU'
var playlistId = "UU" + channelId.Value[2..];
return new PlaylistClient(_http).GetVideosAsync(playlistId, cancellationToken);
return new PlaylistClient(http).GetVideosAsync(playlistId, cancellationToken);
}
}
15 changes: 3 additions & 12 deletions YoutubeExplode/Common/Author.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace YoutubeExplode.Common;
/// <summary>
/// Reference to a channel that owns a specific YouTube video or playlist.
/// </summary>
public class Author
public class Author(ChannelId channelId, string channelTitle)
{
/// <summary>
/// Channel ID.
/// </summary>
public ChannelId ChannelId { get; }
public ChannelId ChannelId { get; } = channelId;

/// <summary>
/// Channel URL.
Expand All @@ -22,21 +22,12 @@ public class Author
/// <summary>
/// Channel title.
/// </summary>
public string ChannelTitle { get; }
public string ChannelTitle { get; } = channelTitle;

/// <inheritdoc cref="ChannelTitle" />
[Obsolete("Use ChannelTitle instead."), ExcludeFromCodeCoverage]
public string Title => ChannelTitle;

/// <summary>
/// Initializes an instance of <see cref="Author" />.
/// </summary>
public Author(ChannelId channelId, string channelTitle)
{
ChannelId = channelId;
ChannelTitle = channelTitle;
}

/// <inheritdoc />
[ExcludeFromCodeCoverage]
public override string ToString() => ChannelTitle;
Expand Down
9 changes: 2 additions & 7 deletions YoutubeExplode/Common/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ namespace YoutubeExplode.Common;
/// <summary>
/// Generic collection of items returned by a single request.
/// </summary>
public class Batch<T>
public class Batch<T>(IReadOnlyList<T> items)
where T : IBatchItem
{
/// <summary>
/// Items included in the batch.
/// </summary>
public IReadOnlyList<T> Items { get; }

/// <summary>
/// Initializes an instance of <see cref="Batch{T}" />.
/// </summary>
public Batch(IReadOnlyList<T> items) => Items = items;
public IReadOnlyList<T> Items { get; } = items;
}

internal static class Batch
Expand Down
15 changes: 3 additions & 12 deletions YoutubeExplode/Common/Resolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,23 @@ namespace YoutubeExplode.Common;
/// <summary>
/// Resolution of an image or a video.
/// </summary>
public readonly partial struct Resolution
public readonly partial struct Resolution(int width, int height)
{
/// <summary>
/// Viewport width, measured in pixels.
/// </summary>
public int Width { get; }
public int Width { get; } = width;

/// <summary>
/// Viewport height, measured in pixels.
/// </summary>
public int Height { get; }
public int Height { get; } = height;

/// <summary>
/// Viewport area (i.e. width multiplied by height).
/// </summary>
public int Area => Width * Height;

/// <summary>
/// Initializes an instance of <see cref="Resolution" />.
/// </summary>
public Resolution(int width, int height)
{
Width = width;
Height = height;
}

/// <inheritdoc />
[ExcludeFromCodeCoverage]
public override string ToString() => $"{Width}x{Height}";
Expand Down
15 changes: 3 additions & 12 deletions YoutubeExplode/Common/Thumbnail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,17 @@ namespace YoutubeExplode.Common;
/// <summary>
/// Thumbnail image.
/// </summary>
public partial class Thumbnail
public partial class Thumbnail(string url, Resolution resolution)
{
/// <summary>
/// Thumbnail URL.
/// </summary>
public string Url { get; }
public string Url { get; } = url;

/// <summary>
/// Thumbnail resolution.
/// </summary>
public Resolution Resolution { get; }

/// <summary>
/// Initializes an instance of <see cref="Thumbnail" />.
/// </summary>
public Thumbnail(string url, Resolution resolution)
{
Url = url;
Resolution = resolution;
}
public Resolution Resolution { get; } = resolution;

/// <inheritdoc />
[ExcludeFromCodeCoverage]
Expand Down
9 changes: 1 addition & 8 deletions YoutubeExplode/Exceptions/PlaylistUnavailableException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@ namespace YoutubeExplode.Exceptions;
/// <summary>
/// Exception thrown when the requested playlist is unavailable.
/// </summary>
public class PlaylistUnavailableException : YoutubeExplodeException
{
/// <summary>
/// Initializes an instance of <see cref="PlaylistUnavailableException" />.
/// </summary>
public PlaylistUnavailableException(string message)
: base(message) { }
}
public class PlaylistUnavailableException(string message) : YoutubeExplodeException(message);
9 changes: 1 addition & 8 deletions YoutubeExplode/Exceptions/RequestLimitExceededException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@ namespace YoutubeExplode.Exceptions;
/// <summary>
/// Exception thrown when YouTube denies a request because the client has exceeded rate limit.
/// </summary>
public class RequestLimitExceededException : YoutubeExplodeException
{
/// <summary>
/// Initializes an instance of <see cref="RequestLimitExceededException" />.
/// </summary>
public RequestLimitExceededException(string message)
: base(message) { }
}
public class RequestLimitExceededException(string message) : YoutubeExplodeException(message);
11 changes: 3 additions & 8 deletions YoutubeExplode/Exceptions/VideoRequiresPurchaseException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ namespace YoutubeExplode.Exceptions;
/// <summary>
/// Exception thrown when the requested video requires purchase.
/// </summary>
public class VideoRequiresPurchaseException : VideoUnplayableException
public class VideoRequiresPurchaseException(string message, VideoId previewVideoId)
: VideoUnplayableException(message)
{
/// <summary>
/// ID of a free preview video which is used as promotion for the original video.
/// </summary>
public VideoId PreviewVideoId { get; }

/// <summary>
/// Initializes an instance of <see cref="VideoRequiresPurchaseException" />
/// </summary>
public VideoRequiresPurchaseException(string message, VideoId previewVideoId)
: base(message) => PreviewVideoId = previewVideoId;
public VideoId PreviewVideoId { get; } = previewVideoId;
}
9 changes: 1 addition & 8 deletions YoutubeExplode/Exceptions/VideoUnavailableException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@ namespace YoutubeExplode.Exceptions;
/// <summary>
/// Exception thrown when the requested video is unavailable.
/// </summary>
public class VideoUnavailableException : VideoUnplayableException
{
/// <summary>
/// Initializes an instance of <see cref="VideoUnavailableException" />.
/// </summary>
public VideoUnavailableException(string message)
: base(message) { }
}
public class VideoUnavailableException(string message) : VideoUnplayableException(message);
9 changes: 1 addition & 8 deletions YoutubeExplode/Exceptions/VideoUnplayableException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@ namespace YoutubeExplode.Exceptions;
/// <summary>
/// Exception thrown when the requested video is unplayable.
/// </summary>
public class VideoUnplayableException : YoutubeExplodeException
{
/// <summary>
/// Initializes an instance of <see cref="VideoUnplayableException" />.
/// </summary>
public VideoUnplayableException(string message)
: base(message) { }
}
public class VideoUnplayableException(string message) : YoutubeExplodeException(message);
Loading

0 comments on commit bd5e6ae

Please sign in to comment.