Ejemplo n.º 1
0
    public bool TryReadSerializedValue <TValue>(out TValue value, params string[] path)
    {
        if (path == null)
        {
            throw new ArgumentNullException(nameof(path));
        }

        var filename = Path.Combine(path);

        try
        {
            if (!filename.EndsWith(JsonExtension, StringComparison.Ordinal))
            {
                filename += JsonExtension;
            }

            if (!TryReadRawText(out var textValue, filename))
            {
                value = default;
                return(false);
            }

            value = _jsonSerializerService.Deserialize <TValue>(textValue);
            return(true);
        }
        catch (Exception exception)
        {
            _logger.LogWarning(exception, $"Error while reading serialized value '{filename}'.");

            value = default;
            return(false);
        }
    }
Ejemplo n.º 2
0
        void TryRegisterDefaultResources(string filename)
        {
            try
            {
                var json = File.ReadAllText(filename);

                // TODO: Refactor to use the resource service.
                var resources =
                    _jsonSerializerService.Deserialize <Dictionary <string, Dictionary <string, string> > >(json);
                if (resources == null)
                {
                    return;
                }

                foreach (var defaultResource in resources)
                {
                    if (defaultResource.Value == null)
                    {
                        continue;
                    }

                    foreach (var defaultResourceValue in defaultResource.Value)
                    {
                        if (defaultResourceValue.Value == null)
                        {
                            continue;
                        }

                        RegisterResource(defaultResource.Key, defaultResourceValue.Key, defaultResourceValue.Value);
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Error while registering default resources.");
            }
        }
Ejemplo n.º 3
0
    public static async Task Run()
    {
        var codes = await PartyCodeScraper.Run();

        var htmlPath          = Path.Combine(DataLocations.TempPath, "partycodes.html");
        var partyRegisterPath = Path.Combine(DataLocations.TempPath, "partyRegister.json");

        File.Delete(htmlPath);
        File.Delete(partyRegisterPath);
        var url = "https://www.aec.gov.au/parties_and_representatives/party_registration/Registered_parties/";

        try
        {
            await Downloader.DownloadFile(htmlPath, url);

            var jsonUrl = (await File.ReadAllLinesAsync(htmlPath))
                          .Single(x => x.Contains("/Parties_and_Representatives/Party_Registration/Registered_parties/files/register"))
                          .Split('"')[1];
            await Downloader.DownloadFile(partyRegisterPath, $"https://www.aec.gov.au{jsonUrl}");

            var          aecParties = JsonSerializerService.Deserialize <PartyData>(partyRegisterPath);
            List <Party> parties    = new();
            foreach (var detail in aecParties.Details)
            {
                var party = DetailToParty(detail, codes);
                parties.Add(party);
            }

            File.Delete(DataLocations.PartiesJsonPath);
            JsonSerializerService.Serialize(parties, DataLocations.PartiesJsonPath);
        }
        catch (Exception exception)
        {
            throw new($"Failed to parse {htmlPath} {htmlPath}", exception);
        }
    }