Skip to content

Commit

Permalink
Add console app as a sample
Browse files Browse the repository at this point in the history
  • Loading branch information
justinyoo committed Nov 8, 2023
1 parent 84b9040 commit 398fc1d
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ This is the NuGet package library that retrieves subtitles from a given YouTube
List<Subtitle> subtitles = await youtube.ExtractSubtitlesAsync(options);
```

## Sample Console Cpp

You can find a sample console app to extract YouTube video details from [here](./samples/YouTubeSubtitlesExtractor.ConsoleApp). Alternatively, run the following command to run the console app.

```bash
dotnet run --project ./samples/YouTubeSubtitlesExtractor.ConsoleApp -- -u [YOUTUBE_VIDEO_URL]
```

## Issues or Feedbacks

Please leave any issues or feedbacks on the [GitHub Issue page](https://github.com/aliencube/youtube-subtitles-extractor/issues).

## TO-DOs

- [ ] Sample console app
- [ ] devcontainer settings

## Acknowledgments
Expand Down
14 changes: 13 additions & 1 deletion YouTubeSubtitlesExtractor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YouTubeSubtitlesExtractor",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{54EEFCF5-7733-451D-8523-6E20173B51E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YouTubeSubtitlesExtractorTests", "test\YouTubeSubtitlesExtractorTests\YouTubeSubtitlesExtractorTests.csproj", "{9C9459D6-C28C-4599-818D-0135AF450AAE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YouTubeSubtitlesExtractorTests", "test\YouTubeSubtitlesExtractorTests\YouTubeSubtitlesExtractorTests.csproj", "{9C9459D6-C28C-4599-818D-0135AF450AAE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{7B63E2DF-B82E-4C29-9F8D-261BD02BE279}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YouTubeSubtitlesExtractor.ConsoleApp", "samples\YouTubeSubtitlesExtractor.ConsoleApp\YouTubeSubtitlesExtractor.ConsoleApp.csproj", "{E0189070-5D9B-43EB-BB71-2012C421DB80}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -25,12 +29,20 @@ Global
{9C9459D6-C28C-4599-818D-0135AF450AAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C9459D6-C28C-4599-818D-0135AF450AAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C9459D6-C28C-4599-818D-0135AF450AAE}.Release|Any CPU.Build.0 = Release|Any CPU
{E0189070-5D9B-43EB-BB71-2012C421DB80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0189070-5D9B-43EB-BB71-2012C421DB80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0189070-5D9B-43EB-BB71-2012C421DB80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0189070-5D9B-43EB-BB71-2012C421DB80}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DA994943-763E-4946-835F-306EEB8D19F8} = {261C8A2F-343C-4ECE-BE55-B7D25CC1DF04}
{9C9459D6-C28C-4599-818D-0135AF450AAE} = {54EEFCF5-7733-451D-8523-6E20173B51E2}
{E0189070-5D9B-43EB-BB71-2012C421DB80} = {7B63E2DF-B82E-4C29-9F8D-261BD02BE279}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2C208B76-6651-4B0D-A717-AE1CC235DE7A}
EndGlobalSection
EndGlobal
4 changes: 3 additions & 1 deletion global.json
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
}
}
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;
}
}
19 changes: 19 additions & 0 deletions samples/YouTubeSubtitlesExtractor.ConsoleApp/Program.cs
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);
11 changes: 11 additions & 0 deletions samples/YouTubeSubtitlesExtractor.ConsoleApp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Sample Console App &ndash; 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]
```
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");
}
}
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>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand Down

0 comments on commit 398fc1d

Please sign in to comment.