Skip to content

Commit

Permalink
Add functional console application that creates Medium Posts
Browse files Browse the repository at this point in the history
These medium posts are created based on values in the .env file or on values found as environment variables. This application still needs heavy refactoring but is functional.
  • Loading branch information
Brend-Smits committed Oct 27, 2021
1 parent d8e8853 commit d8c123b
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
IntegrationToken=something
Title=Very good title
Tags=comma,seperated
CanonicalUrl=
Content=test
ContentFormat=markdown
PublishStatus=Draft
License=all-rights-reserved
PublicationId=123456789
PublicationName=Philips Tech Blog
NotifyFollowers=true
File=
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/PostMediumGitHubAction.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
42 changes: 42 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/PostMediumGitHubAction.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/PostMediumGitHubAction.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/PostMediumGitHubAction.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
28 changes: 28 additions & 0 deletions DotEnv.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace PostMediumGitHubAction
{
using System;
using System.IO;

public static class DotEnv
{
public static void Load(string filePath)
{
if (!File.Exists(filePath))
return;

foreach (var line in File.ReadAllLines(filePath))
{
var parts = line.Split(
'=',
StringSplitOptions.RemoveEmptyEntries);

if (parts.Length != 2)
continue;

Environment.SetEnvironmentVariable(parts[0], parts[1]);
// TODO: Remove Debug console log
// Console.WriteLine("Found environment variable: " + parts[0] + " " + parts[1]);
}
}
}
}
19 changes: 19 additions & 0 deletions MediumError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace PostMediumGitHubAction
{
public class MediumError
{
[JsonPropertyName("message")]
public string Message { get; set; }

[JsonPropertyName("code")]
public int Code { get; set; }
}
public class Root
{
[JsonPropertyName("errors")]
public List<MediumError> Errors { get; set; }
}
}
24 changes: 24 additions & 0 deletions MediumNewPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace PostMediumGitHubAction
{
public class Post
{
[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("tags")]
public string[] Tags { get; set; }

[JsonPropertyName("publishStatus")]
public string PublishStatus { get; set; }

[JsonPropertyName("content")]
public string Content { get; set; }

[JsonPropertyName("contentFormat")]
public string ContentFormat { get; set; }
}
}
30 changes: 30 additions & 0 deletions MediumPublication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace PostMediumGitHubAction
{
public class Publication
{
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("url")]
public string Url { get; set; }

[JsonPropertyName("imageUrl")]
public string ImageUrl { get; set; }
}

public class MediumPublication
{
[JsonPropertyName("data")]
public List<Publication> Publications { get; set; }
}
}
108 changes: 108 additions & 0 deletions MediumService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System.IO;

namespace PostMediumGitHubAction
{
public class MediumService
{
public async Task SubmitNewContentAsync()
{
if (string.IsNullOrEmpty(Program.settings.IntegrationToken))
{
throw new ArgumentNullException("IntegrationToken");
}
MediumUser user = await GetCurrentMediumUserAsync();
Publication pub = await FindMatchingPublicationAsync(user.Data.Id, Program.settings.PublicationName, Program.settings.PublicationId);
if (pub == null)
{
throw new Exception("Could not find publication, did you enter the correct name or id?");
}
if (!string.IsNullOrEmpty(Program.settings.File))
{
Program.settings.Content = await ReadFileFromPath(Program.settings.File);
}
await CreateNewPostUnderPublicationAsync(user.Data.Id, pub.Id);
}

// Retrieves current authenticated user
public async Task<MediumUser> GetCurrentMediumUserAsync()
{
HttpResponseMessage response = await Program.Client.GetAsync("me").ConfigureAwait(false);
response.EnsureSuccessStatusCode();

MediumUser mediumUser = JsonSerializer.Deserialize<MediumUser>(await response.Content.ReadAsByteArrayAsync());
return mediumUser;
}

/// <summary>
/// Retrieve matching publication
/// </summary>
/// <param name="mediumUserId">Id of the Medium User</param>
/// <param name="publicationToLookFor">Name of the Publication to look for</param>
/// <returns></returns>
public async Task<Publication> FindMatchingPublicationAsync(string mediumUserId, string publicationToLookFor, int? publicationId)
{
HttpResponseMessage response = await Program.Client.GetAsync($"users/{mediumUserId}/publications").ConfigureAwait(false);
response.EnsureSuccessStatusCode();

MediumPublication mediumPublications = JsonSerializer.Deserialize<MediumPublication>
(await response.Content.ReadAsByteArrayAsync());
foreach (Publication pub in mediumPublications.Publications)
{
if (!string.IsNullOrEmpty(publicationToLookFor) && pub.Name == publicationToLookFor)
{
return pub;
}
if (publicationId != null && Convert.ToInt32(pub.Id) == publicationId)
{
return pub;
}
}
return null;
}

/// <summary>
/// Create a new post under a publication
/// </summary>
/// <param name="mediumUserId">The id of the user</param>
/// <param name="publicationId">The id of the publication</param>
/// <returns></returns>
public async Task CreateNewPostUnderPublicationAsync(string mediumUserId, string publicationId)
{
Post post = new Post();
post.Content = Program.settings.Content;
post.ContentFormat = Program.settings.ContentFormat;
post.PublishStatus = Program.settings.PublishStatus;
post.Tags = Program.settings.Tags;
post.Title = Program.settings.Title;
HttpResponseMessage response = await Program.Client.
PostAsync($"publications/{publicationId}/posts",
new StringContent(JsonSerializer.Serialize(post), System.Text.Encoding.UTF8, "application/json"))
.ConfigureAwait(false);
try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException)
{
Root error = JsonSerializer.Deserialize<Root>(await response.Content.ReadAsStringAsync());
throw new Exception("Something went wrong when posting: " + error.Errors[0].Message);
}
// TODO: Create new object and return it
}
/// <summary>
/// Reads a file from path and returns result
/// </summary>
/// <param name="filePath">Path of the file</param>
/// <returns></returns>
public async Task<string> ReadFileFromPath(string filePath)
{
string result = await File.ReadAllTextAsync(filePath);
// TODO: Parse any urls found in the content and do something with it like migrating URLS or Uploading images
return result;
}
}
}
29 changes: 29 additions & 0 deletions MediumUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

using System.Text.Json.Serialization;

namespace PostMediumGitHubAction
{
public class Data
{
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("username")]
public string Username { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("url")]
public string Url { get; set; }

[JsonPropertyName("imageUrl")]
public string ImageUrl { get; set; }
}

public class MediumUser
{
[JsonPropertyName("data")]
public Data Data { get; set; }
}
}
9 changes: 9 additions & 0 deletions PostMediumGitHubAction.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>post_to_medium_action</RootNamespace>
</PropertyGroup>

</Project>
45 changes: 45 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace PostMediumGitHubAction
{
class Program
{
public static Settings settings = new Settings();
public static HttpClient Client;
static async Task Main(string[] args)
{
// Load .env file if present
DotEnv.Load(Path.Combine(Directory.GetCurrentDirectory(), ".env"));
ConfigureApplication();
MediumService mediumService = new MediumService();
await mediumService.SubmitNewContentAsync();
}

private static void ConfigureApplication() {
settings.File = Environment.GetEnvironmentVariable(nameof(settings.File));
settings.Content = Environment.GetEnvironmentVariable(nameof(settings.Content));
settings.ContentFormat = Environment.GetEnvironmentVariable(nameof(settings.ContentFormat));
settings.CanonicalUrl = Environment.GetEnvironmentVariable(nameof(settings.CanonicalUrl));
settings.IntegrationToken = Environment.GetEnvironmentVariable(nameof(settings.IntegrationToken));
settings.License = Environment.GetEnvironmentVariable(nameof(settings.License).ToLower());
settings.NotifyFollowers = Convert.ToBoolean(Environment.GetEnvironmentVariable(nameof(settings.NotifyFollowers)).ToLower());
settings.PublicationId = Convert.ToInt32(Environment.GetEnvironmentVariable(nameof(settings.PublicationId)));
settings.PublicationName = Environment.GetEnvironmentVariable(nameof(settings.PublicationName));
settings.PublishStatus = Environment.GetEnvironmentVariable(nameof(settings.PublishStatus)).ToLower();
settings.Tags = Environment.GetEnvironmentVariable(nameof(settings.Tags)).Split(',');
settings.Title = Environment.GetEnvironmentVariable(nameof(settings.Title));
Client = new HttpClient();
Client.BaseAddress = new Uri("https://api.medium.com/v1/");
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Client.DefaultRequestHeaders.Add("Authorization", "Bearer " + settings.IntegrationToken);
}
// TODO: Create method that retrieves the current user.
// TODO: Create method that creates a new post.
// TODO: Create method that reads HTML from a file.
// TODO: Create method that reads Markdown from a file.
}
}
Loading

0 comments on commit d8c123b

Please sign in to comment.