Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dev.To Article Publish Scheduler
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);
autorest --csharp \
--namespace="Aliencube.Forem.DevTo" \
--input-file=swagger.json \
--output-folder=output \
--v3
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;
---
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
var segments = markdown.Split(new[] { "---" }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Trim());
var frontmatter = segments.First();
var body = segments.Skip(1);
var fm = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build()
.Deserialize<FrontMatter>(frontmatter);
fm.Published = true;
frontmatter = new SerializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build()
.Serialize(fm);
var sb = new StringBuilder()
.AppendLine("---")
.AppendLine(frontmatter)
.AppendLine("---")
.AppendLine(body);
markdown = sb.ToString();
var updated = new ArticleUpdateArticle() { BodyMarkdown = markdown };
await api.UpdateArticleAsync(articleId, new ArticleUpdate(updated));
[FunctionName(nameof(SchedulingHttpTrigger.SetScheduleAsync))]
public async Task<IActionResult> SetScheduleAsync(
[HttpTrigger(AuthorizationLevel.Function, "POST", Route = "orchestrators/schedules")] HttpRequest req,
[DurableClient] IDurableOrchestrationClient starter,
ILogger log)
{
...
var input = default(SchedulingRequest);
using (var reader = new StreamReader(req.Body))
{
var payload = await reader.ReadToEndAsync();
input = JsonConvert.DeserializeObject<SchedulingRequest>(payload);
}
var instanceId = await starter.StartNewAsync(
orchestratorFunctionName: orchestratorFunctionName,
instanceId: null,
input: input);
return starter.CreateCheckStatusResponse(req, instanceId);
}
{
"previewUri": "https://dev.to/<username>/xxxx-****-temp-slug-xxxx?preview=xxxx",
"schedule": "yyyy-MM-ddTHH:mm:sszzz"
}
[FunctionName(nameof(SchedulingOrchestrationTrigger.SetScheduleOrchestrationAsync))]
public async Task<SchedulingResponse> SetScheduleOrchestrationAsync(
[OrchestrationTrigger] IDurableOrchestrationContext orchestration,
ILogger log)
{
...
var input = orchestration.GetInput<SchedulingRequest>();
var scheduled = input.Schedule.UtcDateTime;
await orchestration.CreateTimer(scheduled, CancellationToken.None);
var activityFunctionName = nameof(SchedulingActivityTrigger.PublishArticleAsync);
var output = await orchestration.CallActivityAsync<SchedulingResponse>(functionName: activityFunctionName, input: input);
[FunctionName(nameof(SchedulingActivityTrigger.PublishArticleAsync))]
public async Task<SchedulingResponse> PublishArticleAsync(
[ActivityTrigger] SchedulingRequest input,
ILogger log)
{
...
// Web page scraping to get article ID
...
// DevTo API call to get markdown document
...
// Frontmatter update
...
// DevTo API call to update markdown document
...
// article ID 가지고 오는 웹 페이지 스크래핑 로직
...
// 마크다운 블로그 포스트를 가져오는 Dev.To API 호출
...
// 프론트매터 업데이트
...
// 마크다운 블로그 포스트를 업데이트하는 Dev.To API 호출
...
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