<commit_before>package com.msc.serverbrowser.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.msc.serverbrowser.entities.SampServer;

public class ServerUtil
{
	public static List<SampServer> retrieveMasterlistServers(final String version)
	{
		final List<SampServer> servers = new ArrayList<>();
		try
		{
			final URLConnection openConnection = new URL("http://lists.sa-mp.com/" + version + "/servers").openConnection();
			openConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
			try (final BufferedReader in = new BufferedReader(new InputStreamReader(openConnection.getInputStream())))
			{
				String inputLine;
				while ((inputLine = in.readLine()) != null)
				{
					final String[] data = inputLine.split(":");
					final SampServer server = new SampServer(data[0], Integer.parseInt(data[1]));
					if (!servers.contains(server))
					{
						servers.add(server);
					}
				}
			}
		}
		catch (@SuppressWarnings("unused") final IOException exception)
		{
			// Do nthn
		}

		return servers;
	}

	private static String readUrl(final String urlString) throws Exception
	{
		BufferedReader reader = null;
		try
		{
			final URL url = new URL(urlString);
			reader = new BufferedReader(new InputStreamReader(url.openStream()));
			final StringBuffer buffer = new StringBuffer();
			int read;
			final char[] chars = new char[1024];
			while ((read = reader.read(chars)) != -1)
			{
				buffer.append(chars, 0, read);
			}

			return buffer.toString();
		}
		finally
		{
			if (reader != null)
			{
				reader.close();
			}
		}
	}

	public static List<SampServer> retrieveAnnouncedServers() throws Exception
	{
		final List<SampServer> servers = new ArrayList<>();
		final String json = readUrl("http://api.samp.southcla.ws/servers");

		final JsonArray jsonArray = Json.parse(json).asArray();
		jsonArray.forEach(object ->
		{
			final JsonObject jsonServerData = object.asObject();
			final String address = jsonServerData.getString("ip", "Unknown");

			final String[] addressData = address.split(":");

			final SampServer server = new SampServer(addressData[0], address.contains(":") ? Integer.parseInt(addressData[1]) : 7777);

			server.setPlayers(jsonServerData.getInt("pc", 0));
			server.setMaxPlayers(jsonServerData.getInt("pm", 0));
			server.setMode(jsonServerData.getString("gm", "Unknown"));
			server.setHostname(jsonServerData.getString("hn", "Unknown"));
			server.setLanguage(jsonServerData.getString("la", "Unknown"));

			servers.add(server);
		});

		return servers;
	}

	public static SampServer getServerInfo() throws Exception
	{
		final String json = readUrl("http://api.samp.southcla.ws/server/ss.southcla.ws");

		final JsonObject jsonData = Json.parse(json).asObject();
		jsonData.forEach(shit ->
		{
			System.out.println(shit.getName());
			if (shit.getName().equals("ru"))
			{
				shit.getValue().asObject().forEach(value -><commit_msg>Use API v1 announce backend<commit_after>package com.msc.serverbrowser.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.xerces.impl.io.UTF8Reader;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.msc.serverbrowser.entities.SampServer;

public class ServerUtil
{
	public static List<SampServer> retrieveMasterlistServers(final String version)
	{
		final List<SampServer> servers = new ArrayList<>();
		try
		{
			final URLConnection openConnection = new URL("http://lists.sa-mp.com/" + version + "/servers").openConnection();
			openConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
			try (final BufferedReader in = new BufferedReader(new InputStreamReader(openConnection.getInputStream())))
			{
				String inputLine;
				while ((inputLine = in.readLine()) != null)
				{
					final String[] data = inputLine.split(":");
					final SampServer server = new SampServer(data[0], Integer.parseInt(data[1]));
					if (!servers.contains(server))
					{
						servers.add(server);
					}
				}
			}
		}
		catch (@SuppressWarnings("unused") final IOException exception)
		{
			// Do nthn
		}

		return servers;
	}

	private static String readUrl(final String urlString) throws Exception
	{
		BufferedReader reader = null;
		try
		{
			final URL url = new URL(urlString);
			reader = new BufferedReader(new InputStreamReader(url.openStream()));
			final StringBuffer buffer = new StringBuffer();
			int read;
			final char[] chars = new char[1024];
			while ((read = reader.read(chars)) != -1)
			{
				buffer.append(chars, 0, read);
			}

			return buffer.toString();
		}
		finally
		{
			if (reader != null)
			{
				reader.close();
			}
		}
	}

	public static List<SampServer> retrieveAnnouncedServers() throws Exception
	{
		final List<SampServer> servers = new ArrayList<>();

		try (final UTF8Reader reader = new UTF8Reader(new URL("http://api.samp.southcla.ws/v1/servers").openStream()))
		{
			final JsonArray jsonArray = Json.parse(reader).asArray();
			jsonArray.forEach(object ->
			{
				final JsonObject jsonServerData = object.asObject();
				final String address = jsonServerData.getString("ip", "Unknown");

				final String[] addressData = address.split(":");

				final SampServer server = new SampServer(addressData[0], address.contains(":") ? Integer.parseInt(addressData[1]) : 7777);

				server.setPlayers(jsonServerData.getInt("pc", 0));
				server.setMaxPlayers(jsonServerData.getInt("pm", 0));
				server.setMode(jsonServerData.getString("gm", "Unknown"));
				server.setHostname(jsonServerData.getString("hn", "Unknown"));
				server.setLanguage(jsonServerData.getString("la", "Unknown"));

				servers.add(server);
			});

			return servers;
		}
	}

	public static SampServer getServerInfo() throws Exception
	{
		final String json = readUrl("http://api.samp.southcla.ws/v1/server/ss.southcla.ws");

		final JsonObject jsonData = Json.parse(json).asObject();
		jsonData.forEach(shit ->
		{
			System.out.println(shit.getName());
			if (shit.getName().equals("ru"))
			{
				shit.getValue().asObject().forEach(value ->
{'fraction_non_alphanumeric': 0.13984529045957833, 'fraction_numerical': 0.011830729561656303, 'mean_word_length': 4.233333333333333, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4669125', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2064, 'n_words': 429}