private static async Task<PostMetaTagImage> GetBlogPostCardDescription(ILogger log) | |
{ | |
PostMetaTagImage cardDescription = new PostMetaTagImage(); | |
try | |
{ | |
string content = await MakeGitHubAPICall("https://api.github.com/repos/<website-repo>/commits"); | |
List<CommitsData> commits = JsonConvert.DeserializeObject<List<CommitsData>>(content); | |
CommitsData latestCommit = commits.OrderByDescending(x => x.commit.author.date).FirstOrDefault(); | |
//Get Files in latestCommit | |
content = await MakeGitHubAPICall($"https://api.github.com/repos/<website-repo>/commits/{latestCommit.sha}"); | |
CommitResponse commitDetail = JsonConvert.DeserializeObject<CommitResponse>(content); | |
//Iterate files to find aded file | |
commitDetail.files.ForEach(f => | |
{ | |
//If this file is in the contents/static/api/post/ directory and has been added, its a new blog post, | |
//so we have to generate a new card image | |
if(f.status == "added" && f.contents_url.Contains("contents/static/api/post/")) | |
{ | |
//Load the blog post json file that was added | |
content = MakeGitHubAPICall(f.contents_url).Result; | |
ContentResponse postDetails = JsonConvert.DeserializeObject<ContentResponse>(content); | |
//Get post details from Base64 encoded string | |
byte[] data = Convert.FromBase64String(postDetails.content); | |
string decodedString = Encoding.UTF8.GetString(data); | |
//Deserialize string from Base64 | |
BlogPost p = JsonConvert.DeserializeObject<BlogPost>(decodedString); | |
//Set the card description | |
cardDescription.PostTitle = p.results[0].id; | |
cardDescription.PostMetaTagImageDescription = p.results[0].meta.description; | |
} | |
}); | |
} | |
catch(Exception ex) | |
{ | |
log.LogError($"An error occured getting post description for Card from GitHub API: " + ex.Message); | |
} | |
return cardDescription; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment