Ejemplo n.º 1
0
        public void DownloadMtgjson()
        {
            Console.WriteLine("GET {0}", _mtgjsonSourceConfig.Url);

            try
            {
                var responseStream = _webClient.DownloadStream(_mtgjsonSourceConfig.Url);
                if (responseStream == null)
                {
                    Console.WriteLine("Failed to send request to mtgjson.com: empty response");
                    Console.WriteLine();
                    return;
                }

                using (responseStream)
                {
                    var byteArray = responseStream.ReadAllBytes();
                    Console.WriteLine("Downloading complete.");

                    using (var stream = new MemoryStream(byteArray))
                    {
                        Console.WriteLine("Extracting to {0}", AppDir.Data);
                        new FastZip().ExtractZip(
                            stream,
                            AppDir.Data,
                            FastZip.Overwrite.Always,
                            name => true,
                            fileFilter: null,
                            directoryFilter: null,
                            restoreDateTime: true,
                            isStreamOwner: false);
                    }

                    MtgjsonFileUpdated?.Invoke();

                    Console.WriteLine("Done. On next start new cards will be loaded and full-text index will be rebuilt.");
                    Console.WriteLine();
                }
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("Failed to send request to mtgjson.com: {0}", string.Join(", ", ex.InnerExceptions.Select(_ => _.Message)));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to send request to mtgjson.com: {0}", ex.Message);
            }
        }
Ejemplo n.º 2
0
        private async Task downloadDataZip(string url, CancellationToken token)
        {
            Console.WriteLine("GET {0}", url);

            try
            {
                var stream = await _webClient.DownloadStream(url, token);

                if (stream == null)
                {
                    Console.WriteLine("Failed request to mtgjson.com: empty response");
                    Console.WriteLine();
                    return;
                }

                Console.WriteLine("Downloading complete.");
                using (stream)
                {
                    Console.WriteLine("Extracting to {0}", AppDir.Data);
                    new FastZip().ExtractZip(
                        stream,
                        AppDir.Data.Value,
                        FastZip.Overwrite.Always,
                        name => true,
                        fileFilter: null,
                        directoryFilter: null,
                        restoreDateTime: true,
                        isStreamOwner: false);

                    MtgjsonFileUpdated?.Invoke();

                    Console.WriteLine("Done. New data will be available after restart.");
                    Console.WriteLine();
                }
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("Failed request to mtgjson.com: {0}",
                                  string.Join(", ", ex.InnerExceptions.Select(_ => _.Message)));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed request to mtgjson.com: {0}", ex.Message);
            }
        }