示例#1
0
文件: YRC.cs 项目: alefurman40/api_2
        private void GetYRCInfo_API(ref bool isDurachem, ref string[] weight, ref string[] fclass,
                                    ref string[] nmfc, ref string[] pieces, ref int numLineItems, ref bool dimsProvided,
                                    ref string[] cube, ref string[] density, ref StringBuilder sbAccessorials, int AccOptionCount, ref string[] length,
                                    ref string[] width, ref string[] height)
        {
            //DB.Log("YRC Live", "GetYRCInfo");

            YRC_Res = new YRC_Result();
            try
            {
                #region Variables

                //DateTime today = DateTime.Today;
                DateTime today     = quoteData.puDate;
                string   day       = today.Day.ToString();
                string   month     = today.Month.ToString();
                string   year      = today.Year.ToString();
                string   dayOfWeek = today.DayOfWeek.ToString();
                dayOfWeek = dayOfWeek.Remove(3);
                if (day.Length == 1)
                {
                    day = "0" + day;
                }
                if (month.Length == 1)
                {
                    month = "0" + month;
                }

                string doc = "";
                string url, referrer, contentType, accept, method;

                #endregion

                #region Country

                string origCountry = "USA";
                string destCountry = "USA";
                if (HelperFuncs.GetCountryByZip(quoteData.origZip, true, quoteData.origZip, quoteData.destZip).Equals("CANADA"))
                {
                    origCountry = "CAN";
                }
                if (HelperFuncs.GetCountryByZip(quoteData.destZip, false, quoteData.origZip, quoteData.destZip).Equals("CANADA"))
                {
                    destCountry = "CAN";
                }

                #endregion

                #region 10 units limit

                if (fclass.Length > 10)
                {
                    throw new Exception("YRC Freight: Shipments of up to 10 units are handled, you have entered " + fclass.Length.ToString() + " units.");
                }

                #endregion

                #region Items

                StringBuilder items = new StringBuilder();

                string itemNum = string.Empty;

                for (int i = 0; i < fclass.Length; i++)
                {
                    itemNum = (i + 1).ToString();
                    items.Append(string.Concat("&LineItemWeight", itemNum, "=", weight[i]));

                    if (fclass[i].Equals(string.Empty))
                    {
                        // Use NMFC instead of class
                        string[] forSplit = nmfc[i].Split('-');
                        if (forSplit.Length.Equals(2))
                        {
                            items.Append(string.Concat("&LineItemNmfcPrefix", itemNum, "=", forSplit[0], "&LineItemNmfcSuffix", itemNum, "=0", forSplit[1]));
                        }
                        else
                        {
                            items.Append(string.Concat("&LineItemNmfcPrefix", itemNum, "=", nmfc[i], "&LineItemNmfcSuffix", itemNum, "="));
                        }
                    }
                    else
                    {
                        items.Append(string.Concat("&LineItemNmfcClass", itemNum, "="));
                        items.Append(fclass[i]);
                    }

                    items.Append(string.Concat("&LineItemCount", itemNum, "=", pieces[i]));

                    if (dimsProvided.Equals(true))
                    {
                        items.Append(
                            string.Concat(
                                "&LineItemPackageLength", itemNum, "=", length[i],
                                "&LineItemPackageWidth", itemNum, "=", width[i],
                                "&LineItemPackageHeight", itemNum, "=", height[i])
                            );
                    }
                }

                #endregion

                #region Make http requests

                referrer = "";
                url      = string.Concat("http://my.yrc.com/myyrc-api/national/servlet?CONTROLLER=com.rdwy.ec.rexcommon.proxy.http.controller.ProxyApiController&redir=/tfq561",
                                         "&LOGIN_USERID=", acctInfo.username, "&LOGIN_PASSWORD="******"&BusId=", acctInfo.acctNum, "&BusRole=Third%20Party&PaymentTerms=Prepaid",
                                         "&OrigCityName=", quoteData.origCity.Replace(" ", "%20"), "&OrigStateCode=", quoteData.origState, "&OrigZipCode=", quoteData.origZip, "&OrigNationCode=", origCountry,
                                         "&DestCityName=", quoteData.destCity.Replace(" ", "%20"), "&DestStateCode=", quoteData.destState, "&DestZipCode=", quoteData.destZip, "&DestNationCode=", destCountry,
                                         "&ServiceClass=STD&PickupDate=", year, month, day, "&TypeQuery=QUOTE",
                                         items, "&LineItemCount=", fclass.Length,

                                         sbAccessorials, "&AccOptionCount=", AccOptionCount);

                //"&AccOption1=HOMD&AccOptionCount=1");
                contentType = "";
                method      = "GET";
                accept      = "*/*";
                doc         = (string)HelperFuncs.generic_http_request("string", null, url, referrer, contentType, accept, method, "", false);

                //DB.Log("YRC Live request", url);
                //DB.Log("YRC_Live response", doc);


                #endregion

                #region Get regular rates

                //string tmp = doc;
                bool isIntraCanada = false;
                if (origCountry == "CAN" && destCountry == "CAN")
                {
                    if (!quoteData.username.ToLower().Equals("durachem"))
                    {
                        throw new Exception("Intra Canada");  // Intra Canada enabled only for durachem for now
                    }
                    isIntraCanada = true;
                }

                #region Get cost and transit days from the result

                string[] tokens = new string[3];
                tokens[0] = "<TotalCharges>";
                tokens[1] = ">";
                tokens[2] = "<";

                string costStr = HelperFuncs.scrapeFromPage(tokens, doc);

                //DB.Log("YRC_Live costStr", costStr);

                int costInt   = -1;
                int costCents = 0;

                if (int.TryParse(costStr, out int testInt))
                {
                    costInt   = testInt / 100;
                    costCents = testInt % 100;

                    //DB.Log("YRC_Live costInt costCents", costInt + " " + costCents);
                }

                if (double.TryParse(string.Concat(costInt, ".", costCents.ToString().PadLeft(2, '0')), out double costDouble))
                {
                    YRC_Res.standardLTL = costDouble;
                    //DB.Log("YRC_Live costDouble", costDouble.ToString());
                }

                YRC_Res.days = 5;
                tokens[0]    = "<StandardDays>";

                int.TryParse(HelperFuncs.scrapeFromPage(tokens, doc), out YRC_Res.days);

                #endregion

                #endregion
            }

            catch (Exception e)
            {
                #region Exception code

                DB.Log("YRC", e.ToString());

                YRC_Res.standardLTL = -1;

                #endregion
            }
        }
示例#2
0
    public IntermodalRater.railResult getRate()
    {
        // CSXI are blocking our IP bc of scraping
        railResultObj.success = "";
        return(railResultObj);

        int timeOut = 25000;

        try
        {
            HelperFuncs.writeToSiteErrors("CSXI", "oop");
            List <HelperFuncs.Credentials> crds = new List <HelperFuncs.Credentials>();
            string username = "", password = "";

            try
            {
                crds     = HelperFuncs.GetLoginsByCarID(90199);
                username = crds[0].username;
                password = crds[0].password;
            }
            catch (Exception ex)
            {
                HelperFuncs.writeToSiteErrors("CSXI", ex.ToString());
            }

            string url, referrer, contentType, accept, method, data, doc, originCity = "", destCity = "", originState = "", destState = "";

            CookieContainer  container  = new CookieContainer();
            CookieCollection collection = new CookieCollection();

            #region Login and go to rate page

            #region Login request

            referrer    = "https://shipcsx.com/pub_sx_mainpagepublic_jct/sx.shipcsxpublic/PublicNavbar";
            url         = "https://shipcsx.com/pkmslogin.form";
            contentType = "application/x-www-form-urlencoded";
            accept      = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            method      = "POST";
            data        = "login-form-type=pwd&username="******"&password="******"&LoginGoButton.x=7&LoginGoButton.y=8"; //user password
            collection  = (CookieCollection)HelperFuncs.generic_http_request("collection", container, url, referrer, contentType, accept, method, data, true, timeOut);

            #endregion

            #region Some required requests
            //--------------------------------------------------------------------------------------------------------------
            url         = "https://shipcsx.com/sx_mainpage_jct/sx.shipcsx/Main";
            referrer    = "";
            contentType = "";
            method      = "GET";
            collection  = (CookieCollection)HelperFuncs.generic_http_request("collection", container, url, referrer, contentType, accept, method, "", true, timeOut);

            //--------------------------------------------------------------------------------------------------------------
            referrer   = url;
            url        = "https://shipcsx.com/sx_mainpage_jct/sx.shipcsx/Navbar";
            collection = (CookieCollection)HelperFuncs.generic_http_request("collection", container, url, referrer, contentType, accept, method, "", true, timeOut);

            url         = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/TruckingMain";
            referrer    = "https://shipcsx.com/sx_mainpage_jct/sx.shipcsx/Navbar";
            contentType = "";
            method      = "GET";
            collection  = (CookieCollection)HelperFuncs.generic_http_request("collection", container, url, referrer, contentType, accept, method, "", true, timeOut);

            //--------------------------------------------------------------------------------------------------------------

            referrer   = url;
            url        = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/jsp/sx_base_classes/blank.jsp";
            collection = (CookieCollection)HelperFuncs.generic_http_request("collection", container, url, referrer, contentType, accept, method, "", true, timeOut);

            //--------------------------------------------------------------------------------------------------------------

            //referrer = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/TruckingMain";
            url        = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/SpotQuoteBegin?";
            collection = (CookieCollection)HelperFuncs.generic_http_request("collection", container, url, referrer, contentType, accept, method, "", true, timeOut);

            //--------------------------------------------------------------------------------------------------------------

            referrer   = url;
            url        = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/jsp/sx_base_classes/blank.jsp";
            collection = (CookieCollection)HelperFuncs.generic_http_request("collection", container, url, referrer, contentType, accept, method, "", true, timeOut);

            //--------------------------------------------------------------------------------------------------------------
            #endregion

            #region Get City and State

            // Get City and State
            referrer = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/SpotQuoteBegin?";
            url      = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/GetCityStateData?zip=" + origZip;
            doc      = (string)HelperFuncs.generic_http_request("string", container, url, referrer, contentType, accept, method, "", true, timeOut);

            // Scrape origin city and state
            string[] tokens = new string[3];
            tokens[0] = "<city>";
            tokens[1] = ">";
            tokens[2] = "<";

            HelperFuncs.writeToSiteErrors("CSXI origZip", origZip);

            originCity = HelperFuncs.scrapeFromPage(tokens, doc);
            if (originCity == "" || originCity == "not found")
            {
                throw new Exception("No matching City/State for zipcode " + origZip);
            }

            tokens[0]   = "<state>";
            originState = HelperFuncs.scrapeFromPage(tokens, doc);

            url = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/GetUnservicableLocations?zip=" + origZip;
            doc = (string)HelperFuncs.generic_http_request("string", container, url, referrer, contentType, accept, method, "", true, timeOut);

            url = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/GetUnservicableLocations?zip=" + destZip;
            doc = (string)HelperFuncs.generic_http_request("string", container, url, referrer, contentType, accept, method, "", true, timeOut);

            url = "https://shipcsx.com/sx_dealspace_jct/sx.dealspace/GetCityStateData?zip=" + destZip;
            doc = (string)HelperFuncs.generic_http_request("string", container, url, referrer, contentType, accept, method, "", true, timeOut);

            // Scrape destination city and state
            tokens[0] = "<city>";
            destCity  = HelperFuncs.scrapeFromPage(tokens, doc);
            if (destCity == "" || destCity == "not found")
            {
                throw new Exception("No matching City/State for zipcode " + destZip);
            }

            tokens[0] = "<state>";
            destState = HelperFuncs.scrapeFromPage(tokens, doc);

            #endregion

            #region Variables

            HelperFuncs.writeToSiteErrors("CSXI TEST TEST TEST", "CSXI TEST TEST TEST");

            HelperFuncs.writeToSiteErrors("CSXI puDate", puDate.ToShortDateString());
            DateTime dt     = puDate;
            DateTime dtLast = new DateTime();
            dtLast = DateTime.Today.AddDays(6);
            TimeSpan span = dtLast - dt;

            HelperFuncs.writeToSiteErrors("CSXI span.TotalDays", span.TotalDays.ToString());

            bool hasCapacity = true;
            //string hazmat = "";
            //double parseDbl;
            bool   successBool = false;
            string price       = "";
            string transitTime = "";

            bool isLastDateTry = false;

            #endregion

            for (int i = 0; i <= span.TotalDays; i++)
            {
                //HelperFuncs.writeToSiteErrors("CSXI i", i.ToString());
                //HelperFuncs.writeToSiteErrors("CSXI span.TotalDays", span.TotalDays.ToString());

                if (i.Equals((int)span.TotalDays))
                {
                    HelperFuncs.writeToSiteErrors("CSXI i=span", "CSXI i=span");
                    isLastDateTry = true;
                }

                dt = DateTime.Today.AddDays(i + 1);

                if (dt.DayOfWeek.ToString() == "Saturday" || dt.DayOfWeek.ToString() == "Sunday")
                {
                    continue;
                }

                #region Fix date

                string day = dt.Day.ToString(), month = dt.Month.ToString(), year = dt.Year.ToString();
                if (day.Length.Equals(1))
                {
                    day = "0" + day;
                }

                if (month.Length.Equals(1))
                {
                    month = "0" + month;
                }

                #endregion

                hasCapacity = true;

                tryDateCSXI_WithCapacity(ref container, ref origZip, ref originCity,
                                         ref originState, ref destZip, ref destCity, ref destState, ref month, ref day, ref year,
                                         ref hasCapacity, ref transitTime, ref price, ref successBool, ref isLastDateTry);

                if (successBool.Equals(true) && hasCapacity.Equals(true))
                {
                    break;
                }
            }

            #endregion

            #region Not used
            //for (int i = 0; i <= span.TotalDays; i++)
            //{
            //    dt = DateTime.Today.AddDays(i + 1);
            //    //dt = dt.AddDays(i);
            //    if (dt.DayOfWeek.ToString() == "Saturday" || dt.DayOfWeek.ToString() == "Sunday")
            //    {
            //        continue;
            //    }
            //    doc = tryDateCSXI(container, originZipGlobal, destZipGlobal, originCity, destCity, originState, destState, hazmat, i + 1);
            //    //if (doc.Contains("No pricing is available"))
            //    //{
            //    //    throw new Exception("No pricing is available");
            //    //}

            //    tokens[0] = "<price>";
            //    tokens[1] = ">";
            //    tokens[2] = "<";

            //    price = HelperFuncs.scrapeFromPage(tokens, doc).Replace("$", "");
            //    if (!double.TryParse(price, out parseDbl))
            //    {
            //        continue;
            //    }
            //    else
            //    {
            //        successBool = true;
            //    }

            //    tokens[0] = "<transitTime>";
            //    tokens[1] = ">";
            //    tokens[2] = "<";
            //    transitTime = HelperFuncs.scrapeFromPage(tokens, doc).Replace("days", "").Trim();

            //    if (successBool.Equals(true))
            //    {
            //        break;
            //    }
            //}
            #endregion

            if (successBool == true)
            {
                #region Set the result object

                HelperFuncs.writeToSiteErrors("CSXI success", "CSXI success");
                //string[] csxiResultArray = new string[7];
                ////csxiResultArray[0] = SharedRail.success;
                //csxiResultArray[0] = "success";
                //csxiResultArray[1] = "CSXI";

                //csxiResultArray[3] = transitTime;

                //csxiResultArray[2] = price;
                //insertIntoRailLogs("CSXI", 90199, "1", "", "", Convert.ToDouble(rate));
                HelperFuncs.writeToSiteErrors("CSXI live rate", price);

                //csxiResultArray[4] = dt.ToShortDateString();

                Int32 transit;
                //if (!Int32.TryParse(csxiResultArray[3], out transit))
                //{
                //    HelperFuncs.writeToSiteErrors("CSXI", "could not parse transit " + csxiResultArray[3]);
                //}
                //csxiResultArray[5] = dt.AddDays(transit).ToShortDateString();
                //csxiResultArray[6] = "FiftyThreeFt";

                //List<string[]> list
                //list.Add(csxiResultArray);


                railResultObj.success           = "success";
                railResultObj.transitTime       = transitTime;
                railResultObj.rate              = price;
                railResultObj.firstCapacityDate = dt;
                if (int.TryParse(transitTime, out transit))
                {
                    railResultObj.eta = dt.AddDays(transit);
                }

                railResultObj.hasCapacity   = hasCapacity;
                railResultObj.containerSize = "FiftyThreeFt";

                return(railResultObj);

                #endregion
            }
            else
            {
                //throw new Exception("No Capacity for all days, or no rate found");
                HelperFuncs.writeToSiteErrors("CSXI", "No Capacity for all days, or no rate found");
                railResultObj.success = "";
                return(railResultObj);
            }
        }
        catch (Exception e)
        {
            HelperFuncs.writeToSiteErrors("CSXI", e.ToString());
            railResultObj.success = "";
            return(railResultObj);
        }
    }