Skip to content

Commit

Permalink
Housekeeping: applying various RCS/IDE hints
Browse files Browse the repository at this point in the history
  • Loading branch information
ktos committed Oct 25, 2019
1 parent a3feafb commit f60aeb8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
30 changes: 14 additions & 16 deletions src/CoyoteApi/CoyoteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace Eleia.CoyoteApi
/// </summary>
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;

/// <summary>
Expand Down Expand Up @@ -59,7 +59,7 @@ private string BuildUserAgent()
/// <param name="password">Password to be authenticated with</param>
public async Task Login(string username, string password)
{
await GetCsrfToken();
await GetCsrfToken().ConfigureAwait(false);

var data = new FormUrlEncodedContent(new[]
{
Expand All @@ -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);
}

/// <summary>
Expand All @@ -87,7 +86,7 @@ public async Task Login(string username, string password)
/// <param name="comment">Text of the comment</param>
public async Task PostComment(Post post, string comment)
{
await GetCsrfToken();
await GetCsrfToken().ConfigureAwait(false);

var data = new FormUrlEncodedContent(new[]
{
Expand All @@ -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)
{
Expand All @@ -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()
Expand All @@ -135,16 +134,15 @@ private void RemoveXHeaders()
/// </summary>
public async void Logout()
{
await GetCsrfToken();
await GetCsrfToken().ConfigureAwait(false);

var data = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("_token", csrfToken)
});

_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);
}

/// <summary>
Expand All @@ -155,7 +153,7 @@ public async Task<IEnumerable<Post>> 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<PostsApiResult>(json);

Expand Down
3 changes: 2 additions & 1 deletion src/CoyoteApi/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#endregion License

using System;
using System.Collections.Generic;

namespace Eleia.CoyoteApi
{
Expand All @@ -38,7 +39,7 @@ namespace Eleia.CoyoteApi
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class PostsApiResult
{
public Post[] data { get; set; }
public IEnumerable<Post> data { get; set; }
public Links links { get; set; }
public Meta meta { get; set; }
}
Expand Down
7 changes: 2 additions & 5 deletions src/HtmlCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public static string RemoveProperCode(string postText)
/// <returns>Post content without links to post attachments</returns>
public static string RemoveDownloadLinks(string postText)
{
postText = Regex.Replace(postText, "<i class=\"fa fa-download(.|\n)*?</li>", "", RegexOptions.Multiline);
return postText;
return Regex.Replace(postText, "<i class=\"fa fa-download(.|\n)*?</li>", "", RegexOptions.Multiline);
}

/// <summary>
Expand All @@ -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();
}
}
}
12 changes: 7 additions & 5 deletions src/ML/CodeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ namespace Eleia.ML
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class CodeDetector
{
private MLContext mlContext;
private PredictionEngine<CodeDetectorModelInput, CodeDetectorModelOutput> predEngine;
private readonly MLContext mlContext;
private readonly PredictionEngine<CodeDetectorModelInput, CodeDetectorModelOutput> predEngine;

/// <summary>
/// Creates a new instance of CodeDetector, loads ML model into RAM
Expand All @@ -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<CodeDetectorModelInput, CodeDetectorModelOutput>(mlModel);
}

Expand All @@ -61,8 +61,10 @@ public CodeDetector()
/// <returns>Is it a text or a code and with what probability</returns>
public CodeDetectorModelOutput Predict(string text)
{
var input = new CodeDetectorModelInput();
input.Content = text;
var input = new CodeDetectorModelInput
{
Content = text
};

return predEngine.Predict(input);
}
Expand Down
4 changes: 2 additions & 2 deletions src/PostAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace Eleia
/// </summary>
public class PostAnalyzer
{
private float codeDetectorTreshold = 0.99f;
private CodeDetector codeDetector;
private readonly float codeDetectorTreshold = 0.99f;
private readonly CodeDetector codeDetector;

/// <summary>
/// Creates a new instance of PostAnalyzer, loads all detectors used
Expand Down
6 changes: 4 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,18 @@ private static void Main(string[] args)
AnalyzeNewPosts().Wait();
logger.LogDebug("Single run completed");
}
else if(timeBetweenUpdates > 0)
else if (timeBetweenUpdates > 0)
{
while (true)
{
AnalyzeNewPosts().Wait();
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...");
Expand All @@ -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))
Expand Down

0 comments on commit f60aeb8

Please sign in to comment.