コード例 #1
0
    public async IAsyncEnumerable <WeissSchwarzCard> Parse(string url, IProgress <SetParserProgressReport> progress, [EnumeratorCancellation] CancellationToken cancellationToken)
    {
        Log.Information("Starting. URI: {url}", url);
        string textToProcess = null;

        var progressReport = new SetParserProgressReport
        {
            ReportMessage = new MultiLanguageString {
                EN = $"Starting with URI: [{url}]"
            },
            Percentage = 0
        };

        progress.Report(progressReport);

        if (Uri.TryCreate(url, UriKind.Absolute, out var uri) && uri.IsWellFormedOriginalString())
        {
            var html        = await new Uri(url).DownloadHTML(cancellationToken);
            var preSelector = "td > pre";
            textToProcess = html.QuerySelector(preSelector).TextContent;
        }
        else
        {
            var path = Path.Get(url);
            textToProcess = await path.ReadStringAsync(cancellationToken);
        }

        cancellationToken.ThrowIfCancellationRequested();
        progressReport = progressReport with
        {
            ReportMessage = new MultiLanguageString {
                EN = $"Finished obtaining text: [{url}]"
            },
            Percentage = 10
        };
        progress.Report(progressReport);

        var majorSeparator = "================================================================================";
        var textSplits     = textToProcess.Split(majorSeparator);
        var rows           = textSplits.Length - 2;
        var results        = textSplits.AsEnumerable()
                             .Skip(1)
                             .SkipLast(1)
                             .Select((section, index) => (index, card: ParseHOTCText(section)))
        ;

        foreach (var cardPair in results)
        {
            cancellationToken.ThrowIfCancellationRequested();
            progressReport = progressReport.WithParsedSerial(cardPair.card, rows);
            progress.Report(progressReport);
            yield return(cardPair.card);
        }

        progressReport = progressReport with
        {
            ReportMessage = new MultiLanguageString {
                EN = $"Parsed all cards."
            },
            Percentage = 100
        };
        progress.Report(progressReport);
        yield break;
    }
コード例 #2
0
    public async IAsyncEnumerable <WeissSchwarzCard> Parse(string urlOrLocalFile, IProgress <SetParserProgressReport> progress, [EnumeratorCancellation] CancellationToken cancellationToken)
    {
        if (encoreDecksSiteSetMatcher.IsMatch(urlOrLocalFile))
        {
            urlOrLocalFile = TransformIntoAPIFormat(urlOrLocalFile);
        }

        IList <dynamic> setCards       = null;
        var             progressReport = new SetParserProgressReport();

        do
        {
            try
            {
                progressReport = progressReport with
                {
                    ReportMessage = new MultiLanguageString
                    {
                        EN = "Obtaining list of cards..."
                    },
                    Percentage = 1
                };
                progress.Report(progressReport);
                setCards = await urlOrLocalFile.WithRESTHeaders().GetJsonListAsync(cancellationToken);
            }
            catch (FlurlHttpException)
            {
                // Do nothing
            }
        } while (setCards == null);

        progressReport = progressReport with {
            ReportMessage = new MultiLanguageString {
                EN = $"Obtained [{setCards.Count}] cards."
            },
            Percentage = 10
        };
        progress.Report(progressReport);

        foreach (var setCard in setCards)
        {
            cancellationToken.ThrowIfCancellationRequested();
            WeissSchwarzCard result = new WeissSchwarzCard();
            result.Name = new MultiLanguageString();
            var enOptional = DynamicExtensions.AsOptional(setCard.locale.EN);
            var jpOptional = DynamicExtensions.AsOptional(setCard.locale.NP);
            if (((string)enOptional.source)?.ToLower() != "akiba")
            {
                result.Name.EN = enOptional.name;
            }
            result.Name.JP = jpOptional.name;
            (List <object>, List <object>)attributes = (enOptional.attributes, jpOptional.attributes);
            result.Traits   = TranslateTraits(attributes).ToList();
            result.Effect   = ((List <object>)enOptional.ability)?.Cast <string>().ToArray();
            result.Rarity   = setCard.rarity;
            result.Side     = TranslateSide(setCard.side);
            result.Level    = (int?)setCard.level;
            result.Cost     = (int?)setCard.cost;
            result.Power    = (int?)setCard.power;
            result.Soul     = (int?)setCard.soul;
            result.Triggers = TranslateTriggers(setCard.trigger);

            //result.Serial = setCard.cardcode;
            if (!String.IsNullOrEmpty(setCard.imagepath))
            {
                result.Images.Add(new Uri($"https://www.encoredecks.com/images/{setCard.imagepath}"));
            }

            // TODO: Delete all methods related with generating serial.
            // TODO: Switch once LLDX checkbox is checked properly. See: https://trello.com/c/WCT94Sk0/2-card-code-needs-to-be-stored-seperatly-from-side-release
            result.Serial = WeissSchwarzCard.GetSerial(setCard.set.ToString(), setCard.side.ToString(), setCard.lang.ToString(), setCard.release.ToString(), setCard.sid.ToString());

            result.Type    = TranslateType(setCard.cardtype);
            result.Color   = TranslateColor(setCard.colour);
            result.Remarks = $"Parsed: {this.GetType().Name}";

            progressReport = progressReport with
            {
                ReportMessage = new MultiLanguageString {
                    EN = $"Parsed [{result.Serial}]."
                },
                Percentage  = 10 + (int)((progressReport.CardsParsed + 1f) * 90 / setCards.Count),
                CardsParsed = progressReport.CardsParsed + 1
            };
            progress.Report(progressReport);

            yield return(result);
        }
        yield break;
    }