Last active
January 18, 2021 02:23
-
-
Save justinyoo/e94e83b82d1fc5851935bde157eb7d71 to your computer and use it in GitHub Desktop.
Dev.To Article Publish Scheduler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pattern = "<div.+data-article-id=\"(\\d+)\"\\s*id=\"article-body\">"; | |
var regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); | |
var url = "https://dev.to/<username>/xxxx-****-temp-slug-xxxx?preview=xxxx"; | |
var http = new HttpClient(); | |
var html = await http.GetStringAsync(url); | |
var match = regex.Match(html); | |
var articleId = Convert.ToInt32(match.Groups[1].Value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
autorest --csharp \ | |
--namespace="Aliencube.Forem.DevTo" \ | |
--input-file=swagger.json \ | |
--output-folder=output \ | |
--v3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = new HttpClient(); | |
http.DefaultRequestHeaders.Add("api-key", "<devto_api_key>"); | |
var api = new DEVAPIbeta(http, false); | |
var article = ((await api.GetUserUnpublishedArticlesAsync()) as IEnumerable<ArticleMe>) | |
.SingleOrDefault(p => p.Id == articleId); | |
var markdown = article.BodyMarkdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: xxxxx | |
published: false | |
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/xxxxx.png | |
description: xxxxx | |
tags: xxx, yyy, zzz | |
--- | |
Blog Post Body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var segments = markdown.Split(new[] { "---" }, StringSplitOptions.RemoveEmptyEntries) | |
.Select(p => p.Trim()); | |
var frontmatter = segments.First(); | |
var body = segments.Skip(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fm = new DeserializerBuilder() | |
.WithNamingConvention(UnderscoredNamingConvention.Instance) | |
.Build() | |
.Deserialize<FrontMatter>(frontmatter); | |
fm.Published = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
frontmatter = new SerializerBuilder() | |
.WithNamingConvention(UnderscoredNamingConvention.Instance) | |
.Build() | |
.Serialize(fm); | |
var sb = new StringBuilder() | |
.AppendLine("---") | |
.AppendLine(frontmatter) | |
.AppendLine("---") | |
.AppendLine(body); | |
markdown = sb.ToString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var updated = new ArticleUpdateArticle() { BodyMarkdown = markdown }; | |
await api.UpdateArticleAsync(articleId, new ArticleUpdate(updated)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[FunctionName(nameof(SchedulingHttpTrigger.SetScheduleAsync))] | |
public async Task<IActionResult> SetScheduleAsync( | |
[HttpTrigger(AuthorizationLevel.Function, "POST", Route = "orchestrators/schedules")] HttpRequest req, | |
[DurableClient] IDurableOrchestrationClient starter, | |
ILogger log) | |
{ | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = default(SchedulingRequest); | |
using (var reader = new StreamReader(req.Body)) | |
{ | |
var payload = await reader.ReadToEndAsync(); | |
input = JsonConvert.DeserializeObject<SchedulingRequest>(payload); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var instanceId = await starter.StartNewAsync( | |
orchestratorFunctionName: orchestratorFunctionName, | |
instanceId: null, | |
input: input); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return starter.CreateCheckStatusResponse(req, instanceId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"previewUri": "https://dev.to/<username>/xxxx-****-temp-slug-xxxx?preview=xxxx", | |
"schedule": "yyyy-MM-ddTHH:mm:sszzz" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[FunctionName(nameof(SchedulingOrchestrationTrigger.SetScheduleOrchestrationAsync))] | |
public async Task<SchedulingResponse> SetScheduleOrchestrationAsync( | |
[OrchestrationTrigger] IDurableOrchestrationContext orchestration, | |
ILogger log) | |
{ | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = orchestration.GetInput<SchedulingRequest>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var scheduled = input.Schedule.UtcDateTime; | |
await orchestration.CreateTimer(scheduled, CancellationToken.None); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var activityFunctionName = nameof(SchedulingActivityTrigger.PublishArticleAsync); | |
var output = await orchestration.CallActivityAsync<SchedulingResponse>(functionName: activityFunctionName, input: input); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return output; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[FunctionName(nameof(SchedulingActivityTrigger.PublishArticleAsync))] | |
public async Task<SchedulingResponse> PublishArticleAsync( | |
[ActivityTrigger] SchedulingRequest input, | |
ILogger log) | |
{ | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Web page scraping to get article ID | |
... | |
// DevTo API call to get markdown document | |
... | |
// Frontmatter update | |
... | |
// DevTo API call to update markdown document | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// article ID 가지고 오는 웹 페이지 스크래핑 로직 | |
... | |
// 마크다운 블로그 포스트를 가져오는 Dev.To API 호출 | |
... | |
// 프론트매터 업데이트 | |
... | |
// 마크다운 블로그 포스트를 업데이트하는 Dev.To API 호출 | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var response = new SchedulingResponse() | |
{ | |
Published = result, | |
Message = $"Article published at {input.Schedule:yyyy-MM-ddTHH:mm:sszzzz}" | |
}; | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment