/// <summary> /// Receives file and writes it to a JSON file /// </summary> /// <param name="url"></param> public void WriteToJSON(string file) { const string fileName = @"\CheckLinkCLI2JsonOutput.json"; WebLinkChecker lc = new WebLinkChecker(); var link = lc.GetLinkDetails(file); var jsonToWrite = JsonConvert.SerializeObject(link, Formatting.Indented); string path = Directory.GetCurrentDirectory() + fileName; using (var writer = new StreamWriter(path)) { writer.Write(jsonToWrite); } }
public static void Main(string[] args) { FileReader FileReader = new FileReader(); WebLinkChecker LinkChecker = new WebLinkChecker(); #region Dev env if (IsDebug()) { //TODO: Search function that checks only one or few links on-demand foreach (var input in args) { if (!File.Exists(input) && input.Contains(':') && !(input.EndsWith(".txt") || input.EndsWith(".html")) && input.StartsWith("http")) { LinkChecker.GetAllEndPointWithUri(input); Console.WriteLine("\n"); } else if (File.Exists(input)) { var links = FileReader.ExtractLinks(htmlFile); foreach (var link in links) { LinkChecker.GetAllEndPointWithUri(link); } } else { Console.WriteLine($"File {input} does not exist"); } } } #endregion else { if (args.Length == 0) { Console.WriteLine("Please provide file name with links as an argument..."); Console.WriteLine("For example: CheckLinksCLI2 file_name.txt"); } #region Command line options else if (version.Contains(args[0])) { Console.WriteLine("Application Name: CheckLinkCLI2 \n" + "Release: 0.1"); } #endregion else { foreach (var file in args) { if (File.Exists(file) && !(file.EndsWith(".txt") || file.EndsWith(".html"))) { Console.WriteLine($"Application is unable to parse {file} at this moment\n" + "Please lookout for this feature in a future release\n" + "Thank you for using CheckLinkCLI2!"); } else if (!File.Exists(file) && file.Contains(':') && !(file.EndsWith(".txt") || file.EndsWith(".html")) && file.StartsWith("http")) { LinkChecker.GetAllEndPointWithUri(file); Console.WriteLine("\n"); } else { if (File.Exists(file)) { Console.Write("===|Reading file : "); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"{file}|===\n"); Console.ResetColor(); var links = FileReader.ExtractLinks(file); links.Sort(); foreach (var link in links) { LinkChecker.GetAllEndPointWithUri(link); } Console.WriteLine("\n"); } else { Console.WriteLine("No such file or url exist...\n"); } } } Console.WriteLine($"Good links: {WebLinkChecker.goodCounter} | Bad links: {WebLinkChecker.badCounter} | Unknown links: {WebLinkChecker.unknownCounter}"); } } }