-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"sdk": { | ||
"allowPrerelease": false | ||
"version": "8.0.100", | ||
"rollForward": "latestPatch", | ||
"allowPrerelease": true | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
samples/YouTubeSubtitlesExtractor.ConsoleApp/Options/ArgumentOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Aliencube.YouTubeSubtitlesExtractor.ConsoleApp.Options; | ||
|
||
/// <summary> | ||
/// This represents the options entity from the arguments passed. | ||
/// </summary> | ||
public class ArgumentOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets the YouTube video URL. | ||
/// </summary> | ||
public string VideoUrl { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the value indicating whether to display help or not. | ||
/// </summary> | ||
public bool Help { get; set; } | ||
|
||
/// <summary> | ||
/// Parses the arguments and returns the options entity. | ||
/// </summary> | ||
/// <param name="args">List of arguments.</param> | ||
/// <returns>Returns the parsed argument as <see cref="ArgumentOptions"/> instance.</returns> | ||
public static ArgumentOptions Parse(string[] args) | ||
{ | ||
var options = new ArgumentOptions(); | ||
for (var i = 0; i < args.Length; i++) | ||
{ | ||
var arg = args[i]; | ||
switch (arg) | ||
{ | ||
case "-u": | ||
case "--url": | ||
case "--video-url": | ||
options.VideoUrl = i < args.Length - 1 ? args[++i] : string.Empty; | ||
break; | ||
|
||
case "-h": | ||
case "--help": | ||
options.Help = true; | ||
break; | ||
} | ||
} | ||
|
||
return options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Aliencube.YouTubeSubtitlesExtractor; | ||
using Aliencube.YouTubeSubtitlesExtractor.Abstractions; | ||
using Aliencube.YouTubeSubtitlesExtractor.ConsoleApp.Services; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
var host = Host.CreateDefaultBuilder(args) | ||
.UseConsoleLifetime() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddHttpClient(); | ||
services.AddTransient<IYouTubeVideo, YouTubeVideo>(); | ||
services.AddTransient<IYouTubeService, YouTubeService>(); | ||
}) | ||
.Build(); | ||
|
||
var service = host.Services.GetRequiredService<IYouTubeService>(); | ||
await service.ExecuteAsync(args); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Sample Console App – Get YouTube Video Details | ||
|
||
This sample console app shows how to get YouTube video details. | ||
|
||
## Getting Started | ||
|
||
Run the following command to run the console app. | ||
|
||
```bash | ||
dotnet run -- -u [YOUTUBE_VIDEO_URL] | ||
``` |
71 changes: 71 additions & 0 deletions
71
samples/YouTubeSubtitlesExtractor.ConsoleApp/Services/YouTubeService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Aliencube.YouTubeSubtitlesExtractor.Abstractions; | ||
using Aliencube.YouTubeSubtitlesExtractor.ConsoleApp.Options; | ||
using Aliencube.YouTubeSubtitlesExtractor.Models; | ||
|
||
namespace Aliencube.YouTubeSubtitlesExtractor.ConsoleApp.Services; | ||
|
||
/// <summary> | ||
/// This provides interfaces to the <see cref="YouTubeService"/> class. | ||
/// </summary> | ||
public interface IYouTubeService | ||
{ | ||
/// <summary> | ||
/// Executes the service. | ||
/// </summary> | ||
/// <param name="args">List of arguments parsed from the command line.</param> | ||
Task ExecuteAsync(string[] args); | ||
} | ||
|
||
/// <summary> | ||
/// This represents the service entity to extract YouTube video details. | ||
/// </summary> | ||
public class YouTubeService : IYouTubeService | ||
{ | ||
private readonly IYouTubeVideo _video; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="YouTubeService"/> class. | ||
/// </summary> | ||
/// <param name="video"><see cref="IYouTubeVideo"/> instance.</param> | ||
public YouTubeService(IYouTubeVideo video) | ||
{ | ||
this._video = video ?? throw new ArgumentNullException(nameof(video)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task ExecuteAsync(string[] args) | ||
{ | ||
var options = ArgumentOptions.Parse(args); | ||
if (options.Help) | ||
{ | ||
this.DisplayHelp(); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var details = await this._video.ExtractVideoDetailsAsync(options.VideoUrl).ConfigureAwait(false); | ||
this.DisplayDetails(details); | ||
} | ||
catch(Exception ex) | ||
{ | ||
Console.WriteLine("Invalid video URL"); | ||
this.DisplayHelp(); | ||
} | ||
} | ||
|
||
private void DisplayDetails(VideoDetails details) | ||
{ | ||
Console.WriteLine($"Title: {details.Title}"); | ||
Console.WriteLine($"Author: {details.Author}"); | ||
Console.WriteLine($"Description: {details.ShortDescription}"); | ||
Console.WriteLine($"Available Language Code: {details.AvaiableLanguageCodes.Aggregate((a, b) => $"{a}, {b}")}"); | ||
} | ||
|
||
private void DisplayHelp() | ||
{ | ||
Console.WriteLine("Usage:"); | ||
Console.WriteLine(" -u, --url, --video-url <url> YouTube video URL"); | ||
Console.WriteLine(" -h, --help Display help"); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
samples/YouTubeSubtitlesExtractor.ConsoleApp/YouTubeSubtitlesExtractor.ConsoleApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
|
||
<AssemblyName>Aliencube.YouTubeSubtitlesExtractor.ConsoleApp</AssemblyName> | ||
<RootNamespace>Aliencube.YouTubeSubtitlesExtractor.ConsoleApp</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\YouTubeSubtitlesExtractor\YouTubeSubtitlesExtractor.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
2 changes: 1 addition & 1 deletion
2
test/YouTubeSubtitlesExtractorTests/YouTubeSubtitlesExtractorTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters