diff --git a/src/CoyoteApi/CoyoteHandler.cs b/src/CoyoteApi/CoyoteHandler.cs index 5ec0d24..6d0e3d7 100644 --- a/src/CoyoteApi/CoyoteHandler.cs +++ b/src/CoyoteApi/CoyoteHandler.cs @@ -17,9 +17,9 @@ namespace Eleia.CoyoteApi /// public class CoyoteHandler { - private HttpClient hc; - private ILogger _logger; - private CookieContainer cookieContainer; + private readonly HttpClient hc; + private readonly ILogger _logger; + private readonly CookieContainer cookieContainer; private string csrfToken; /// @@ -59,7 +59,7 @@ private string BuildUserAgent() /// Password to be authenticated with public async Task Login(string username, string password) { - await GetCsrfToken(); + await GetCsrfToken().ConfigureAwait(false); var data = new FormUrlEncodedContent(new[] { @@ -69,15 +69,14 @@ public async Task Login(string username, string password) }); _logger.LogDebug("Performing login"); - var loginResult = await hc.PostAsync(Endpoints.LoginPage, data); - var loginResultContent = await loginResult.Content.ReadAsStringAsync(); + var loginResult = await hc.PostAsync(Endpoints.LoginPage, data).ConfigureAwait(false); if (!loginResult.IsSuccessStatusCode) { _logger.LogError("Login was not successful. Status code: {0}", loginResult.StatusCode); } - await Task.Delay(Delay); + await Task.Delay(Delay).ConfigureAwait(false); } /// @@ -87,7 +86,7 @@ public async Task Login(string username, string password) /// Text of the comment public async Task PostComment(Post post, string comment) { - await GetCsrfToken(); + await GetCsrfToken().ConfigureAwait(false); var data = new FormUrlEncodedContent(new[] { @@ -98,7 +97,7 @@ public async Task PostComment(Post post, string comment) _logger.LogDebug("Posting comment"); hc.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest"); hc.DefaultRequestHeaders.Add("X-CSRF-TOKEN", csrfToken); - var commentResult = await hc.PostAsync(Endpoints.CommentPage, data); + var commentResult = await hc.PostAsync(Endpoints.CommentPage, data).ConfigureAwait(false); if (!commentResult.IsSuccessStatusCode) { @@ -109,19 +108,19 @@ public async Task PostComment(Post post, string comment) _logger.LogInformation("Posted comment to post {0}", post.id); } - await Task.Delay(Delay); + await Task.Delay(Delay).ConfigureAwait(false); } private async Task GetCsrfToken() { _logger.LogDebug("Getting CSRF token"); RemoveXHeaders(); - var mainPage = await hc.GetAsync(Endpoints.MainPage).Result.Content.ReadAsStringAsync(); + var mainPage = await hc.GetAsync(Endpoints.MainPage).Result.Content.ReadAsStringAsync().ConfigureAwait(false); var hap = new HtmlDocument(); hap.LoadHtml(mainPage); csrfToken = hap.DocumentNode.SelectSingleNode("/html/head/meta[4]").Attributes[1].Value; - await Task.Delay(Delay); + await Task.Delay(Delay).ConfigureAwait(false); } private void RemoveXHeaders() @@ -135,7 +134,7 @@ private void RemoveXHeaders() /// public async void Logout() { - await GetCsrfToken(); + await GetCsrfToken().ConfigureAwait(false); var data = new FormUrlEncodedContent(new[] { @@ -143,8 +142,7 @@ public async void Logout() }); _logger.LogDebug("Performing logout"); - var loginResult = await hc.PostAsync(Endpoints.LogoutPage, data); - var loginResultContent = await loginResult.Content.ReadAsStringAsync(); + await hc.PostAsync(Endpoints.LogoutPage, data).ConfigureAwait(false); } /// @@ -155,7 +153,7 @@ public async Task> GetPosts(int page = 1) { RemoveXHeaders(); _logger.LogDebug("Getting new posts"); - var json = await hc.GetStringAsync($"{Endpoints.PostsApi}?page={page}"); + var json = await hc.GetStringAsync($"{Endpoints.PostsApi}?page={page}").ConfigureAwait(false); var result = JsonSerializer.Deserialize(json); diff --git a/src/CoyoteApi/Post.cs b/src/CoyoteApi/Post.cs index ccdfb27..1588a43 100644 --- a/src/CoyoteApi/Post.cs +++ b/src/CoyoteApi/Post.cs @@ -30,6 +30,7 @@ #endregion License using System; +using System.Collections.Generic; namespace Eleia.CoyoteApi { @@ -38,7 +39,7 @@ namespace Eleia.CoyoteApi [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public class PostsApiResult { - public Post[] data { get; set; } + public IEnumerable data { get; set; } public Links links { get; set; } public Meta meta { get; set; } } diff --git a/src/HtmlCleaner.cs b/src/HtmlCleaner.cs index cc7dbb1..fca1e8f 100644 --- a/src/HtmlCleaner.cs +++ b/src/HtmlCleaner.cs @@ -60,8 +60,7 @@ public static string RemoveProperCode(string postText) /// Post content without links to post attachments public static string RemoveDownloadLinks(string postText) { - postText = Regex.Replace(postText, "", "", RegexOptions.Multiline); - return postText; + return Regex.Replace(postText, "", "", RegexOptions.Multiline); } /// @@ -82,9 +81,7 @@ public static string StripTags(string item) public static string StripWhitespace(string item) { var cleaned = item.Replace("\n", ""); - cleaned = Regex.Replace(cleaned, @"\s+", " ").Trim(); - - return cleaned; + return Regex.Replace(cleaned, @"\s+", " ").Trim(); } } } \ No newline at end of file diff --git a/src/ML/CodeDetector.cs b/src/ML/CodeDetector.cs index 0e8585a..7158368 100644 --- a/src/ML/CodeDetector.cs +++ b/src/ML/CodeDetector.cs @@ -40,8 +40,8 @@ namespace Eleia.ML [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public class CodeDetector { - private MLContext mlContext; - private PredictionEngine predEngine; + private readonly MLContext mlContext; + private readonly PredictionEngine predEngine; /// /// Creates a new instance of CodeDetector, loads ML model into RAM @@ -50,7 +50,7 @@ public CodeDetector() { mlContext = new MLContext(); string modelPath = AppDomain.CurrentDomain.BaseDirectory + "MLModel.zip"; - ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema); + ITransformer mlModel = mlContext.Model.Load(modelPath, out _); predEngine = mlContext.Model.CreatePredictionEngine(mlModel); } @@ -61,8 +61,10 @@ public CodeDetector() /// Is it a text or a code and with what probability public CodeDetectorModelOutput Predict(string text) { - var input = new CodeDetectorModelInput(); - input.Content = text; + var input = new CodeDetectorModelInput + { + Content = text + }; return predEngine.Predict(input); } diff --git a/src/PostAnalyzer.cs b/src/PostAnalyzer.cs index 432bdb9..64710b0 100644 --- a/src/PostAnalyzer.cs +++ b/src/PostAnalyzer.cs @@ -44,8 +44,8 @@ namespace Eleia /// public class PostAnalyzer { - private float codeDetectorTreshold = 0.99f; - private CodeDetector codeDetector; + private readonly float codeDetectorTreshold = 0.99f; + private readonly CodeDetector codeDetector; /// /// Creates a new instance of PostAnalyzer, loads all detectors used diff --git a/src/Program.cs b/src/Program.cs index d41fafa..02bc085 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -108,7 +108,7 @@ private static void Main(string[] args) AnalyzeNewPosts().Wait(); logger.LogDebug("Single run completed"); } - else if(timeBetweenUpdates > 0) + else if (timeBetweenUpdates > 0) { while (true) { @@ -116,9 +116,10 @@ private static void Main(string[] args) logger.LogDebug("Going to sleep for {0} minutes", timeBetweenUpdates); Thread.Sleep(TimeSpan.FromMinutes(timeBetweenUpdates)); } - } + } } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "RCS1090:Call 'ConfigureAwait(false)'.", Justification = "Not a library")] private static async Task AnalyzeNewPosts() { logger.LogDebug("Getting posts..."); @@ -137,6 +138,7 @@ private static bool IgnorePost(Post post) return !Endpoints.IsDebug && post.forum_id != 24; } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "RCS1090:Call 'ConfigureAwait(false)'.", Justification = "Not a library")] private static async Task AnalyzePost(Post post) { if (analyzed.Contains(post.id))