Exemple #1
0
        public async Task <ReturnCodes> RunAsync()
        {
            try
            {
                var accessToken = await _facebookApi.GetAccessTokenAsync(_settings.AppId, _settings.AppSecret);

                if (string.IsNullOrWhiteSpace(accessToken))
                {
                    Console.WriteLine("Error. Access token was not retrieved.");
                    return(ReturnCodes.AccessTokenError);
                }

                Console.WriteLine("Access token retrieved.");

                var pageItems = _fileUtils.LoadCommentsPageInfo(_settings.InputFilePath);
                if (pageItems == null || !pageItems.Any())
                {
                    Console.WriteLine($"No input data retrieved from: {_settings.InputFilePath}");
                    return(ReturnCodes.NoCommentsInfoError);
                }

                foreach (var page in pageItems)
                {
                    var comments = await _facebookApi.GetPageCommentsAsync(accessToken, page.FacebookPageId);

                    if (comments == null || !comments.Any())
                    {
                        Console.WriteLine($"No Facebook comments retrieved.");
                        return(ReturnCodes.FacebookGetCommentsError);
                    }

                    Console.WriteLine($"Page '{page.TargetPageTitle}'");

                    var disqusCommentsXml = _diqusFormatter.ConvertCommentsIntoXml(comments, page.TargetPageTitle, page.TargetPageUrl, page.TargetPageId);
                    if (disqusCommentsXml == null || disqusCommentsXml.Root == null)
                    {
                        Console.WriteLine($"Disqus comments convertion failed.");
                        return(ReturnCodes.DisqusConvertionError);
                    }

                    var filePath = _fileUtils.FormatOutputFilePath(_settings.OutputPath, page.TargetPageTitle);
                    _fileUtils.SaveAsXml(disqusCommentsXml, filePath);
                    Console.WriteLine($"Disqus comments saved into: {filePath}");
                }

                return(ReturnCodes.Success);
            }
            catch (FacebookApiException ex)
            {
                Console.WriteLine($"Facebook API error occurred: {ex.Message}");
                return(ReturnCodes.FacebookGetTokenError);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unexpected error occurred: {ex.Message}");
                return(ReturnCodes.UnexpectedError);
            }
        }
Exemple #2
0
        public async Task RunAsync_ShouldReturnNoCommentsInfoErrorCode_WhenNullReturned()
        {
            // Arrange
            _facebookApi.GetAccessTokenAsync(Arg.Any <string>(), Arg.Any <string>())
            .Returns("token");
            _fileUtils.LoadCommentsPageInfo(Arg.Any <string>())
            .Returns(null as List <CommentsPageInfo>);
            var app = new Startup(_settings, _facebookApi, _disqusFormatter, _fileUtils);

            // Act
            var result = await app.RunAsync();

            // Assert
            result.Should().Be(ReturnCodes.NoCommentsInfoError);
        }