コード例 #1
0
ファイル: GeoNamesClient.cs プロジェクト: detmach/SharpIrcBot
        public async Task <PostCodeSearchResult> SearchForPostCode(string postCodeString)
        {
            Match postCodeMatch = PostCodeRegex.Match(postCodeString);

            if (!postCodeMatch.Success)
            {
                return(null);
            }

            string country       = postCodeMatch.Groups["country"].Value;
            string countryAlpha2 = CountryCodeToAlpha2(country);

            if (countryAlpha2 == null)
            {
                return(null);
            }

            string postCode = postCodeMatch.Groups["postcode"].Value;

            var postCodeSearchResult = new PostCodeSearchResult();

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.ConnectionClose = true;

                var postCodeSearchUri = new Uri(
                    $"http://api.geonames.org/postalCodeSearchJSON?postalcode={WebUtility.UrlEncode(postCode)}&country={WebUtility.UrlEncode(countryAlpha2)}&maxRows=1&username={WebUtility.UrlEncode(Config.Username)}"
                    );
                string postCodeSearchResponse = await client.GetStringAsync(postCodeSearchUri);

                PopulateWithJsonString(postCodeSearchResult, postCodeSearchResponse);
            }
            return(postCodeSearchResult);
        }
コード例 #2
0
ファイル: GeoNamesClient.cs プロジェクト: detmach/SharpIrcBot
        public async Task <GeoName> GetFirstGeoName(string query)
        {
            PostCodeSearchResult postCodes = await SearchForPostCode(query);

            if (postCodes != null)
            {
                return(postCodes.PostCodeEntries.Any()
                    ? postCodes.PostCodeEntries[0]
                    : null);
            }

            GeoSearchResult result = await SearchForLocation(query);

            if (result == null)
            {
                return(null);
            }

            return(result.GeoNames.Any()
                ? result.GeoNames[0]
                : null);
        }