private async Task <WeissSchwarzDeck> Parse(Uri uri)
        {
            var encoreDecksDeckAPIURL = "https://www.encoredecks.com/api/deck";

            var localPath = uri.LocalPath;
            var deckID    = localPath.Substring(localPath.LastIndexOf('/') + 1);

            Log.Information("Deck ID: {deckID}", deckID);

            dynamic deckJSON = await GetDeckJSON(encoreDecksDeckAPIURL, deckID);

            WeissSchwarzDeck res = new WeissSchwarzDeck();

            res.Name = deckJSON.name;

            using (var db = _database())
            {
                await db.Database.MigrateAsync();

                foreach (dynamic card in deckJSON.cards)
                {
                    string           serial = WeissSchwarzCard.GetSerial(card.set.ToString(), card.side.ToString(), card.lang.ToString(), card.release.ToString(), card.sid.ToString());
                    WeissSchwarzCard wscard = await db.WeissSchwarzCards.FindAsync(serial);

                    if (wscard == null)
                    {
                        string setID = card.series;
                        await _parse($"https://www.encoredecks.com/api/series/{setID}/cards");

                        wscard = await db.WeissSchwarzCards.FindAsync(serial);
                    }

                    if (res.Ratios.TryGetValue(wscard, out int quantity))
                    {
                        res.Ratios[wscard]++;
                    }
                    else
                    {
                        res.Ratios[wscard] = 1;
                    }

                    //Log.Information("Parsed: {@wscard}", wscard);
                }
            }
            var simpleRatios = res.AsSimpleDictionary();

            Log.Information("Deck Parsed: {@simpleRatios}", simpleRatios);
            Log.Information("Cards in Deck: {@count}", simpleRatios.Values.Sum());
            return(res);
        }
Example #2
0
        public async Task Export(WeissSchwarzDeck deck, IExportInfo info)
        {
            Log.Information("Exporting as Deck JSON.");
            var jsonFilename   = Path.CreateDirectory(info.Destination).Combine($"deck_{deck.Name.AsFileNameFriendly()}.json");
            var simplifiedDeck = new
            {
                Name    = deck.Name,
                Remarks = deck.Remarks,
                Ratios  = deck.AsSimpleDictionary()
            };

            jsonFilename.Open(
                async s => await JsonSerializer.SerializeAsync(s, simplifiedDeck, options: _defaultOptions),
                System.IO.FileMode.Create,
                System.IO.FileAccess.Write,
                System.IO.FileShare.ReadWrite
                );
            Log.Information($"Done: {jsonFilename.FullPath}");

            if (!String.IsNullOrWhiteSpace(info.OutCommand))
            {
                await ExecuteCommandAsync(info.OutCommand, jsonFilename);
            }
        }
Example #3
0
        public async Task <WeissSchwarzDeck> Parse(string sourceUrlOrFile)
        {
            var document = await sourceUrlOrFile.WithHTMLHeaders().GetHTMLAsync();

            //var deckView = document.QuerySelector(".deckview");
            //var cardControllers = deckView.QuerySelectorAll(".card-controller-inner");
            var deckID = urlMatcher.Match(sourceUrlOrFile).Groups[2];

            Log.Information("Parsing ID: {deckID}", deckID);
            var response = await $"{deckLogApiUrlPrefix}{deckID}" //
                           .WithReferrer(sourceUrlOrFile)         //
                           .PostJsonAsync(null);
            var json = JsonConvert.DeserializeObject <dynamic>(await response.Content.ReadAsStringAsync());
            //var json = JsonConverter.CreateDefault().Deserialize<dynamic>(new JsonReader(await response.Content.ReadAsStreamAsync()));
            var newDeck        = new WeissSchwarzDeck();
            var missingSerials = new List <string>();

            newDeck.Name    = json.title.ToString();
            newDeck.Remarks = json.memo.ToString();
            using (var db = _database())
            {
                List <dynamic> items = new List <dynamic>();
                items.AddRange(json.list);
                items.AddRange(json.sub_list);
                foreach (var cardJSON in items)
                {
                    string serial = cardJSON.card_number.ToString();
                    serial = serial.Replace('+', '+');
                    if (serial == null)
                    {
                        Log.Warning("serial is null for some reason!");
                    }
                    var card = await db.WeissSchwarzCards.FindAsync(serial);

                    int quantity = cardJSON.num;
                    if (card != null)
                    {
                        Log.Debug("Adding: {card} [{quantity}]", card?.Serial, quantity);
                        if (newDeck.Ratios.TryGetValue(card, out int oldVal))
                        {
                            newDeck.Ratios[card] = oldVal + quantity;
                        }
                        else
                        {
                            var url = awsWeissSchwarzSitePrefix + cardJSON.img;
                            Log.Debug("Adding URL into Images: {url}", url);
                            card.Images.Add(new Uri(url));
                            newDeck.Ratios.Add(card, quantity);
                        }
                    }
                    else
                    {
                        missingSerials.Add(serial);
                        //throw new DeckParsingException($"MISSING_SERIAL_{serial}");
                        Log.Warning("Serial has been effectively skipped because it's not found on the local db: [{serial}]", serial);
                    }
                }
            }
            if (missingSerials.Count > 0)
            {
                throw new DeckParsingException($"The following serials are missing from the DB:\n{missingSerials.ConcatAsString("\n")}");
            }
            else
            {
                Log.Debug($"Result Deck: {JsonConvert.SerializeObject(newDeck.AsSimpleDictionary())}");
                return(newDeck);
            }
        }