protected virtual void WritePostParameters(Stream outputStream, WriteXmlDelegate writeXmlDelegate)
        {
            using (var xmlWriter = new XmlTextWriter(outputStream, Encoding.UTF8))
            {
                xmlWriter.WriteStartDocument();
                xmlWriter.Formatting = Formatting.Indented;

                writeXmlDelegate(xmlWriter);

                xmlWriter.WriteEndDocument();
            }
#if (DEBUG)
            // Also copy XML to debug output
            Console.WriteLine("Sending Data:");
            var s = new MemoryStream();
            using (var xmlWriter = new XmlTextWriter(s, Encoding.UTF8))
            {
                xmlWriter.WriteStartDocument();
                xmlWriter.Formatting = Formatting.Indented;

                writeXmlDelegate(xmlWriter);

                xmlWriter.WriteEndDocument();
            }
            Console.WriteLine(Encoding.UTF8.GetString(s.ToArray()));
#endif
        }
Exemple #2
0
        private static void WritePostParameters(System.IO.Stream outputStream, WriteXmlDelegate writeXmlDelegate)
        {
            using (XmlTextWriter xmlWriter = new XmlTextWriter(outputStream, Encoding.UTF8))
            {
                xmlWriter.WriteStartDocument();
                xmlWriter.Formatting = Formatting.Indented;

                writeXmlDelegate(xmlWriter);

                xmlWriter.WriteEndDocument();
            }
        }
        public override void scan(int sleepTime = 0)
        {
            DriverWrapper driver = null;

            ScoreBoardFinder sbf = new ScoreBoardFinder(sleepTime);

            sbf.Start();

            while (true)
            {
                try
                {
                    if (driver == null)
                    {
                        driver = driverCreator.CreateDriver("");
                    }

                    var hstats = new Dictionary <string, int>();
                    var astats = new Dictionary <string, int>();

                    var copyOfTheList = sbf.GetWebBlobs();

                    log.Debug("There are " + copyOfTheList.Count() + " games in the blob list");
                    if (copyOfTheList.Count() == 0)
                    {
                        var downTime = 20000;
                        log.Debug("Going to sleep for " + downTime + " miiliseconds");
                        driver.ForceSleep(downTime);
                        continue;
                    }

                    foreach (var blob in copyOfTheList)
                    {
                        log.Debug("On the blob loop...");

                        string homeTeamName = blob.HomeTeam;
                        string awayTeamName = blob.AwayTeam;
                        string league       = blob.League;

                        string scoreUrl = blob.Url;

                        driver.Url = scoreUrl;
                        driver.DirtySleep(sleepTime);

                        string text = driver.GetElementText("//*[@id=\"commentaryContent\"]");

                        int totalDAs = -1;
                        int totalAs  = -1;
                        int totalCs  = -1;
                        int totalBs  = -1;

                        int homeDAs    = -1;
                        int homeAs     = -1;
                        int homeCs     = -1;
                        int homeBs     = -1;
                        int homeSonTs  = -1;
                        int homeSoffTs = -1;

                        int awayDAs    = -1;
                        int awayAs     = -1;
                        int awayCs     = -1;
                        int awayBs     = -1;
                        int awaySonTs  = -1;
                        int awaySoffTs = -1;

                        if (string.IsNullOrEmpty(text) == false)
                        {
                            var splits = text.Split('\n').ToList();

                            splits.RemoveAll(x => String.IsNullOrEmpty(x));
                            splits.RemoveAll(x => x[0] == ' ');
                            splits.RemoveAll(x => char.IsDigit(x[0]));

                            var distinct = splits.Distinct();

                            var query = distinct.Select(g => new { Name = g, Count = g.Count() });

                            splits.ForEach(x => x.Trim());

                            totalDAs = splits.Count(x => x.StartsWith("Dangerous Attack by"));
                            totalAs  = splits.Count(x => x.StartsWith("Attack by"));
                            totalCs  = splits.Count(x => x.StartsWith("Clearance by"));
                            totalBs  = splits.Count(x => x.StartsWith("Blocked Shot for"));

                            homeDAs    = splits.Count(x => x.StartsWith("Dangerous Attack by " + homeTeamName));
                            homeAs     = splits.Count(x => x.StartsWith("Attack by " + homeTeamName));
                            homeCs     = splits.Count(x => x.StartsWith("Clearance by " + homeTeamName));
                            homeBs     = splits.Count(x => x.StartsWith("Blocked Shot for " + homeTeamName));
                            homeSonTs  = splits.Count(x => x.StartsWith("Shot On Target for " + homeTeamName));
                            homeSoffTs = splits.Count(x => x.StartsWith("Shot Off Target for " + homeTeamName));

                            awayDAs    = splits.Count(x => x.StartsWith("Dangerous Attack by " + awayTeamName));
                            awayAs     = splits.Count(x => x.StartsWith("Attack by " + awayTeamName));
                            awayCs     = splits.Count(x => x.StartsWith("Clearance by " + awayTeamName));
                            awayBs     = splits.Count(x => x.StartsWith("Blocked Shot for " + awayTeamName));
                            awaySonTs  = splits.Count(x => x.StartsWith("Shot On Target for " + awayTeamName));
                            awaySoffTs = splits.Count(x => x.StartsWith("Shot Off Target for " + awayTeamName));

                            if (homeDAs + awayDAs != totalDAs)
                            {
                                if (homeDAs == 0)
                                {
                                    homeDAs = totalDAs - awayDAs;
                                }
                                if (awayDAs == 0)
                                {
                                    awayDAs = totalDAs - homeDAs;
                                }
                            }

                            if (homeAs + awayAs != totalAs)
                            {
                                if (homeAs == 0)
                                {
                                    homeAs = totalAs - awayAs;
                                }
                                if (awayAs == 0)
                                {
                                    awayAs = totalAs - homeAs;
                                }
                            }

                            if (homeCs + awayCs != totalCs)
                            {
                                if (homeCs == 0)
                                {
                                    homeCs = totalCs - awayCs;
                                }
                                if (awayCs == 0)
                                {
                                    awayCs = totalCs - homeCs;
                                }
                            }

                            if (homeBs + awayBs != totalBs)
                            {
                                if (homeBs == 0)
                                {
                                    homeBs = totalBs - awayBs;
                                }
                                if (awayBs == 0)
                                {
                                    awayBs = totalBs - homeBs;
                                }
                            }
                        }

                        log.Debug("Game:\t\t" + homeTeamName + " v " + awayTeamName);

                        if (String.IsNullOrEmpty(scoreUrl) == false)
                        {
                            string previewText = driver.GetElementText("//*[@id=\"previewContents\"]");
                            string time        = driver.GetElementText("//*[@id=\"time\"]");
                            string period      = driver.GetElementText("//*[@id=\"period\"]");

                            if (string.IsNullOrEmpty(time))
                            {
                                time = period;
                            }

                            //"Ivory Coast\r\nTogo\r\n56%\r\n44%"
                            //"TotalNormal Time1st Half2nd Half\r\nIvory Coast 2 0 5 5 1 5 18 22 1 0\r\nTogo 1 0 4 9 0 6 16 21 3 0"
                            bool clickedOk = driver.ClickElement("//*[@id=\"statisticsTab\"]");

                            if (!clickedOk)
                            {
                                log.Error("======> Click failed");
                            }

                            var    previewSplits = Regex.Split(previewText, "\r\n").ToList();
                            string aPossession   = previewSplits.Last().Replace("%", "");
                            previewSplits.RemoveAt(previewSplits.Count() - 1);
                            string hPossession = previewSplits.Last().Replace("%", "");

                            string statsText = driver.GetElementText("//*[@id=\"statsTable\"]/tbody");

                            string homeStatsText = Regex.Split(statsText, "\r\n").ToList().ElementAt(0);
                            string awayStatsText = Regex.Split(statsText, "\r\n").ToList().ElementAt(1);

                            var homeStatsList = Regex.Split(homeStatsText, " ").ToList();
                            var justHomeStats = homeStatsList.GetRange(homeStatsList.Count() - 10, 10);

                            var awayStatsList = Regex.Split(awayStatsText, " ").ToList();
                            var justAwayStats = awayStatsList.GetRange(awayStatsList.Count() - 10, 10);

                            hstats[statType[0]] = ParseInt(statType[0], hPossession);

                            for (int i = 0; i != 10; ++i)
                            {
                                int parseResult = ParseInt(statType[i + 1], justHomeStats[i]);
                                if (parseResult == -1 && (i == 0 || i == 5 || i == 8 || i == 9))
                                {
                                    hstats[statType[i + 1]] = 0;
                                }
                                else
                                {
                                    hstats[statType[i + 1]] = parseResult;
                                }
                            }

                            if (hstats[statType[3]] == -1)
                            {
                                hstats[statType[3]] = homeSonTs;
                            }

                            if (hstats[statType[4]] == -1)
                            {
                                hstats[statType[4]] = homeSoffTs;
                            }

                            hstats[statType[11]] = homeAs;
                            hstats[statType[12]] = homeDAs;
                            hstats[statType[13]] = homeBs;
                            hstats[statType[14]] = homeCs;

                            astats[statType[0]] = ParseInt(statType[0], aPossession);

                            for (int i = 0; i != 10; ++i)
                            {
                                int parseResult = ParseInt(statType[i + 1], justAwayStats[i]);
                                if (parseResult == -1 && (i == 0 || i == 5 || i == 8 || i == 9))
                                {
                                    astats[statType[i + 1]] = 0;
                                }
                                else
                                {
                                    astats[statType[i + 1]] = parseResult;
                                }
                            }

                            if (astats[statType[3]] == -1)
                            {
                                astats[statType[3]] = awaySonTs;
                            }

                            if (astats[statType[4]] == -1)
                            {
                                astats[statType[4]] = awaySoffTs;
                            }

                            astats[statType[11]] = awayAs;
                            astats[statType[12]] = awayDAs;
                            astats[statType[13]] = awayBs;
                            astats[statType[14]] = awayCs;

                            homeTeamName = DoSubstitutions(homeTeamName);
                            awayTeamName = DoSubstitutions(awayTeamName);
                            league       = DoSubstitutions(league);

                            bool homeTeamLongest = homeTeamName.Length > awayTeamName.Length;

                            log.Info("League:\t\t" + league + " at " + time);
                            log.Info(homeTeamName.PadRight(homeTeamLongest ? homeTeamName.Length + 1 : awayTeamName.Length + 1) + String.Join(" ", hstats.Values));
                            log.Info(awayTeamName.PadRight(homeTeamLongest ? homeTeamName.Length + 1 : awayTeamName.Length + 1) + String.Join(" ", astats.Values));

                            if (hstats.Keys.Any(x => x == "-1") || astats.Keys.Any(x => x == "-1"))
                            {
                                log.Warn("Bad Stat detected.... skipping");
                                continue;
                            }

                            string today     = DateTime.Now.ToUniversalTime().ToString("ddMMyy");
                            string yesterday = (DateTime.Today.ToUniversalTime() - TimeSpan.FromDays(1)).ToString("ddMMyy");
                            string finalName = Path.Combine(xmlPath, league, homeTeamName + " v " + awayTeamName + "_" + today + ".xml");

                            bool exists = File.Exists(finalName);

                            //edge case of games going over midnight
                            bool bOverMidnight = false;
                            if (exists == false)
                            {
                                string anotherName = Path.Combine(xmlPath, league, homeTeamName + " v " + awayTeamName + "_" + yesterday + ".xml");
                                if (File.Exists(anotherName))
                                {
                                    finalName     = anotherName;
                                    exists        = true;
                                    bOverMidnight = true;
                                }
                            }

                            SendToWebDelegate sd = new SendToWebDelegate(SendToWeb);
                            sd.BeginInvoke(league, bOverMidnight ? DateTime.Today.ToUniversalTime() - TimeSpan.FromDays(1) : DateTime.Now.ToUniversalTime(), homeTeamName, awayTeamName, hstats, astats, time, null, null);

                            WriteXmlDelegate wd = new WriteXmlDelegate(WriteXml);
                            wd.BeginInvoke(xmlPath, hstats, astats, homeTeamName, awayTeamName, league, time, exists, finalName, null, null);
                        }
                    }
                }
                catch (Exception ce)
                {
                    log.Error("Exception caught: " + ce);
                    if (driver != null)
                    {
                        driver.Quit();
                        driver.Dispose();
                        driver = null;
                    }
                }
            }
        }
        protected virtual void WritePostParameters(Stream outputStream, WriteXmlDelegate writeXmlDelegate)
        {
            using (var xmlWriter = new XmlTextWriter(outputStream, Encoding.UTF8))
            {
                xmlWriter.WriteStartDocument();
                xmlWriter.Formatting = Formatting.Indented;

                writeXmlDelegate(xmlWriter);

                xmlWriter.WriteEndDocument();
            }
            #if (DEBUG)
            // Also copy XML to debug output
            Console.WriteLine("Sending Data:");
            var s = new MemoryStream();
            using (var xmlWriter = new XmlTextWriter(s, Encoding.UTF8))
            {
                xmlWriter.WriteStartDocument();
                xmlWriter.Formatting = Formatting.Indented;

                writeXmlDelegate(xmlWriter);

                xmlWriter.WriteEndDocument();
            }
            Console.WriteLine(Encoding.UTF8.GetString(s.ToArray()));
            #endif
        }
        protected virtual HttpStatusCode PerformRequest(HttpRequestMethod method, string urlPath,
            WriteXmlDelegate writeXmlDelegate, ReadXmlDelegate readXmlDelegate, ReadXmlListDelegate readXmlListDelegate)
        {
            var url = Settings.GetServerUri(urlPath);
            #if (DEBUG)
            Console.WriteLine("Requesting " + method + " " + url);
            #endif
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "application/xml";      // Tells the server to return XML instead of HTML
            request.ContentType = "application/xml; charset=utf-8"; // The request is an XML document
            request.SendChunked = false;             // Send it all as one request
            request.UserAgent = Settings.UserAgent;
            request.Headers.Add(HttpRequestHeader.Authorization, Settings.AuthorizationHeaderValue);
            request.Method = method.ToString().ToUpper();

            Debug.WriteLine(String.Format("Recurly: Requesting {0} {1}", request.Method, request.RequestUri));

            if ((method == HttpRequestMethod.Post || method == HttpRequestMethod.Put) && (writeXmlDelegate != null))
            {
                // 60 second timeout -- some payment gateways (e.g. PayPal) can take a while to respond
                request.Timeout = 60000;

                // Write POST/PUT body
                using (var requestStream = request.GetRequestStream())
                {
                    WritePostParameters(requestStream, writeXmlDelegate);
                }
            }
            else
            {
                request.ContentLength = 0;
            }

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {

                    ReadWebResponse(response, readXmlDelegate, readXmlListDelegate);
                    return response.StatusCode;

                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null) throw;

                var response = (HttpWebResponse)ex.Response;
                var statusCode = response.StatusCode;
                Error[] errors;

                Debug.WriteLine(String.Format("Recurly Library Received: {0} - {1}", (int)statusCode, statusCode));

                switch (response.StatusCode)
                {
                    case HttpStatusCode.OK:
                    case HttpStatusCode.Accepted:
                    case HttpStatusCode.Created:
                    case HttpStatusCode.NoContent:
                        ReadWebResponse(response, readXmlDelegate, readXmlListDelegate);

                        return HttpStatusCode.NoContent;

                    case HttpStatusCode.NotFound:
                        errors = Error.ReadResponseAndParseErrors(response);
                        if (errors.Length > 0)
                            throw new NotFoundException(errors[0].Message, errors);
                        throw new NotFoundException("The requested object was not found.", errors);

                    case HttpStatusCode.Unauthorized:
                    case HttpStatusCode.Forbidden:
                        errors = Error.ReadResponseAndParseErrors(response);
                        throw new InvalidCredentialsException(errors);

                    case HttpStatusCode.PreconditionFailed:
                        errors = Error.ReadResponseAndParseErrors(response);
                        throw new ValidationException(errors);

                    case HttpStatusCode.ServiceUnavailable:
                        throw new TemporarilyUnavailableException();

                    case HttpStatusCode.InternalServerError:
                        errors = Error.ReadResponseAndParseErrors(response);
                        throw new ServerException(errors);
                }

                if ((int)statusCode == ValidationException.HttpStatusCode) // Unprocessable Entity
                {
                    errors = Error.ReadResponseAndParseErrors(response);
                    if (errors.Length > 0) Debug.WriteLine(errors[0].ToString());
                    else Debug.WriteLine("Client Error: " + response.ToString());
                    throw new ValidationException(errors);
                }

                throw;
            }
        }
 public HttpStatusCode PerformRequest(HttpRequestMethod method, string urlPath,
     WriteXmlDelegate writeXmlDelegate, ReadXmlDelegate readXmlDelegate)
 {
     return PerformRequest(method, urlPath, writeXmlDelegate, readXmlDelegate, null);
 }
        protected virtual HttpStatusCode PerformRequest(HttpRequestMethod method, string urlPath,
                                                        WriteXmlDelegate writeXmlDelegate, ReadXmlDelegate readXmlDelegate, ReadXmlListDelegate readXmlListDelegate, ReadResponseDelegate reseponseDelegate)
        {
            var url = Settings.GetServerUri(urlPath);

#if (DEBUG)
            Console.WriteLine("Requesting " + method + " " + url);
#endif
            var request = (HttpWebRequest)WebRequest.Create(url);

            if (!request.RequestUri.Host.EndsWith(Settings.ValidDomain))
            {
                throw new RecurlyException("Domain " + request.RequestUri.Host + " is not a valid Recurly domain");
            }

            request.Accept      = "application/xml";                // Tells the server to return XML instead of HTML
            request.ContentType = "application/xml; charset=utf-8"; // The request is an XML document
            request.SendChunked = false;                            // Send it all as one request
            request.UserAgent   = Settings.UserAgent;
            request.Headers.Add(HttpRequestHeader.Authorization, Settings.AuthorizationHeaderValue);
            request.Headers.Add("X-Api-Version", Settings.RecurlyApiVersion);
            request.Method = method.ToString().ToUpper();

            Console.WriteLine(String.Format("Recurly: Requesting {0} {1}", request.Method, request.RequestUri));

            if ((method == HttpRequestMethod.Post || method == HttpRequestMethod.Put) && (writeXmlDelegate != null))
            {
                // 60 second timeout -- some payment gateways (e.g. PayPal) can take a while to respond
                request.Timeout = 60000;

                // Write POST/PUT body
                using (var requestStream = request.GetRequestStream())
                {
                    WritePostParameters(requestStream, writeXmlDelegate);
                }
            }
            else
            {
                request.ContentLength = 0;
            }

            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    ReadWebResponse(response, readXmlDelegate, readXmlListDelegate, reseponseDelegate);
                    return(response.StatusCode);
                }
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    throw;
                }

                var    response   = (HttpWebResponse)ex.Response;
                var    statusCode = response.StatusCode;
                Errors errors;

                Console.WriteLine(String.Format("Recurly Library Received: {0} - {1}", (int)statusCode, statusCode));

                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                case HttpStatusCode.Accepted:
                case HttpStatusCode.Created:
                case HttpStatusCode.NoContent:
                    ReadWebResponse(response, readXmlDelegate, readXmlListDelegate, reseponseDelegate);

                    return(HttpStatusCode.NoContent);

                case HttpStatusCode.NotFound:
                    errors = Errors.ReadResponseAndParseErrors(response);
                    if (errors.ValidationErrors.HasAny())
                    {
                        throw new NotFoundException(errors.ValidationErrors[0].Message, errors);
                    }
                    throw new NotFoundException("The requested object was not found.", errors);

                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.Forbidden:
                    errors = Errors.ReadResponseAndParseErrors(response);
                    throw new InvalidCredentialsException(errors);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.PreconditionFailed:
                    errors = Errors.ReadResponseAndParseErrors(response);
                    throw new ValidationException(errors);

                case HttpStatusCode.ServiceUnavailable:
                    throw new TemporarilyUnavailableException();

                case HttpStatusCode.InternalServerError:
                    errors = Errors.ReadResponseAndParseErrors(response);
                    throw new ServerException(errors);
                }

                if ((int)statusCode == ValidationException.HttpStatusCode) // Unprocessable Entity
                {
                    errors = Errors.ReadResponseAndParseErrors(response);
                    if (errors.ValidationErrors.HasAny())
                    {
                        Console.WriteLine(errors.ValidationErrors[0].ToString());
                    }
                    else
                    {
                        Console.WriteLine("Client Error: " + response.ToString());
                    }
                    throw new ValidationException(errors);
                }

                throw;
            }
        }
 public HttpStatusCode PerformRequest(HttpRequestMethod method, string urlPath,
                                      WriteXmlDelegate writeXmlDelegate, ReadResponseDelegate responseDelegate)
 {
     return(PerformRequest(method, urlPath, writeXmlDelegate, null, null, responseDelegate));
 }
Exemple #9
0
        public static HttpStatusCode PerformRequest(HttpRequestMethod method, string urlPath,
                                                    WriteXmlDelegate writeXmlDelegate, ReadXmlDelegate readXmlDelegate)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerUrl(Environment) + urlPath);

            request.Accept      = "application/xml";                // Tells the server to return XML instead of HTML
            request.ContentType = "application/xml; charset=utf-8"; // The request is an XML document
            request.SendChunked = false;                            // Send it all as one request
            request.UserAgent   = UserAgent;
            request.Headers.Add(HttpRequestHeader.Authorization, AuthorizationHeaderValue);
            request.Method = method.ToString().ToUpper();

            System.Diagnostics.Debug.WriteLine(String.Format("Recurly: Requesting {0} {1}",
                                                             request.Method, request.RequestUri.ToString()));

            if ((method == HttpRequestMethod.Post || method == HttpRequestMethod.Put) && (writeXmlDelegate != null))
            {
                // 60 second timeout -- some payment gateways (e.g. PayPal) can take a while to respond
                request.Timeout = 60000;

                // Write POST/PUT body
                using (Stream requestStream = request.GetRequestStream())
                    WritePostParameters(requestStream, writeXmlDelegate);
            }

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    return(ReadWebResponse(response, readXmlDelegate));
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    HttpWebResponse response   = (HttpWebResponse)ex.Response;
                    HttpStatusCode  statusCode = response.StatusCode;
                    RecurlyError[]  errors;

                    System.Diagnostics.Debug.WriteLine(String.Format("Recurly Library Received: {0} - {1}",
                                                                     (int)statusCode, statusCode.ToString()));

                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.OK:
                    case HttpStatusCode.Accepted:
                    case HttpStatusCode.Created:
                    case HttpStatusCode.NoContent:
                        return(ReadWebResponse(response, readXmlDelegate));

                    case HttpStatusCode.NotFound:
                        errors = RecurlyError.ReadResponseAndParseErrors(response);
                        if (errors.Length >= 0)
                        {
                            throw new NotFoundException(errors[0].Message, errors);
                        }
                        else
                        {
                            throw new NotFoundException("The requested object was not found.", errors);
                        }

                    case HttpStatusCode.Unauthorized:
                    case HttpStatusCode.Forbidden:
                        errors = RecurlyError.ReadResponseAndParseErrors(response);
                        throw new InvalidCredentialsException(errors);

                    case HttpStatusCode.PreconditionFailed:
                        errors = RecurlyError.ReadResponseAndParseErrors(response);
                        throw new ValidationException(errors);

                    case HttpStatusCode.ServiceUnavailable:
                        throw new TemporarilyUnavailableException();

                    case HttpStatusCode.InternalServerError:
                        errors = RecurlyError.ReadResponseAndParseErrors(response);
                        throw new RecurlyServerException(errors);
                    }

                    if ((int)statusCode == ValidationException.HttpStatusCode) // Unprocessable Entity
                    {
                        errors = RecurlyError.ReadResponseAndParseErrors(response);
                        throw new ValidationException(errors);
                    }
                }

                throw;
            }
        }
Exemple #10
0
 public static HttpStatusCode PerformRequest(HttpRequestMethod method, string urlPath,
                                             WriteXmlDelegate writeXmlDelegate)
 {
     return(PerformRequest(method, urlPath, writeXmlDelegate, null));
 }
        private static void WritePostParameters(System.IO.Stream outputStream, WriteXmlDelegate writeXmlDelegate)
        {
            using (XmlTextWriter xmlWriter = new XmlTextWriter(outputStream, Encoding.UTF8))
            {
                xmlWriter.WriteStartDocument();
                xmlWriter.Formatting = Formatting.Indented;

                writeXmlDelegate(xmlWriter);

                xmlWriter.WriteEndDocument();
            }
        }
        public static HttpStatusCode PerformRequest(HttpRequestMethod method, string urlPath,
            WriteXmlDelegate writeXmlDelegate, ReadXmlDelegate readXmlDelegate)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerUrl(Environment) + urlPath);
            request.Accept = "application/xml";      // Tells the server to return XML instead of HTML
            request.ContentType = "application/xml"; // The request is an XML document
            request.SendChunked = false;             // Send it all as one request
            request.UserAgent = UserAgent;
            request.Headers.Add(HttpRequestHeader.Authorization, AuthorizationHeaderValue);
            request.Method = method.ToString().ToUpper();

            System.Diagnostics.Debug.WriteLine(String.Format("Recurly: Requesting {0} {1}",
                request.Method, request.RequestUri.ToString()));

            if ((method == HttpRequestMethod.Post || method == HttpRequestMethod.Put) && (writeXmlDelegate != null))
            {
                // 60 second timeout -- some payment gateways (e.g. PayPal) can take a while to respond
                request.Timeout = 60000;

                // Write POST/PUT body
                using (Stream requestStream = request.GetRequestStream())
                    WritePostParameters(requestStream, writeXmlDelegate);
            }

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return ReadWebResponse(response, readXmlDelegate);
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    HttpWebResponse response = (HttpWebResponse)ex.Response;
                    HttpStatusCode statusCode = response.StatusCode;
                    RecurlyError[] errors;

                    System.Diagnostics.Debug.WriteLine(String.Format("Recurly Library Received: {0} - {1}",
                        (int)statusCode, statusCode.ToString()));

                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.OK:
                        case HttpStatusCode.Accepted:
                        case HttpStatusCode.Created:
                        case HttpStatusCode.NoContent:
                            return ReadWebResponse(response, readXmlDelegate);

                        case HttpStatusCode.NotFound:
                            errors = RecurlyError.ReadResponseAndParseErrors(response);
                            if (errors.Length >= 0)
                                throw new NotFoundException(errors[0].Message, errors);
                            else
                                throw new NotFoundException("The requested object was not found.", errors);

                        case HttpStatusCode.Unauthorized:
                        case HttpStatusCode.Forbidden:
                            errors = RecurlyError.ReadResponseAndParseErrors(response);
                            throw new InvalidCredentialsException(errors);

                        case HttpStatusCode.PreconditionFailed:
                            errors = RecurlyError.ReadResponseAndParseErrors(response);
                            throw new ValidationException(errors);

                        case HttpStatusCode.ServiceUnavailable:
                            throw new TemporarilyUnavailableException();

                        case HttpStatusCode.InternalServerError:
                            errors = RecurlyError.ReadResponseAndParseErrors(response);
                            throw new RecurlyServerException(errors);
                    }

                    if ((int)statusCode == ValidationException.HttpStatusCode) // Unprocessable Entity
                    {
                        errors = RecurlyError.ReadResponseAndParseErrors(response);
                        throw new ValidationException(errors);
                    }
                }

                throw;
            }
        }
 public static HttpStatusCode PerformRequest(string appName, HttpRequestMethod method, string urlPath,
     WriteXmlDelegate writeXmlDelegate)
 {
     return PerformRequest(appName, method, urlPath, writeXmlDelegate, null);
 }
        public override void scan(int sleepTime)
        {
            DriverWrapper driverWrapper = null;

            int idx = -1;

            if (driverWrapper == null)
            {
                string agentString = "--user-agent=\"Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533/1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1\"";
                driverWrapper = DriverFactory.getDriverWaiter(BrowserAutomation.DriverFactory.Browser.Chrome, agentString);
            }

            int    badLoopCounter = 0;
            string botID          = System.Guid.NewGuid().ToString();

            while (true)
            {
                idx++;
                try
                {
                    if (driverWrapper == null)
                    {
                        string agentString = "--user-agent=\"Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533/1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1\"";

                        driverWrapper = driverCreator.CreateDriver(agentString);

                        if (driverWrapper == null)
                        {
                            log.Error("Failed to make a Selenium Driver");
                            continue;
                        }
                    }

                    //load the main page
                    driverWrapper.Url = "https://mobile.bet365.com/premium/#type=InPlay;key=1;ip=1;lng=1";
                    //driverWrapper.Url = "https://mobile.bet365.com/premium/#type=Splash;key=1;ip=0;lng=1";
                    bool inPlayElement = false;

                    try
                    {
                        inPlayElement = driverWrapper.WaitUntil(ExpectedBotCondition.VerifyInplayScreen(), 60);
                    }
                    catch (WebDriverTimeoutException)
                    {
                        log.Debug("No games in play, going to sleep for a bit....");
                        driverWrapper.Quit();
                        driverWrapper.Dispose();
                        driverWrapper = null;
                        continue;
                    }

                    List <IWebElement> fixtureElements = null;

                    if (inPlayElement)
                    {
                        fixtureElements = driverWrapper.FindElements(By.ClassName("Fixture")).ToList();
                        int removed = fixtureElements.RemoveAll(x => x.GetAttribute("class") != "Fixture");

                        log.Warn("Fixtures: " + fixtureElements.Count);
                        log.Warn("Removed: " + removed);

                        var fixtureList = driverWrapper.FindElement(By.ClassName("FixtureList"));
                        var fText       = fixtureList.Text;

                        var fixtureSplits = Regex.Split(fText, "\r\n").ToList();

                        fixtureSplits.RemoveAll(x => excludeString.Contains(x.ToUpper()));

                        var competitionName = "";
                        competitions.Clear();

                        while (fixtureSplits.Count() != 0)
                        {
                            var tempBuf = new List <string>();

                            fixtureSplits.RemoveAll(x => x == "VIDEO");

                            Game a = null;

                            while (fixtureSplits.Count() != 0)
                            {
                                if (fixtureSplits.First().StartsWith("  "))
                                {
                                    tempBuf.Add(Chomp(fixtureSplits));
                                    break;
                                }
                                else
                                {
                                    tempBuf.Add(Chomp(fixtureSplits));
                                }
                            }

                            if (tempBuf.Count() == 2)
                            {
                                var team1 = tempBuf[0];
                                var team2 = tempBuf[1];

                                if (team1.Contains(":"))
                                {
                                    team1 = team1.Substring(team1.IndexOf(' '));
                                }

                                a = new Game();
                                a.competitionName = competitionName.Trim();
                                a.team1           = team1.Trim();
                                a.team2           = team2.Trim();
                            }
                            else if (tempBuf.Count() == 3)
                            {
                                competitionName = tempBuf[0];
                                var team1 = tempBuf[1];
                                var team2 = tempBuf[2];

                                if (team1.Contains(":"))
                                {
                                    team1 = team1.Substring(team1.IndexOf(' '));
                                }

                                a = new Game();
                                a.competitionName = competitionName.Trim();
                                a.team1           = team1.Trim();
                                a.team2           = team2.Trim();
                            }
                            else
                            {
                                log.Info("Unexpected number of string in temp buf");
                            }

                            if (a != null)
                            {
                                competitions.Add(a);
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }

                    if (idx == -1)
                    {
                        Random random = new Random();
                        idx = random.Next(0, fixtureElements.Count());
                    }

                    int elementCount = 0;

                    fixtureElements.ForEach(x =>
                    {
                        if (idx == elementCount)
                        {
                            log.Warn(x.Text);
                        }
                        else
                        {
                        }
                        // log.Debug(x.Text);
                        ++elementCount;
                    });

                    log.Info("Scanning game " + idx + " of " + fixtureElements.Count() + " games in play at " + DateTime.Now.ToUniversalTime());

                    if (idx < fixtureElements.Count())
                    {
                        var hstats = new Dictionary <string, int>();
                        var astats = new Dictionary <string, int>();

                        int attempts = 3;

                        fixtureElements.ElementAt(idx).Click();

                        string clockText = "";

                        try
                        {
                            bool clockIsOnScreen = driverWrapper.WaitUntil(ExpectedBotCondition.ThereIsAClock(), 20);

                            if (clockIsOnScreen)
                            {
                                var clock = driverWrapper.FindElement(By.Id("mlClock"));
                                if (clock.Text.Contains(':'))
                                {
                                    clockText = clock.Text;
                                }
                            }

                            if (String.IsNullOrEmpty(clockText))
                            {
                                log.Error("No time avaiable!!!!");
                                throw new Exception();
                            }
                        }
                        catch (Exception vr)
                        {
                            log.Warn("cleanScores == null -  " + vr);

                            ++badLoopCounter;

                            if (badLoopCounter == 5)
                            {
                                log.Warn("Bad loop counter reset...");

                                badLoopCounter = 0;
                                driverWrapper.Quit();
                                driverWrapper.Dispose();
                                driverWrapper = null;
                            }

                            continue;
                        }

                        try
                        {
                            driverWrapper.WaitUntil(ExpectedBotCondition.ThereIsAnIdWithAttributeWithValue("arena", "style", "height: 144px;"), 5);
                        }
                        catch
                        {
                            log.Warn("Couldn't find stats areas, continuing....");
                            continue;
                        }

                        System.Threading.Thread.Sleep(400);

                        IJavaScriptExecutor js = driverWrapper.Driver as IJavaScriptExecutor;
                        js.ExecuteScript("document.getElementsByClassName('carousel')[0].setAttribute('style', '-webkit-transform: translate(-50%, 0px);')");

                        System.Threading.Thread.Sleep(400);

                        string hCardsAndCornersText = "";
                        string aCardsAndCornersText = "";

                        Func <IWebDriver, bool> f1 = driver =>
                        {
                            ReadOnlyCollection <IWebElement> elems = null;

                            try
                            {
                                elems = driver.FindElements(By.Id("team1IconStats"));
                            }
                            catch { }

                            if (elems.Count != 0)
                            {
                                hCardsAndCornersText = elems.First().Text;
                            }
                            return(hCardsAndCornersText.Split(' ').Count() == 3);
                        };

                        Func <IWebDriver, bool> f2 = driver =>
                        {
                            ReadOnlyCollection <IWebElement> elems = null;

                            try
                            {
                                elems = driver.FindElements(By.Id("team2IconStats"));
                            }
                            catch { }

                            if (elems.Count != 0)
                            {
                                aCardsAndCornersText = elems.First().Text;
                            }
                            return(aCardsAndCornersText.Split(' ').Count() == 3);
                        };

                        try
                        {
                            driverWrapper.WaitUntilConditionIsTrue(f1, 2);
                            driverWrapper.WaitUntilConditionIsTrue(f2, 2);
                        }
                        catch
                        {
                            try
                            {
                                js.ExecuteScript("document.getElementsByClassName('carousel')[0].setAttribute('style', '-webkit-transform: translate(-50%, 0px);')");
                                driverWrapper.WaitUntilConditionIsTrue(f1, 3);
                                driverWrapper.WaitUntilConditionIsTrue(f2, 3);
                            }
                            catch { }
                        }

                        if (hCardsAndCornersText == "" ||
                            aCardsAndCornersText == "")
                        {
                            log.Warn("hCardsAndCorners == null");
                            log.Warn("Resetting driver...");

                            ++badLoopCounter;

                            if (badLoopCounter == 5)
                            {
                                badLoopCounter = 0;
                                driverWrapper.Quit();
                                driverWrapper.Dispose();
                                driverWrapper = null;
                            }

                            continue;
                        }

                        var inPlayTitles = driverWrapper.GetValuesByClassName("EventViewTitle", attempts, 1, new char[] { '@' });
                        if (inPlayTitles == null)
                        {
                            log.Warn("inPlayTitles == null"); continue;
                        }

                        bool rballOkay = true;

                        List <string> shotsOnTarget    = null;
                        List <string> shotsOffTarget   = null;
                        List <string> attacks          = null;
                        List <string> dangerousAttacks = null;

                        shotsOnTarget = driverWrapper.GetValuesById("stat1", attempts, 3, "\r\n");

                        if (shotsOnTarget == null)
                        {
                            IWebElement noStats = driverWrapper.FindElement(By.Id("noStats"));
                            if (noStats != null)
                            {
                                log.Debug("shotsOnTarget == null Message: " + noStats.Text);
                            }
                            else
                            {
                                log.Warn("shotsOnTarget == null Expected no statistics but it's not displayed for some other reason");
                            }

                            rballOkay = false;
                        }

                        if (rballOkay == true)
                        {
                            shotsOffTarget = driverWrapper.GetValuesById("stat2", attempts, 3, "\r\n");
                            if (shotsOffTarget == null)
                            {
                                log.Warn("shotsOffTarget == null"); rballOkay = false;
                            }

                            attacks = driverWrapper.GetValuesById("stat3", attempts, 3, "\r\n");
                            if (attacks == null)
                            {
                                log.Warn("attacks == null"); rballOkay = false;
                            }

                            dangerousAttacks = driverWrapper.GetValuesById("stat4", attempts, 3, "\r\n");
                            if (dangerousAttacks == null)
                            {
                                log.Warn("dangerousAttacks == null"); rballOkay = false;
                            }
                        }

                        string inPlayTitle = inPlayTitles.ElementAt(0);

                        if (inPlayTitle.Contains("\r\n"))
                        {
                            inPlayTitle = inPlayTitle.Substring(0, inPlayTitle.IndexOf("\r\n"));
                        }

                        var vals = new List <string>();

                        Action <Dictionary <string, int>, StatAlias, string, int> setStat =
                            (Dictionary <string, int> d, StatAlias alias, string val, int at) =>
                        {
                            string statString = stat(alias);
                            d[statString] = ParseInt(statString, val);
                        };

                        Action <Dictionary <string, int>, StatAlias[], List <string> > setStat2 =
                            (Dictionary <string, int> d, StatAlias[] alias, List <string> list) =>
                        {
                            for (int i = 0; i < alias.Length; ++i)
                            {
                                string statString = stat(alias[i]);
                                d[statString] = ParseInt(statString, list.ElementAt(i));
                            }
                        };

                        StatAlias[] aliases = { StatAlias.RedCards, StatAlias.YellowCards, StatAlias.Corners };

                        setStat2(hstats, aliases, hCardsAndCornersText.Split(' ').ToList());
                        setStat2(astats, aliases, aCardsAndCornersText.Split(' ').ToList());

                        if (rballOkay)
                        {
                            aliases = new StatAlias[] { StatAlias.ShotsOnTarget, StatAlias.ShotsOffTarget, StatAlias.Attacks, StatAlias.DangerousAttacks };

                            Func <List <string>, string> h = x => x.ElementAt(0);

                            setStat2(hstats, aliases, new List <string> {
                                h(shotsOnTarget), h(shotsOffTarget), h(attacks), h(dangerousAttacks)
                            });

                            Func <List <string>, string> a = x => x.ElementAt(2);

                            setStat2(astats, aliases,
                                     new List <string> {
                                a(shotsOnTarget), a(shotsOffTarget), a(attacks), a(dangerousAttacks)
                            });
                        }

                        var team1score = driverWrapper.FindElement(By.Id("team1score")).Text;
                        var team2score = driverWrapper.FindElement(By.Id("team2score")).Text;
                        setStat(hstats, StatAlias.Goals, team1score, 0);
                        setStat(astats, StatAlias.Goals, team2score, 1);

                        var teams = Regex.Split(inPlayTitle, " v ");

                        string homeTeamName = DoSubstitutions(teams.ElementAt(0));
                        string awayTeamName = DoSubstitutions(teams.ElementAt(1));

                        string today = DateTime.Now.ToUniversalTime().ToString("ddMMyy");

                        Game maybeGame = null;

                        try
                        {
                            maybeGame = competitions.SingleOrDefault(x => x.team1.ToUpper().StartsWith(homeTeamName.ToUpper()) && x.team2.ToUpper().StartsWith(awayTeamName.ToUpper()));
                        }
                        catch (Exception ce)
                        {
                            log.Warn("Exception thrown in your shit code!:" + ce);
                        }

                        string league = "All";

                        if (maybeGame != null)
                        {
                            league = DoSubstitutions(maybeGame.competitionName);
                        }

                        string yesterday = (DateTime.Today.ToUniversalTime() - TimeSpan.FromDays(1)).ToString("ddMMyy");

                        string finalName = "";

                        try
                        {
                            finalName = Path.Combine(xmlPath, league, homeTeamName + " v " + awayTeamName + "_" + today + ".xml");
                        }
                        catch (Exception ce)
                        {
                            log.Warn("Another exception thrown in your shit code!:" + ce);
                            throw ce;
                        }

                        bool exists = File.Exists(finalName);

                        //edge case of games going over midnight
                        bool bOverMidnight = false;

                        if (exists == false)
                        {
                            string anotherName = Path.Combine(xmlPath, league, homeTeamName + " v " + awayTeamName + "_" + yesterday + ".xml");
                            if (File.Exists(anotherName))
                            {
                                finalName     = anotherName;
                                exists        = true;
                                bOverMidnight = true;
                            }
                        }

                        SendToWebDelegate sd = new SendToWebDelegate(SendToWeb);
                        sd.BeginInvoke(league, bOverMidnight ? DateTime.Today.ToUniversalTime() - TimeSpan.FromDays(1) : DateTime.Now.ToUniversalTime(), homeTeamName, awayTeamName, hstats, astats, clockText, null, null);
                        //SendToWeb(league, bOverMidnight ? DateTime.Today - TimeSpan.FromDays(1) : DateTime.Now, homeTeamName, awayTeamName, hstats, astats, clockText);

                        WriteXmlDelegate wd = new WriteXmlDelegate(WriteXml);
                        wd.BeginInvoke(xmlPath, hstats, astats, homeTeamName, awayTeamName, league, clockText, exists, finalName, null, null);
                    }
                    else
                    {
                        idx = -1;
                    }
                }
                catch (System.Net.WebException we)
                {
                    log.Warn("Caught Web Exception: " + we);
                    continue;
                }
                catch (OpenQA.Selenium.WebDriverException we)
                {
                    log.Error("Exception thrown: " + we);
                    if (driverWrapper != null)
                    {
                        driverWrapper.Quit();
                        driverWrapper.Dispose();
                        driverWrapper = null;
                    }
                }
                catch (Exception we)
                {
                    log.Error("Exception thrown: " + we);
                    if (driverWrapper != null)
                    {
                        driverWrapper.Quit();
                        driverWrapper.Dispose();
                        driverWrapper = null;
                    }
                }
            }
        }