示例#1
0
        private void RefreshFromServer(int gameId, IFileSystemService fileSystemService, IHttpRequestService httpRequestService)
        {
            var file = Path.Combine(_settings.DumpDirectory, String.Format("{0}.json", gameId));

            if (fileSystemService.FileExists(file))
            {
                bool fileValid = (DateTime.Now - fileSystemService.GetFileLastModified(file)) < TimeSpan.FromHours(16);
                if (fileValid)
                {
                    return;
                }
            }

            Debug.WriteLine(String.Format("{0} fetching patch data {1}", DateTime.Now, gameId));
            var url = String.Format("http://retroachievements.org/dorequest.php?u={0}&t={1}&g={2}&h=1&r=patch",
                                    _settings.UserName, _settings.DoRequestToken, gameId);
            var request  = new HttpRequest(url);
            var response = httpRequestService.Request(request);

            if (response.Status == System.Net.HttpStatusCode.OK)
            {
                using (var outputStream = fileSystemService.CreateFile(file))
                {
                    byte[] buffer = new byte[4096];
                    using (var stream = response.GetResponseStream())
                    {
                        int bytesRead;
                        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            if (bytesRead < 100)
                            {
                                var str = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                                if (str.Contains("Error"))
                                {
                                    if (_progress != null)
                                    {
                                        _progress.IsEnabled = false;
                                        _progress.Label     = String.Empty;
                                    }
                                    _backgroundWorkerService.InvokeOnUiThread(() => MessageBoxViewModel.ShowMessage(str));
                                    return;
                                }
                            }
                            outputStream.Write(buffer, 0, bytesRead);
                        }
                    }
                }

                string contents;
                using (var stream = new StreamReader(fileSystemService.OpenFile(file, OpenFileMode.Read)))
                {
                    contents = stream.ReadToEnd();
                }
            }
        }
示例#2
0
        private string GetPage(string filename, string url, bool requiresCookie)
        {
            bool fileValid = false;

            if (_fileSystemService.FileExists(filename))
            {
                var expireHours = ExpireHours;
                if (expireHours == 0)
                {
                    expireHours = 16;
                }

                fileValid = (DateTime.Now - _fileSystemService.GetFileLastModified(filename)) < TimeSpan.FromHours(expireHours);
            }

            if (!fileValid)
            {
                var request = new HttpRequest(url);
                if (requiresCookie)
                {
                    var settings = ServiceRepository.Instance.FindService <ISettings>();
                    if (String.IsNullOrEmpty(settings.Cookie) || String.IsNullOrEmpty(settings.UserName))
                    {
                        return(null);
                    }
                    request.Headers["Cookie"] = String.Format("RA_User={0}; RA_Cookie={1}", settings.UserName, settings.Cookie);
                }

                var response = _httpRequestService.Request(request);
                if (response.Status != System.Net.HttpStatusCode.OK)
                {
                    return(null);
                }

                using (var outputStream = _fileSystemService.CreateFile(filename))
                {
                    byte[] buffer = new byte[4096];
                    using (var stream = response.GetResponseStream())
                    {
                        int bytesRead;
                        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            outputStream.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            }

            using (var stream = new StreamReader(_fileSystemService.OpenFile(filename, OpenFileMode.Read)))
            {
                return(stream.ReadToEnd());
            }
        }
示例#3
0
        private string GetPage(string filename, string url)
        {
            bool fileValid = false;

            if (_fileSystemService.FileExists(filename))
            {
                fileValid = (DateTime.Now - _fileSystemService.GetFileLastModified(filename)) < TimeSpan.FromHours(16);
            }

            if (!fileValid)
            {
                var request  = new HttpRequest(url);
                var response = _httpRequestService.Request(request);
                if (response.Status != System.Net.HttpStatusCode.OK)
                {
                    return(null);
                }

                using (var outputStream = _fileSystemService.CreateFile(filename))
                {
                    byte[] buffer = new byte[4096];
                    using (var stream = response.GetResponseStream())
                    {
                        int bytesRead;
                        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            outputStream.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            }

            using (var stream = new StreamReader(_fileSystemService.OpenFile(filename, OpenFileMode.Read)))
            {
                return(stream.ReadToEnd());
            }
        }