Skip to content

Commit

Permalink
fix folder creation
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinschumacher committed May 27, 2024
1 parent 76ccbbc commit 871c32f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
28 changes: 26 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
using System.Globalization;
using TMDbLib.Client;

const string defaultOutputDirectory = "wwwroot/data/";

CultureInfo.CurrentCulture = new CultureInfo("de-DE", true);
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de-DE", true);

Expand Down Expand Up @@ -46,10 +44,15 @@
builder.Services.AddScoped<JsonDataRenderer>();
var app = builder.Build();

// Create the output directory if needed.

ExecuteScrapingProcess(app.Services);


static void ExecuteScrapingProcess(IServiceProvider serviceProvider)
{
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
var defaultOutputDirectory = CreateOutputDirectory(configuration);
using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<KinohannoverContext>();
context.Database.Migrate();
Expand Down Expand Up @@ -78,3 +81,24 @@ static void ExecuteScrapingProcess(IServiceProvider serviceProvider)
var jsonRenderer = scope.ServiceProvider.GetRequiredService<JsonDataRenderer>();
jsonRenderer.Render(Path.Combine(defaultOutputDirectory, "data.json"));
}


static string CreateOutputDirectory(IConfiguration config)
{
var defaultOutputDirectory = config["DataOutputPath"];
if (string.IsNullOrWhiteSpace(defaultOutputDirectory))
{
throw new InvalidOperationException("No output directory found.");
}
if (!Directory.Exists(defaultOutputDirectory))
{
var info = Directory.CreateDirectory(defaultOutputDirectory);
defaultOutputDirectory = info.FullName;
}
else
{
Path.GetFullPath(defaultOutputDirectory);
}

return defaultOutputDirectory;
}
4 changes: 3 additions & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
},
"ConnectionStrings": {
"kinohannoverContext": "Data Source=wwwroot/data/data.sqlite.db"
}
},

"DataOutputPath": "wwwroot/data/"
}

0 comments on commit 871c32f

Please sign in to comment.