public static async Task <PokemonAttributes> SearchPokemon(bool isPremium, int numberPokemon) { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); _sayLog = LogManager.GetLogger("SERVIDOR COORDENADAS:"); using (var downloadData = new WebClient()) { var allPokemon = string.Empty; if (isPremium) { allPokemon = await downloadData.DownloadStringTaskAsync(ApiPremium); _sayLog.Info("Estamos carregando a lista. Aguarde..."); } else { _freeUserKey = await PokemonRaro.PokemonRaro.PokedexApiFree(); allPokemon = await downloadData.DownloadStringTaskAsync(string.Format( $"http://api.pokedex100.com/feedlink/{_freeUserKey},candies,{numberPokemon}")); } if (!CheckReceivedData(allPokemon).Result) { return(null); } var splitPokemonByLine = allPokemon.Split(new[] { "\n" }, StringSplitOptions.None).ToList(); splitPokemonByLine.RemoveAt(0); var pokemonList = new PokemonAttributes { PokemonAtribbutes = new PokemonAtribbute[splitPokemonByLine.Count] }; //Regex search settings const string regexCoordinates = @"-?\d{1,2}.\d{6},-?\d{1,3}.\d{6}"; const string regexIv = @"iv((\d{1,3})|\?)"; const string regexCP = @"cp((\d{1,4})|\?)"; const string regexTimeLeft = @"\(\d{1,2}:\d{1,2}\)"; const string regexGetNumber = @",\d{1,3},"; for (var i = 0; i < splitPokemonByLine.Count; i++) { var numPokemon = Regex.Match(splitPokemonByLine[i], regexGetNumber).Value; numPokemon = numPokemon.Remove(0, 1); numPokemon = numPokemon.Remove(numPokemon.Length - 1, 1); var nomePokemon = Enum.GetName(typeof(PokemonId), int.Parse(numPokemon)); var coordinates = Regex.Match(splitPokemonByLine[i], regexCoordinates).Value; var getLatLong = coordinates.Split(','); if (!getLatLong[0].Contains('.')) { continue; } else if (!getLatLong[1].Contains('.')) { continue; } var getIv = Regex.Match(splitPokemonByLine[i], regexIv).Value; if (string.IsNullOrEmpty(getIv)) { getIv = "?"; } else { getIv = getIv.Remove(0, 2); } var getCp = Regex.Match(splitPokemonByLine[i], regexCP).Value; if (string.IsNullOrEmpty(getCp)) { getCp = "?"; } else { getCp = getCp.Remove(0, 2); } var getTimeLeft = Regex.Match(splitPokemonByLine[i], regexTimeLeft).Value; var country = string.Empty; GeoLocation geoLoc = new GeoLocation(); geoLoc.Latitude = 0; geoLoc.Longitude = 0; try { geoLoc.Latitude = double.Parse(getLatLong[0]); geoLoc.Longitude = double.Parse(getLatLong[1]); _sayLog.Fatal($"lat: {geoLoc.Latitude} long: {geoLoc.Longitude}"); var teste = new CountryReverseGeocodeService(); country = teste.FindCountry(geoLoc).Id; country = country.ToLower(); } catch (Exception e) { _sayLog.Error(e.Message + " " + e.StackTrace); } pokemonList.PokemonAtribbutes[i] = new PokemonAtribbute(); pokemonList.PokemonAtribbutes[i].Name = nomePokemon; pokemonList.PokemonAtribbutes[i].Number = numPokemon; pokemonList.PokemonAtribbutes[i].Country = country ?? "bra"; pokemonList.PokemonAtribbutes[i].Iv = getIv; pokemonList.PokemonAtribbutes[i].Cp = getCp; pokemonList.PokemonAtribbutes[i].TimeLeft = getTimeLeft; pokemonList.PokemonAtribbutes[i].Latitude = geoLoc.Latitude; pokemonList.PokemonAtribbutes[i].Longitude = geoLoc.Longitude; } return(pokemonList); } }
public static string ToJson(this PokemonAttributes self) { return(JsonConvert.SerializeObject(self, Converter.Settings)); }