Exemplo n.º 1
0
 /// <summary>
 /// 异步遍历并提取各个页面的图片链接。
 /// </summary>
 /// <param name="page">页面链接提取对象。</param>
 /// <param name="enumCount">指定遍历的页面数量;
 /// 为 -1 则遍历至最后一页,为 0 则不进行遍历。默认为 0。</param>
 private static void EnumeratePageExtractAsync(YanderePage page, int enumCount = -1)
 {
     enumCount = (enumCount < 0) ?
                 (page.Count - page.Index + 1) :
                 ((enumCount == 0) ? 1 : enumCount);
     for (int i = page.Index; i < page.Index + enumCount; i++)
     {
         ConsoleWindow.ExtractImageLinksAsync(page[i]);
     }
 }
Exemplo n.º 2
0
        /// <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();
            }
        }