Skip to content

Commit

Permalink
Load content file from public HTTP endpoint (#198)
Browse files Browse the repository at this point in the history
* Add support for reading the content-export from a public (unauthenticated) HTTP endpoint
  • Loading branch information
HowardvanRooijen authored Nov 3, 2023
1 parent cb4e3df commit 675c882
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Solutions/Stacker.Cli/Commands/FacebookBufferCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public override async Task<int> ExecuteAsync([NotNull] CommandContext context, [
{
await this.contentTasks.BufferContentItemsAsync<FacebookFormatter>(
settings.ContentFilePath,
settings.ContentUri,
this.profilePrefix,
settings.ProfileName,
settings.PublicationPeriod,
Expand All @@ -54,6 +55,10 @@ public class Settings : CommandSettings
[Description("Content file path.")]
public FilePath ContentFilePath { get; init; }

[CommandOption("-h|--content-http-uri")]
[Description("Content http uri.")]
public Uri ContentUri { get; init; }

[CommandOption("-n|--profile-name")]
[Description("Facebook profile to Buffer.")]
public string ProfileName { get; init; }
Expand Down
5 changes: 5 additions & 0 deletions Solutions/Stacker.Cli/Commands/LinkedInBufferCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public override async Task<int> ExecuteAsync([NotNull] CommandContext context, [
{
await this.contentTasks.BufferContentItemsAsync<FacebookFormatter>(
settings.ContentFilePath,
settings.ContentUri,
this.profilePrefix,
settings.ProfileName,
settings.PublicationPeriod,
Expand All @@ -55,6 +56,10 @@ public class Settings : CommandSettings
[Description("Content file path.")]
public FilePath ContentFilePath { get; init; }

[CommandOption("-h|--content-http-uri")]
[Description("Content http uri.")]
public Uri ContentUri { get; init; }

[CommandOption("-n|--profile-name")]
[Description("LinkedIn profile to Buffer.")]
public string ProfileName { get; init; }
Expand Down
5 changes: 5 additions & 0 deletions Solutions/Stacker.Cli/Commands/TwitterBufferCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public override async Task<int> ExecuteAsync([NotNull] CommandContext context, [
{
await this.contentTasks.BufferContentItemsAsync<TweetFormatter>(
settings.ContentFilePath,
settings.ContentUri,
this.profilePrefix,
settings.ProfileName,
settings.PublicationPeriod,
Expand All @@ -55,6 +56,10 @@ public class Settings : CommandSettings
[Description("Content file path.")]
public FilePath ContentFilePath { get; init; }

[CommandOption("-h|--content-http-uri")]
[Description("Content http uri.")]
public Uri ContentUri { get; init; }

[CommandOption("-n|--profile-name")]
[Description("Twitter profile to Buffer.")]
public string ProfileName { get; init; }
Expand Down
2 changes: 2 additions & 0 deletions Solutions/Stacker.Cli/Contracts/Tasks/IContentTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface IContentTasks
{
Task BufferContentItemsAsync<TContentFormatter>(
FilePath contentFilePath,
Uri contentUri,
string profilePrefix,
string profileName,
PublicationPeriod publicationPeriod,
Expand All @@ -30,6 +31,7 @@ Task BufferContentItemsAsync<TContentFormatter>(

Task<IEnumerable<ContentItem>> LoadContentItemsAsync(
FilePath contentFilePath,
Uri contentUri,
PublicationPeriod publicationPeriod,
DateTime fromDate,
DateTime toDate,
Expand Down
1 change: 1 addition & 0 deletions Solutions/Stacker.Cli/StackerCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static Task<int> Main(string[] args)
config.AddExample("twitter", "buffer", "-c", """c:\temp\content.json""", "-n", "endjin", "--filter-by-tag", "MicrosoftFabric", "--what-if");
config.AddExample("twitter", "buffer", "-c", """c:\temp\content.json""", "-n", "endjin", "--from-date", "2023/06/01", "--to-date", "2023/06/30");
config.AddExample("twitter", "buffer", "-c", """c:\temp\content.json""", "-n", "endjin", "--filter-by-tag", "PowerBI", "--from-date", "2023/06/01", "--to-date", "2023/06/30");
config.AddExample("twitter", "buffer", "-h", """https://localhost/stacker-export.json""", "-n", "endjin", "--filter-by-tag", "MicrosoftFabric", "--what-if");
config.AddExample("environment", "init");
config.AddExample("wordpress", "export", "markdown", "-w", """C:\temp\wordpress-export.xml""", "-o", """C:\Temp\Blog""");
config.AddExample("wordpress", "export", "universal", "-w", """C:\temp\wordpress-export.xml""", "-o", """C:\Temp\Blog\export.json""");
Expand Down
23 changes: 20 additions & 3 deletions Solutions/Stacker.Cli/Tasks/ContentTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using NodaTime;
Expand All @@ -27,15 +28,18 @@ public class ContentTasks : IContentTasks
{
private readonly IBufferClient bufferClient;
private readonly StackerSettings settings;
private readonly IHttpClientFactory httpClientFactory;

public ContentTasks(IBufferClient bufferClient, StackerSettings settings)
public ContentTasks(IBufferClient bufferClient, StackerSettings settings, IHttpClientFactory httpClientFactory)
{
this.bufferClient = bufferClient;
this.settings = settings;
this.httpClientFactory = httpClientFactory;
}

public async Task BufferContentItemsAsync<TContentFormatter>(
FilePath contentFilePath,
Uri contentUri,
string profilePrefix,
string profileName,
PublicationPeriod publicationPeriod,
Expand All @@ -55,7 +59,7 @@ public async Task BufferContentItemsAsync<TContentFormatter>(
AnsiConsole.MarkupLineInterpolated($"[yellow1]Buffer Profile:[/] {profileKey} = {profile}");
AnsiConsole.MarkupLineInterpolated($"[yellow1]Loading:[/] {contentFilePath}");

IEnumerable<ContentItem> contentItems = await this.LoadContentItemsAsync(contentFilePath, publicationPeriod, fromDate, toDate, itemCount, filterByTag).ConfigureAwait(false);
IEnumerable<ContentItem> contentItems = await this.LoadContentItemsAsync(contentFilePath, contentUri, publicationPeriod, fromDate, toDate, itemCount, filterByTag).ConfigureAwait(false);
IEnumerable<string> formattedContentItems = formatter.Format("social", profileName, contentItems, this.settings);

await this.bufferClient.UploadAsync(formattedContentItems, profile, whatIf).ConfigureAwait(false);
Expand All @@ -68,13 +72,26 @@ public async Task BufferContentItemsAsync<TContentFormatter>(

public async Task<IEnumerable<ContentItem>> LoadContentItemsAsync(
FilePath contentFilePath,
Uri contentUri,
PublicationPeriod publicationPeriod,
DateTime fromDate,
DateTime toDate,
int itemCount,
string filterByTag)
{
List<ContentItem> content = JsonSerializer.Deserialize<List<ContentItem>>(await File.ReadAllTextAsync(contentFilePath.FullPath).ConfigureAwait(false));
string fileContent = string.Empty;

if (contentUri is not null || !string.IsNullOrEmpty(contentUri.AbsoluteUri))
{
using HttpClient client = this.httpClientFactory.CreateClient();
fileContent = await client.GetStringAsync(contentUri).ConfigureAwait(false);
}
else if (contentFilePath is not null || !string.IsNullOrEmpty(contentFilePath.FullPath))
{
fileContent = await File.ReadAllTextAsync(contentFilePath.FullPath).ConfigureAwait(false);
}

List<ContentItem> content = JsonSerializer.Deserialize<List<ContentItem>>(fileContent);

if (publicationPeriod != PublicationPeriod.None)
{
Expand Down

0 comments on commit 675c882

Please sign in to comment.