/// <summary> /// 控制台模式的主要过程。 /// </summary> /// <param name="args">程序的启动参数。</param> private static void Run(string[] args) { if (ConsoleWindow.IsStarted) { return; } else { ConsoleWindow.IsStarted = true; } var param = new ArgumentReader(args, true, new[] { "-e", "-t", "-o" }, new[] { "-h" }); if (param.GetSwitch("-h")) { ConsoleWindow.ShowHelp(); } else { var pages = new List <YanderePage>(); for (int i = 0; i <= ushort.MaxValue; i++) { var pageLink = param.GetParameter(i); if (pageLink is null) { break; } pageLink = ConsoleWindow.FormatPageLink(pageLink); var page = new YanderePage(pageLink); if (!YanderePage.IsYanderePage(page)) { throw new ArgumentException(new ArgumentException().Message, "PageLink"); } pages.Add(page); } if (pages.Count == 0) { pages.Add(new YanderePage(YanderePage.IndexPageLink)); } int enumCount = int.Parse(param.GetParameter("-e") ?? 0.ToString()); if (!(param.GetParameter("-t") is null)) { int maxThreads = int.Parse(param.GetParameter("-t")); if (maxThreads < 1) { throw new ArgumentOutOfRangeException("-t MaxThreads"); } ThreadPool.SetMaxThreads(maxThreads, maxThreads); } if (!(param.GetParameter("-o") is null)) { ConsoleWindow.OutFile = Path.GetFullPath(param.GetParameter("-o")); if (!Directory.Exists(Path.GetDirectoryName(ConsoleWindow.OutFile))) { throw new DirectoryNotFoundException(); } } foreach (var page in pages) { if (enumCount == 0) { ConsoleWindow.ExtractImageLinksAsync(page); } else { ConsoleWindow.EnumeratePageExtractAsync(page, enumCount); } } while (ConsoleWindow.WorkingThreads != 0) { Thread.Sleep(10); } if (ConsoleWindow.ImageLinks.Count <= ushort.MaxValue) { Clipboard.SetText(string.Join(Environment.NewLine, ConsoleWindow.ImageLinks)); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(Properties.StringResources.ConsoleWindow_Complete); Console.ReadKey(); } }
/// <summary> /// 异步提取页面中包含的图片链接。 /// </summary> /// <param name="page">页面链接提取对象。</param> private static void ExtractImageLinksAsync(YanderePage page) { ThreadPool.QueueUserWorkItem( state => ConsoleWindow.ExtractImageLinks((YanderePage)state), page); }