Ejemplo n.º 1
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.º 2
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.º 3
0
        public void compareTripDateAndConfigDate(CrawlerModels.tripInfo tripInfo, DateTime configDate)
        {
            DateTime orderDate;

            try
            {
                if (tripInfo == null)
                {
                    throw new ArgumentException("無法取得訂位紀錄的車次");
                }

                var orderTime = tripInfo.tripType == "回程" ? tripInfo.startTime : tripInfo.arrivalTime;
                orderDate = Convert.ToDateTime(DateTime.Now.Year + $"/{tripInfo.date} {orderTime}");
            }
            catch (Exception ex) {
                throw new ArgumentException($"無法取得訂位紀錄的車次日期 {tripInfo.date} {tripInfo.arrivalTime}");
            }

            if (tripInfo.tripType == "去程")
            {
                //加30分鐘的buffer
                if (configDate <= orderDate.AddMinutes(30))
                {
                    throw new InvalidConfigException("設定檔的回程不得小於訂位紀錄的去程抵達時間");
                }
            }
            else if (tripInfo.tripType == "回程")
            {
                //因為是算去程到達時間,而設定檔是設出發時間,就再加個2.5小時當作去程抵達時間
                if (configDate.AddHours(2.5) >= orderDate)
                {
                    throw new InvalidConfigException("設定檔的去程不得大於訂位紀錄的回程出發時間");
                }
            }
            else
            {
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="trips">搜尋車次頁面取得的車次列表</param>
        /// <param name="orderTripInfo">目前訂單的行程資訊</param>
        /// <param name="ConfigTargetDate">設定檔設定的目標出發時間</param>
        /// <returns></returns>
        public string FindMatchTrip(List <CrawlerModels.Trips> trips, CrawlerModels.tripInfo orderTripInfo, DateTime ConfigTargetDate)
        {
            //組出行程的抵達時間
            var orderArrivalDate = Convert.ToDateTime(DateTime.Now.Year + $"/{orderTripInfo.date} {orderTripInfo.arrivalTime}");
            var tripsOrderByTime = trips.OrderBy(x => x.arrivalTime).ToList();

            //依照自訂方法排序
            tripsOrderByTime.Sort();
            //將車次列表轉成Datetime格式
            var tripsList = tripsOrderByTime.Select(x => new
            {
                buttonName  = x.buttonName,
                arrivalTime = Convert.ToDateTime(DateTime.Now.Year + $"/{x.date} {x.arrivalTime}"),
                startTime   = Convert.ToDateTime(DateTime.Now.Year + $"/{x.date} {x.startTime}"),
                totalTime   = x.totalTime
            })
                            //如果出發時間跟設定檔設定的目標出發時間相比,差超過一小時,就不要使用
                            .Where(x => Math.Abs((x.startTime - ConfigTargetDate).TotalHours) < 1)
                            .ToList();

            //找出比目前訂位紀錄的抵達時間早的車次 這個判斷可能會有問題 先拿掉好了 依搜尋到的結果為主
            // return tripsList.FirstOrDefault(x => x.arrivalTime < orderArrivalDate)?.buttonName;
            return(tripsList.FirstOrDefault()?.buttonName);
        }