public async Task <WebDataExtractionResult> ExtractAsync(WurmServerInfo serverInfo)
        {
            WebDataExtractionResult result = new WebDataExtractionResult(serverInfo.ServerName);

            var res = await httpWebRequests.GetResponseAsync(serverInfo.WebStatsUrl).ConfigureAwait(false);

            DateTime headerLastUpdated = res.LastModified;

            using (Stream stream = res.GetResponseStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    bool   canReadServerName = false;
                    bool   canReadUptime     = false;
                    bool   canReadWurmTime   = false;
                    string line;
                    string strUptime       = string.Empty;
                    string strWurmDateTime = string.Empty;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (canReadServerName)
                        {
                            Match  match = Regex.Match(line, @">.+<");
                            string name  = match.Value.Substring(1, match.Value.Length - 2);
                            if (!string.Equals(name, serverInfo.ServerName.Original, StringComparison.InvariantCultureIgnoreCase))
                            {
                                throw new WurmApiException(
                                          string.Format(
                                              "Extracted server name does not match server description, expected {0}, actual {1}",
                                              serverInfo.ServerName.Original,
                                              name));
                            }
                            canReadServerName = false;
                        }
                        else if (canReadUptime)
                        {
                            Match match = Regex.Match(line, @">.+<");
                            strUptime     = match.Value.Substring(1, match.Value.Length - 2);
                            canReadUptime = false;
                        }
                        else if (canReadWurmTime)
                        {
                            Match match = Regex.Match(line, @">.+<");
                            strWurmDateTime = match.Value.Substring(1, match.Value.Length - 2);
                            canReadWurmTime = false;
                        }

                        if (Regex.IsMatch(line, "Server name"))
                        {
                            canReadServerName = true;
                        }
                        else if (Regex.IsMatch(line, "Uptime"))
                        {
                            canReadUptime = true;
                        }
                        else if (Regex.IsMatch(line, "Wurm Time"))
                        {
                            canReadWurmTime = true;
                        }
                    }

                    DateTime dtnow = Time.Get.LocalNow;
                    if (headerLastUpdated > dtnow)
                    {
                        headerLastUpdated = dtnow;
                    }

                    TimeSpan uptime = GetTimeSpanFromUptimeWebString(strUptime);
                    result.ServerUptime = uptime;

                    WurmDateTime wdt = GetWurmDateTimeFromWdtWebString(strWurmDateTime);
                    result.WurmDateTime = wdt;

                    result.LastUpdated = headerLastUpdated;
                }
            }

            return(result);
        }
Beispiel #2
0
        public async Task<WebDataExtractionResult> ExtractAsync(WurmServerInfo serverInfo)
        {
            WebDataExtractionResult result = new WebDataExtractionResult(serverInfo.Name);

            var res = await this.httpWebRequests.GetResponseAsync(serverInfo.WebStatsUrl).ConfigureAwait(false);
            DateTime headerLastUpdated = res.LastModified;

            using (Stream stream = res.GetResponseStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    bool canReadServerName = false;
                    bool canReadUptime = false;
                    bool canReadWurmTime = false;
                    string line;
                    string strUptime = string.Empty;
                    string strWurmDateTime = string.Empty;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (canReadServerName)
                        {
                            Match match = Regex.Match(line, @">.+<");
                            string name = match.Value.Substring(1, match.Value.Length - 2);
                            if (!string.Equals(name, serverInfo.Name.Original, StringComparison.InvariantCultureIgnoreCase))
                            {
                                throw new WurmApiException(
                                    string.Format(
                                        "Extracted server name does not match server description, expected {0}, actual {1}",
                                        serverInfo.Name.Original,
                                        name));
                            }
                            canReadServerName = false;
                        }
                        else if (canReadUptime)
                        {
                            Match match = Regex.Match(line, @">.+<");
                            strUptime = match.Value.Substring(1, match.Value.Length - 2);
                            canReadUptime = false;
                        }
                        else if (canReadWurmTime)
                        {
                            Match match = Regex.Match(line, @">.+<");
                            strWurmDateTime = match.Value.Substring(1, match.Value.Length - 2);
                            canReadWurmTime = false;
                        }

                        if (Regex.IsMatch(line, "Server name"))
                        {
                            canReadServerName = true;
                        }
                        else if (Regex.IsMatch(line, "Uptime"))
                        {
                            canReadUptime = true;
                        }
                        else if (Regex.IsMatch(line, "Wurm Time"))
                        {
                            canReadWurmTime = true;
                        }
                    }

                    DateTime dtnow = Time.Get.LocalNow;
                    if (headerLastUpdated > dtnow)
                    {
                        headerLastUpdated = dtnow;
                    }

                    TimeSpan uptime = GetTimeSpanFromUptimeWebString(strUptime);
                    result.ServerUptime = uptime;

                    WurmDateTime wdt = GetWurmDateTimeFromWdtWebString(strWurmDateTime);
                    result.WurmDateTime = wdt;

                    result.LastUpdated = headerLastUpdated;
                }
            }

            return result;
        }