Skip to content

Commit

Permalink
Merge pull request #21 from tdwright/master
Browse files Browse the repository at this point in the history
Added option to print elapsed milliseconds to console at end of process
  • Loading branch information
hmol committed Oct 7, 2016
2 parents 30e6088 + df51a2a commit 4ab27ee
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions LinkCrawler/LinkCrawler/App.config
Expand Up @@ -13,6 +13,7 @@
<add key="SuccessHttpStatusCodes" value="1xx,2xx,3xx" />
<!--explanation of regex below: http://regexr.com/3cqt9 -->
<add key="ValidUrlRegex" value="(^http[s]?:\/{2})|(^www)|(^\/{1,2})" />
<add key="PrintSummary" value="false" />
<add key="Csv.FilePath" value="C:\tmp\output.csv" />
<add key="Csv.Overwrite" value="true" />
<add key="Csv.Delimiter" value=";" />
Expand Down
34 changes: 34 additions & 0 deletions LinkCrawler/LinkCrawler/LinkCrawler.cs
Expand Up @@ -7,6 +7,7 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace LinkCrawler
{
Expand All @@ -19,7 +20,9 @@ public class LinkCrawler
public IValidUrlParser ValidUrlParser { get; set; }
public bool OnlyReportBrokenLinksToOutput { get; set; }
public static List<string> VisitedUrlList { get; set; }
public static List<string> CompletedUrlList { get; set; }
private ISettings _settings;
private Stopwatch timer;

public LinkCrawler(IEnumerable<IOutput> outputs, IValidUrlParser validUrlParser, ISettings settings)
{
Expand All @@ -28,13 +31,17 @@ public LinkCrawler(IEnumerable<IOutput> outputs, IValidUrlParser validUrlParser,
ValidUrlParser = validUrlParser;
CheckImages = settings.CheckImages;
VisitedUrlList = new List<string>();
CompletedUrlList = new List<string>();
RestRequest = new RestRequest(Method.GET).SetHeader("Accept", "*/*");
OnlyReportBrokenLinksToOutput = settings.OnlyReportBrokenLinksToOutput;
_settings = settings;
this.timer = new Stopwatch();
}

public void Start()
{
this.timer.Start();
VisitedUrlList.Add(BaseUrl);
SendRequest(BaseUrl);
}

Expand Down Expand Up @@ -91,6 +98,33 @@ public void WriteOutput(IResponseModel responseModel)
output.WriteInfo(responseModel);
}
}

CheckIfFinal(responseModel);
}

private void CheckIfFinal(IResponseModel responseModel)
{
if (!CompletedUrlList.Contains(responseModel.RequestedUrl))
{
CompletedUrlList.Add(responseModel.RequestedUrl);

if ((CompletedUrlList.Count == VisitedUrlList.Count) && (VisitedUrlList.Count > 1))
FinaliseSession();
}
}

private void FinaliseSession()
{
this.timer.Stop();
if (this._settings.PrintSummary)
{
string message = @"
Processing completed in " + this.timer.ElapsedMilliseconds.ToString() + "ms";
foreach (var output in Outputs)
{
output.WriteInfo(message);
}
}
}
}
}
7 changes: 6 additions & 1 deletion LinkCrawler/LinkCrawler/Utils/Outputs/ConsoleOutput.cs
Expand Up @@ -13,7 +13,12 @@ public void WriteError(IResponseModel responseModel)

public void WriteInfo(IResponseModel responseModel)
{
Console.WriteLine(responseModel.ToString());
WriteInfo(responseModel.ToString());
}

public void WriteInfo(String Info)
{
Console.WriteLine(Info);
}
}
}
5 changes: 5 additions & 0 deletions LinkCrawler/LinkCrawler/Utils/Outputs/CsvOutput.cs
Expand Up @@ -40,6 +40,11 @@ public void WriteInfo(IResponseModel responseModel)
Write(responseModel);
}

public void WriteInfo(String Info)
{
// Do nothing - string info is only for console
}

private void Write(IResponseModel responseModel)
{
_writer?.WriteLine("{1}{0}{2}{0}{3}{0}{4}",
Expand Down
1 change: 1 addition & 0 deletions LinkCrawler/LinkCrawler/Utils/Outputs/IOutput.cs
Expand Up @@ -6,5 +6,6 @@ public interface IOutput
{
void WriteError(IResponseModel responseModel);
void WriteInfo(IResponseModel responseModel);
void WriteInfo(string InfoString);
}
}
5 changes: 5 additions & 0 deletions LinkCrawler/LinkCrawler/Utils/Outputs/SlackOutput.cs
Expand Up @@ -21,5 +21,10 @@ public void WriteInfo(IResponseModel responseModel)
{
// Write nothing to Slack
}

public void WriteInfo(string Info)
{
// Write nothing to Slack
}
}
}
1 change: 1 addition & 0 deletions LinkCrawler/LinkCrawler/Utils/Settings/Constants.cs
Expand Up @@ -17,6 +17,7 @@ public static class AppSettings
public const string CsvDelimiter = "Csv.Delimiter";
public const string SuccessHttpStatusCodes = "SuccessHttpStatusCodes";
public const string OutputProviders = "outputProviders";
public const string PrintSummary = "PrintSummary";
}

public static class Response
Expand Down
2 changes: 2 additions & 0 deletions LinkCrawler/LinkCrawler/Utils/Settings/ISettings.cs
Expand Up @@ -27,5 +27,7 @@ public interface ISettings
string CsvDelimiter { get; }

bool IsSuccess(HttpStatusCode statusCode);

bool PrintSummary { get; }
}
}
3 changes: 3 additions & 0 deletions LinkCrawler/LinkCrawler/Utils/Settings/Settings.cs
Expand Up @@ -39,6 +39,9 @@ public class Settings : ISettings
public string CsvDelimiter =>
ConfigurationManager.AppSettings[Constants.AppSettings.CsvDelimiter];

public bool PrintSummary =>
ConfigurationManager.AppSettings[Constants.AppSettings.PrintSummary].ToBool();

public bool IsSuccess(HttpStatusCode statusCode)
{
var configuredCodes = ConfigurationManager.AppSettings[Constants.AppSettings.SuccessHttpStatusCodes] ?? "";
Expand Down

0 comments on commit 4ab27ee

Please sign in to comment.