Example #1
0
 public void Relative(string parentUri, string uri, string expected)
 {
     var mapped = new UrlToPathMapper(PathMappingRule.Relative, new Uri(parentUri)).GetPath(new Uri(uri));
     Assert.AreEqual(expected, mapped);
 }
Example #2
0
 public void FullPath(string uri, string expected)
 {
     var mapped = new UrlToPathMapper(PathMappingRule.FullPath).GetPath(new Uri(uri));
     Assert.AreEqual(expected, mapped);
 }
Example #3
0
 public void FullWithFileName(string uri, string fileName, string expected)
 {
     var mapped = new UrlToPathMapper(PathMappingRule.Full).GetPath(new Uri(uri), fileName);
     Assert.AreEqual(expected, mapped);
 }
Example #4
0
        private static void Main(ApplicationArguments arguments)
        {
            EnsureConsoleWindowHandle();
            if (arguments.IgnoreCertificateValidation)
                ServicePointManager.ServerCertificateValidationCallback = (x1, x2, x3, x4) => true;

            Console.Title = "LightGet";
            Console.CursorVisible = false;

            var loggerForDownloader = new LoggerForDownloader { Prefix = ConsoleIndent };

            var mapper = new UrlToPathMapper(arguments.OutputPathFormat, arguments.Url);
            var directory = new DirectoryInfo(Directory.GetCurrentDirectory());

            var downloader = new Downloader(loggerForDownloader, (url, fileName) => new FileInfo(Path.Combine(directory.FullName, mapper.GetPath(url, fileName))));
            var extractor = new LinkExtractor();

            var credentials = arguments.User != null ? new NetworkCredential(arguments.User, arguments.Password) : null;
            var visited = new HashSet<Uri>();

            var queue = new Queue<Uri>();
            queue.Enqueue(arguments.Url);

            while (queue.Count > 0) {
                var url = queue.Dequeue();
                ConsoleUI.WriteLine(ConsoleColor.White, url);

                DownloaderResult result;
                try {
                    result = Download(downloader, url, credentials);
                }
                catch (Exception ex) {
                    ConsoleUI.WriteLine(ConsoleColor.Red, ex.Message);
                    continue;
                }
                finally {
                    Console.WriteLine();
                }

                visited.Add(url);
                if (arguments.FollowLinks == LinkFollowingRule.None)
                    break;

                using (var reader = result.File.OpenText()) {
                    var links = extractor.ExtractLinks(result.Url, reader, result.ContentType);
                    foreach (var link in links.Where(l => !visited.Contains(l))) {
                        if (!ShouldFollow(link, arguments))
                            continue;

                        queue.Enqueue(link);
                    }
                }
            }

            Console.CursorVisible = true;
        }