Skip to content

Commit

Permalink
Adding Environment and User Secrets check validation and error for En…
Browse files Browse the repository at this point in the history
…d User
  • Loading branch information
RogerBarreto committed May 7, 2023
1 parent 4fd9db9 commit ac67c90
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace AI.YouTubeSummarizer;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

public class ConfigurationData
public class ConfigurationSettings
{
public string OPENAI_KEY { get; set; }

Expand Down
22 changes: 22 additions & 0 deletions AI.YouTubeSummarizer/EndException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Runtime.Serialization;

namespace AI.YouTubeSummarizer;
[Serializable]
internal class EndException : Exception
{
public EndException()
{
}

public EndException(string? message) : base(message)
{
}

public EndException(string? message, Exception? innerException) : base(message, innerException)
{
}

protected EndException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
85 changes: 59 additions & 26 deletions AI.YouTubeSummarizer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using System.Text;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
Expand All @@ -9,45 +10,77 @@ internal class Program
{
private static async Task Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;

IConfiguration configuration = ConfigureSettings();
var configData = configuration.Get<ConfigurationData>();

var summarizer = new YouTubeSummarizer(configData);
try
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("======== Welcome to YouTube video Summarizer =========\n");

Console.WriteLine("======== Welcome to YouTube video Summarizer =========\n");
ConfigurationSettings configData = ConfigureSettings();
var summarizer = new YouTubeSummarizer(configData);

summarizer.PromptForLanguages();
do
{
Console.WriteLine("YouTube Video URL: (https://www.youtube.com/watch?v=...)");
var youTubeUrl = Console.ReadLine();

if (string.IsNullOrWhiteSpace(youTubeUrl))
summarizer.PromptForLanguages();
do
{
continue;
}
Console.WriteLine("YouTube Video URL: (https://www.youtube.com/watch?v=...)");
var youTubeUrl = Console.ReadLine();

try
{
await summarizer.SummarizeVideoAsync(youTubeUrl);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
if (string.IsNullOrWhiteSpace(youTubeUrl))
{
continue;
}

try
{
await summarizer.SummarizeVideoAsync(youTubeUrl);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
while (true);
}
catch (EndException ex)
{
Console.WriteLine(ex.Message);
}
while (true);
}

private static IConfiguration ConfigureSettings()
private static ConfigurationSettings ConfigureSettings()
{
var builder = new ConfigurationBuilder()
.AddUserSecrets<ConfigurationData>()
.AddUserSecrets<ConfigurationSettings>()
.AddJsonFile("config.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();

return builder.Build();
var configuration = builder.Build();

var configData = configuration.Get<ConfigurationSettings>();
configData.OPENAI_KEY = GetApiKey(configData);

return configData;
}

private static string GetApiKey(ConfigurationSettings configData)
{
const string openAIKey = "OPENAI_KEY";

var apiKey =
Environment.GetEnvironmentVariable(openAIKey) ??
Environment.GetEnvironmentVariable(openAIKey, EnvironmentVariableTarget.User) ??
Environment.GetEnvironmentVariable(openAIKey, EnvironmentVariableTarget.Machine);

if (string.IsNullOrEmpty(apiKey))
{
apiKey = configData.OPENAI_KEY;
}

if (string.IsNullOrEmpty(apiKey))
{
throw new EndException("⛔ OPENAI_KEY is not set\n\nConfigure it using either Environment Variables or User Secrets.\n\nPlease refer to installation instructions here: https://github.com/RogerBarreto/sk-youtube-summarizer#readme");
}

return apiKey;
}
}
4 changes: 2 additions & 2 deletions AI.YouTubeSummarizer/YouTubeSummarizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace AI.YouTubeSummarizer;

public class YouTubeSummarizer
{
private ConfigurationData _configData;
private ConfigurationSettings _configData;
private Queue<TranscriptItem> _transcriptItems;
private StringBuilder _sb;
private double _lastStartTime;
private TranscriptItem _lastItem;
private string _videoTitle;
private LanguageConfig _selectedLanguage;

public YouTubeSummarizer(ConfigurationData configData)
public YouTubeSummarizer(ConfigurationSettings configData)
{
this._configData = configData;
this._transcriptItems = new Queue<TranscriptItem>();
Expand Down

0 comments on commit ac67c90

Please sign in to comment.