예제 #1
0
        public static PythonExecutionResult SearchViaScrapping(AirlineSearch filter, ScrappingSearch scrappingSearch)
        {
            PythonExecutionResult result = new PythonExecutionResult();

            try
            {
                int  nbMaxAttempts   = 3;
                int  nbAttempts      = 0;
                bool continueProcess = true;

                while (continueProcess)
                {
                    nbAttempts = nbAttempts + 1;
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " **  START SearchViaScrapping ** : " + nbAttempts);
                    result = Run(filter, scrappingSearch);

                    ProxyItem proxyItemToModify = scrappingSearch.ProxiesList.Find(p => p.Proxy == scrappingSearch.Proxy);
                    scrappingSearch.ProxiesList.Find(p => p.Proxy == scrappingSearch.Proxy).UseNumber = proxyItemToModify.UseNumber + 1;
                    if (!result.Success)
                    {
                        scrappingSearch.ProxiesList.Find(p => p.Proxy == scrappingSearch.Proxy).Failure = proxyItemToModify.Failure + 1;
                        scrappingSearch.Proxy = ProxyHelper.GetBestProxy(scrappingSearch.ProxiesList);
                    }

                    continueProcess = !result.Success && nbAttempts < nbMaxAttempts;
                }
                result.ProxiesList = scrappingSearch.ProxiesList;
            }
            catch (Exception e)
            {
                result.Success = false;
                result.Error   = e.ToString();
            }
            finally
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " **  END SearchViaScrapping **");
            }

            return(result);
        }
예제 #2
0
        public static PythonExecutionResult Run(AirlineSearch filter, ScrappingSearch scrappingSearch)
        {
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***  START Scrapping *** : " + (scrappingSearch?.Provider ?? "") + " | " + (scrappingSearch?.Proxy ?? ""));
            PythonExecutionResult result = new PythonExecutionResult();

            System.Diagnostics.Process cmd = new System.Diagnostics.Process();
            try
            {
                // https://stackoverflow.com/questions/1469764/run-command-prompt-commands


                string args = "\"" + scrappingSearch.Proxy + "\" \"" + scrappingSearch.SearchTripProviderId + "\" \"" + scrappingSearch.Provider + "\" \"" + filter.FromAirportCode + "\" \"" + filter.ToAirportCode + "\" \"" + filter.DirectFlightsOnly.ToString().ToLower() + "\" \"" + filter.FromDate.Value.ToString("dd'/'MM'/'yyyy") + "\"";
                if (filter.Return)
                {
                    args = args + " \"" + filter.ToDate.Value.ToString("dd'/'MM'/'yyyy") + "\"";
                }


                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName    = "cmd.exe";
                startInfo.Arguments   = string.Format("/C {0} {1} {2}", scrappingSearch.PythonPath, scrappingSearch.MainPythonScriptPath, args);

                startInfo.RedirectStandardInput  = true;
                startInfo.RedirectStandardOutput = true;
                cmd.StartInfo.CreateNoWindow     = false;
                startInfo.UseShellExecute        = false;;

                /*
                 * start.UseShellExecute = false;// Do not use OS shell
                 * start.CreateNoWindow = true; // We don't need new window
                 * start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
                 * start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
                 */
                cmd.StartInfo = startInfo;
                cmd.Start();
                string        strResult  = "";
                List <string> resultList = new List <string>();
                while (!cmd.StandardOutput.EndOfStream)
                {
                    strResult = cmd.StandardOutput.ReadLine();
                    resultList.Add(strResult);
                }

                if (!String.IsNullOrWhiteSpace(strResult))
                {
                    if (strResult.StartsWith("OK"))
                    {
                        result.Success = true;
                        if (strResult.Contains("|"))
                        {
                            string FoundTripNumber = strResult.Split('|')[1];
                            if (!String.IsNullOrWhiteSpace(FoundTripNumber))
                            {
                                ;
                            }
                            result.FoundTripsNumber = Convert.ToInt32(FoundTripNumber);
                        }
                        result.Error = null;
                    }
                    else
                    {
                        if (strResult.Contains("|"))
                        {
                            result.Error = strResult.Split('|')[1];
                        }
                        if (result.Error == null || result.Error.ToLower() != PythonError.WebdriverTimeout.ToLower())
                        {
                            foreach (string log in resultList)
                            {
                                Console.WriteLine(log);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                result.Success = false;
                result.Error   = e.ToString();
                FlightsEngine.Utils.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Provider = " + scrappingSearch.Provider + " and Proxy = " + scrappingSearch.Proxy + " and filters = " + filter.ToSpecialString());
            }
            finally
            {
                cmd.StandardInput.WriteLine("exit");
                cmd.WaitForExit();
                cmd.Close();
            }

            if (result.Success)
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***  END Scrapping *** : SUCCESS => " + (String.IsNullOrWhiteSpace(result.Error) ? result.FoundTripsNumber.ToString() : result.Error));
            }
            else
            {
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***  END Scrapping *** : FAILURE => " + (result.Error ?? ""));
            }
            return(result);
        }