Ejemplo n.º 1
0
        public void validConfigDateTime(TicketOrders config, CrawlerModels.orderPageInfo orderInfo)
        {
            var haveBackDate = haveBackDateFunc(config);
            var haveToDate   = haveToDateFunc(config);

            if (haveToDate)
            {
                var configToDate = validDateFormat(config.targetDate.TripToDate, config.OrderId);
                validPassDate(configToDate, config.OrderId);

                /**
                 * logic:去程到達時間不可晚於回程出發時間或相距過近
                 * 去程日期不可晚於回程日期
                 **/
                var backTripInfo = orderInfo.trips.FirstOrDefault(x => x.tripType == "回程");
                compareTripDateAndConfigDate(backTripInfo, configToDate);
            }


            if (haveBackDate)
            {
                var configBackDate = validDateFormat(config.targetDate.TripBackDate, config.OrderId);
                validPassDate(configBackDate, config.OrderId);
                var toTripInfo = orderInfo.trips.FirstOrDefault(x => x.tripType == "去程");
                compareTripDateAndConfigDate(toTripInfo, configBackDate);
            }
        }
Ejemplo n.º 2
0
        public CrawlerModels.orderPageInfo GetOrderInformation(string html)
        {
            var _parser     = new HtmlParser();
            var result      = new CrawlerModels.orderPageInfo();
            var dom         = _parser.ParseDocument(html);
            var mainContent = dom.GetElementById("content");

            ValidSessionExpire(mainContent);
            CheckPageError(dom);

            var allrow = mainContent.QuerySelectorAll(".table_simple tbody tr");

            foreach (var row in allrow)
            {
                var title = row.QuerySelector(".section_subtitle");
                if (title != null && Regex.IsMatch(title.TextContent, "去程|回程"))
                {
                    var tds      = row.ChildNodes.ToList().Select(x => x.TextContent).Where(x => !string.IsNullOrEmpty(x?.Trim())).ToList();
                    var tripInfo = new CrawlerModels.tripInfo();
                    tripInfo.tripType       = tds[0];
                    tripInfo.date           = tds[1];
                    tripInfo.trainNo        = tds[2];
                    tripInfo.startStation   = tds[3];
                    tripInfo.arrivalStation = tds[4];
                    tripInfo.startTime      = tds[5];
                    tripInfo.arrivalTime    = tds[6];
                    tripInfo.price          = tds[7];
                    tripInfo.seat           = tds[8];
                    result.trips.Add(tripInfo);
                }
                ;
            }
            result.isRoundTrip    = result.trips.Any(x => x.tripType == "回程");
            result.isTripEditable = dom.QuerySelector("input[value='變更行程']") != null;
            var form = dom.QuerySelector("form");

            result.nextStepUrl = form.GetAttribute("action");
            var paymentRow    = dom.QuerySelector(".table_details  tr  td:contains('交易狀態')");
            var paymentDetail = paymentRow.NextElementSibling.QuerySelectorAll("span > span").Select(x => x.TextContent);

            result.isAlreadPaid = paymentDetail.Any(x => x == "已付款");
            if (!result.isAlreadPaid)
            {
                result.paymentDeadLine = paymentDetail.FirstOrDefault(x => Regex.IsMatch(x, @"\d+\/\d+"));
            }

            return(result);
        }
Ejemplo n.º 3
0
        public CrawlerModels.orderPageInfo post_search_order_form(string IdCard, string OrderId)
        {
            CrawlerModels.orderPageInfo orderInfo = null;
            try
            {
                var orderPageHtml  = _requestClient.GoTo_search_order_page();
                var searchOrderUrl = _htmlParser.GetNextStepUrl(orderPageHtml);
                var html           = _requestClient.post_search_order_form(IdCard, OrderId, searchOrderUrl);
                orderInfo = _htmlParser.GetOrderInformation(html);
            }
            catch (CritialPageErrorException ex)
            {
                throw new CritialPageErrorException($"身份證字號:{IdCard}\r\n訂位代號:{OrderId}\r\n {ex.Message}");
            }

            return(orderInfo);
        }
Ejemplo n.º 4
0
        public CrawlerModels.modifyResult ModifyTrip(CrawlerModels.ModifyTripType tripType, TicketOrders config, CrawlerModels.orderPageInfo orderInfo)
        {
            CrawlerModels.modifyResult result = CrawlerModels.modifyResult.fail;

            try
            {
                var modifyTripPageHtml = _requestClient.GoTo_modifyTrip_form(orderInfo.nextStepUrl);
                //判斷去/回程是否可更改
                var OrderStatus = _htmlParser.CheckModifyFormIsAvailable(modifyTripPageHtml);
                _validation.validOrderStatus(tripType, OrderStatus);
                var searchTripUrl = _htmlParser.GetNextStepUrl(modifyTripPageHtml);

                var formatDate     = _config.GetValidOrderDate(config, tripType, orderInfo);
                var html           = _requestClient.post_search_trip_form(tripType, searchTripUrl, formatDate);
                var modifyOrderUrl = _htmlParser.GetNextStepUrl(html);
                var trips          = _htmlParser.GetTripsPerPageAndHandleError(html, tripType);
                if (trips != null && trips.Count() >= 0)
                {
                    var tripInfo = new CrawlerModels.tripInfo();
                    if (tripType == CrawlerModels.ModifyTripType.To)
                    {
                        tripInfo = orderInfo.trips.FirstOrDefault(x => x.tripType == "去程");
                    }
                    else
                    {
                        tripInfo = orderInfo.trips.FirstOrDefault(x => x.tripType == "回程");
                    }

                    var matchTrip = _tripCompare.FindMatchTrip(trips.ToList(), tripInfo, formatDate.tripDateTime);
                    if (!string.IsNullOrEmpty(matchTrip))
                    {
                        string modifyResultHtml = _requestClient.post_modifyTrip_form(matchTrip, modifyOrderUrl, tripType);
                        result = _htmlParser.ModifyOrderResult(modifyResultHtml);
                        if (result == CrawlerModels.modifyResult.needConfirm)
                        {
                            var    confirmOrderUrl = _htmlParser.GetNextStepUrl(modifyResultHtml);
                            string confirmResult   = _requestClient.post_confirm_form(confirmOrderUrl);
                            result = _htmlParser.ModifyOrderResult(confirmResult);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
            }
            return(result);
        }
Ejemplo n.º 5
0
        //將設定檔的訂位時間轉成高鐵網站可用的格式
        public (string tripDate, string tripTime, DateTime tripDateTime) GetValidOrderDate(TicketOrders config, CrawlerModels.ModifyTripType tripType, CrawlerModels.orderPageInfo orderInfo)
        {
            string tripDate   = "";
            string tripTime   = "";
            var    targetDate = config.targetDate;

            switch (tripType)
            {
            case CrawlerModels.ModifyTripType.To:
                tripDate = targetDate.TripToDate;
                tripTime = targetDate.TripToTime;
                break;

            case CrawlerModels.ModifyTripType.Back:
                tripDate = targetDate.TripBackDate;
                tripTime = targetDate.TripBackTime;
                break;
            }

            var formatTime = getTimeDropDownValue(tripTime);

            if (string.IsNullOrEmpty(formatTime))
            {
                var msg = $"訂位代號:{config.OrderId} 設定檔Time欄位格式錯誤,time:{tripTime} 必須為mm:dd 以30分鐘為間隔";
                _logger.LogCritical(msg);
                throw new InvalidConfigException(msg);
            }
            var tripDateTime = Convert.ToDateTime($"{tripDate} {tripTime}");

            return(tripDate, formatTime, tripDateTime);
        }