Ejemplo n.º 1
0
        static void Main(string[] args)
        {
#if TEST_ARGS
            args = new string[]
            {
                "https://cn.pornhub.com/album/48071401",
                "-all",
                "-debug",
            };
#endif
            #region Parse Args
            foreach (string arg in args)
            {
                if (arg[0] == '-' || arg[0] == '/')
                {
                    int    index = arg.IndexOf(":");
                    string key   = arg.Substring(1, index == -1 ? arg.Length - 1 : index - 1).ToLower();
                    string value = arg.Substring(index + 1, arg.Length - index - 1);

                    if (Args.ContainsKey(key))
                    {
                        Args.Remove(key);
                    }

                    Args.Add(key, value);
                }
            }

            if (Args.ContainsKey(ARG_LANG))
            {
                try
                {
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Args[ARG_LANG]);
                }
                catch (Exception e)
                {
                    ThrowException(e);
                    Console.WriteLine();
                    return;
                }
            }

            if (args.Length < 1 || Args.ContainsKey(ARG_HELP))
            {
                Console.WriteLine(string.Format("{0}", Properties.Resources.Msg_Help_Use));
                Console.WriteLine();
                Console.WriteLine(string.Format("   -All                {0}", Properties.Resources.Msg_Help_All));
                Console.WriteLine(string.Format("   -Dir:<path>         {0}", Properties.Resources.Msg_Help_DownloadDir));
                Console.WriteLine(string.Format("   -Index:<value>      {0}", Properties.Resources.Msg_Help_StartIndex));
                Console.WriteLine(string.Format("   -Length:<value>     {0}", Properties.Resources.Msg_Help_Length));
                Console.WriteLine(string.Format("   -Debug              {0}", Properties.Resources.Msg_Help_Debug));
                Console.WriteLine(string.Format("   -Info               {0}", Properties.Resources.Msg_Help_DebugInfo));
                Console.WriteLine(string.Format("   -Rename:<pattern>   {0}", Properties.Resources.Msg_Help_Rename));
                Console.WriteLine();
                Console.WriteLine(Properties.Resources.Msg_Help_Rename_Keywords);
                Console.WriteLine();
                Console.WriteLine(Properties.Resources.Msg_Help_DownloadDir_Keywords);
                Console.WriteLine();
                return;
            }

            if (!Args.ContainsKey(ARG_DOWNLOADDIR))
            {
                Args.Add(ARG_DOWNLOADDIR, DefaultDownloadDir);
            }
            if (string.IsNullOrWhiteSpace(Args[ARG_DOWNLOADDIR]))
            {
                Args[ARG_DOWNLOADDIR] = DefaultDownloadDir;
            }

            IsDebug     = Args.ContainsKey(ARG_DEBUG);
            IsDebugInfo = Args.ContainsKey(ARG_DEBUG_INFO);

            if (!LinkInfo.TryParse(args[0], out LinkInfo argLink))
            {
                if (IsDebug)
                {
                    Console.WriteLine(string.Format(Properties.Resources.Msg_InvalidLink, args[0]));
                }
                Console.WriteLine();
                return;
            }
            #endregion

            #region Console Title
            Console.Title = $"{Properties.Resources.Title}: {getArgsString(args)}";
            string getArgsString(string[] _args)
            {
                string result = string.Empty;

                foreach (string _arg in _args)
                {
                    result += $"{_arg} ";
                }
                return(result.Trim());
            }

            #endregion

            try
            {
                if (argLink.Type.ToLower() == "album")
                {
                    Regex loadingRegex = new Regex("<body onload=\"go\\(\\)\">\\s*Loading \\.\\.\\.\\s*</body>");
                    if (Args.ContainsKey(ARG_ALL))
                    {
                        Regex corruptRegex = new Regex("<div class=\"photoBlockBox\">\\s*<p class=\"corruptMessage\">[\\S\\s]*</p>");
                        int   pageIndex    = 1;
                        argLink.Args = $"?page={pageIndex}";
                        Console.WriteLine(string.Format(Properties.Resources.Msg_Album_Download, argLink.FullUrl));
                        string albumHtmlText = GetHtmlText(argLink.FullUrl);

                        while (!corruptRegex.IsMatch(albumHtmlText) || loadingRegex.IsMatch(albumHtmlText))
                        {
                            if (loadingRegex.IsMatch(albumHtmlText))
                            {
                                if (IsDebug)
                                {
                                    Console.ForegroundColor = ConsoleColor.Yellow;
                                    Console.WriteLine($"Regex '{loadingRegex}' is matched. Retrying in 5 seconds.");
                                    Console.ResetColor();
                                }

                                Thread.Sleep(5000);
                                albumHtmlText = GetHtmlText(argLink.FullUrl);
                                continue;
                            }

                            DownloadAlbum(albumHtmlText, argLink, GetDownloadDir(argLink, albumHtmlText, pageIndex.ToString()));
                            pageIndex++;
                            argLink.Args = $"?page={pageIndex}";
                            Console.WriteLine(string.Format(Properties.Resources.Msg_Album_Download, argLink.FullUrl));
                            albumHtmlText = GetHtmlText(argLink.FullUrl);
                        }
                        ConsoleBackLine(); //Empty page.
                    }
                    else
                    {
                        Console.WriteLine(string.Format(Properties.Resources.Msg_Album_Download, argLink.FullUrl));
                        string albumHtmlText = GetHtmlText(argLink.FullUrl);

                        while (loadingRegex.IsMatch(albumHtmlText))
                        {
                            if (IsDebug)
                            {
                                Console.ForegroundColor = ConsoleColor.Yellow;
                                Console.WriteLine($"Regex '{loadingRegex}' is matched. Retrying in 5 seconds.");
                                Console.ResetColor();
                            }

                            Thread.Sleep(5000);
                            albumHtmlText = GetHtmlText(argLink.FullUrl);
                        }

                        DownloadAlbum(albumHtmlText, argLink,
                                      GetDownloadDir(argLink, albumHtmlText, "1"),
                                      Args.ContainsKey(ARG_STARTINDEX) ? Convert.ToInt32(Args[ARG_STARTINDEX]) : 0,
                                      Args.ContainsKey(ARG_LENGTH) ? Convert.ToInt32(Args[ARG_LENGTH]) : 0);
                    }
                }
                else if (argLink.Type.ToLower() == "photo")
                {
                    DownloadPhoto(argLink, GetDownloadDir(argLink, string.Empty));
                }
            }
            catch (Exception e)
            {
                ThrowException(e);
            }

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(Properties.Resources.Msg_AllDone);
            Console.ResetColor();
            Console.WriteLine();
        }