public async Task <WikipediaArticle> FetchArticle(SubmissionRequestDto submissionRequest) { WikipediaArticle wiki = new WikipediaArticle(); // Extract the title to use from the link var uri = new Uri(submissionRequest.Link); var segments = uri.Segments; var title = segments[2]; var detailsResponse = await _client.GetStringAsync($"/w/api.php?action=query&format=json&formatversion=2&prop=pageimages|extracts|pageterms&piprop=original&titles={title}&pilicense=any&exlimit=1&exintro=1"); JObject data = (JObject)JsonConvert.DeserializeObject(detailsResponse); // Fetch the contents from wikimedia API Task <string> wikiDescription = ParseArticleDescription(data); Task <string> wikiImageUrl = ParseArticleImage(data); Task <string> wikiLinkText = ParseArticleLinkText(data); _logger.LogInformation("Building Article"); // Build the article object wiki.Title = title.Replace('_', ' '); wiki.Description = await wikiDescription; wiki.ImageUrl = await wikiImageUrl; var linkText = await wikiLinkText; wiki.LinkText = string.IsNullOrEmpty(linkText) ? $"A wild wiki submitted by {submissionRequest.Username}" : linkText; wiki.Author = submissionRequest.Username; wiki.Link = submissionRequest.Link; wiki.Comments = "### Why it's wild:\n\n" + (string.IsNullOrEmpty(submissionRequest.Comments) ? $"No comments provided." : submissionRequest.Comments); return(wiki); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { _wikipediaClient = new WikipediaClient(log); _githubClient = new GithubClient(log, Environment.GetEnvironmentVariable("GithubToken", EnvironmentVariableTarget.Process)); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); SubmissionRequestDto submissionRequest = JsonConvert.DeserializeObject <SubmissionRequestDto>(requestBody); WikipediaArticle wikipediaArticle = await _wikipediaClient.FetchArticle(submissionRequest); log.LogInformation("Finished Parsing Wikipedia Data"); string details = await _githubClient.UploadNewWikiPost(wikipediaArticle); return(new JsonResult("Success")); }