<commit_before>package heufybot.modules;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import heufybot.utils.FileUtils;
import heufybot.utils.StringUtils;
import heufybot.utils.URLUtils;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class WeatherInterface 
{
	private final static String APIkey = FileUtils.readFile("data/worldweatheronlineapikey.txt").replaceAll("\n", "");
	private final static String APIAddress = "http://api.worldweatheronline.com/free/v1/weather.ashx?";
	private final static String web = "http://www.worldweatheronline.com/v2/weather.aspx?q=";
	
	public String getWeather(float latitude, float longitude) throws ParseException
	{
		StringBuilder builder = new StringBuilder();
		builder.append(APIAddress);
		builder.append("q=" + latitude + "," + longitude);
		builder.append("&key=" + APIkey);
		builder.append("&fx=no");
		builder.append("&format=json");
		JSONObject object = getJSON(builder.toString());
		
		String parsedJSON = parseJSONForWeather(object);
		if(parsedJSON == null)
		{
			return null;
		}
		return parsedJSON + " | More info: " + URLUtils.shortenURL(web + latitude + "," + longitude);
	}
	
	public String getForecast(float latitude, float longitude) throws ParseException
	{
		StringBuilder builder = new StringBuilder();
		builder.append(APIAddress);
		builder.append("q=" + latitude + "," + longitude);
		builder.append("&key=" + APIkey);
		builder.append("&num_of_days=4");
		builder.append("&cc=no");
		builder.append("&format=json");
		JSONObject object = getJSON(builder.toString());
		
		String parsedJSON = parseJSONForForecast(object);
		if(parsedJSON == null)
		{
			return null;
		}
		return parsedJSON;
	}
	
	private String parseJSONForWeather(JSONObject object)
	{
		JSONObject data = (JSONObject)object.get("data");
		
		if((JSONArray)data.get("weather") == null)
		{
			return null;
		}
		
		JSONObject currentCondition = (JSONObject) ((JSONArray)data.get("current_condition")).get(0);

		String tempC = currentCondition.get("temp_C").toString();
		String tempF = currentCondition.get("temp_F").toString();
		String windspeedMiles = currentCondition.get("windspeedMiles").toString();
		String windspeedKmph = currentCondition.get("windspeedKmph").toString();
		String windDir = currentCondition.get("winddir16Point").toString();
		String desc = ((JSONObject)((JSONArray)currentCondition.get("weatherDesc")).get(0)).get("value").toString();
		String humidity = currentCondition.get("humidity").toString();
		
		return String.format("Temp: %sC / %sF | Weather: %s | Humidity: %s%c | Wind Speed: %s kmph / %s mph | Wind Direction: %s", tempC, tempF, desc, humidity, '%', windspeedKmph, windspeedMiles, windDir);
	}
	
	private String parseJSONForForecast(JSONObject object)
	{
		JSONObject data = (JSONObject)object.get("data");<commit_msg>Fix weather (again)<commit_after>package heufybot.modules;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import heufybot.utils.FileUtils;
import heufybot.utils.StringUtils;
import heufybot.utils.URLUtils;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class WeatherInterface 
{
	private final static String APIkey = FileUtils.readFile("data/worldweatheronlineapikey.txt").replaceAll("\n", "");
	private final static String APIAddress = "http://api.worldweatheronline.com/free/v1/weather.ashx?";
	private final static String web = "http://www.worldweatheronline.com/v2/weather.aspx?q=";
	
	public String getWeather(float latitude, float longitude) throws ParseException
	{
		StringBuilder builder = new StringBuilder();
		builder.append(APIAddress);
		builder.append("q=" + latitude + "," + longitude);
		builder.append("&key=" + APIkey);
		builder.append("&fx=no");
		builder.append("&format=json");
		JSONObject object = getJSON(builder.toString());
		
		String parsedJSON = parseJSONForWeather(object);
		if(parsedJSON == null)
		{
			return null;
		}
		return parsedJSON + " | More info: " + URLUtils.shortenURL(web + latitude + "," + longitude);
	}
	
	public String getForecast(float latitude, float longitude) throws ParseException
	{
		StringBuilder builder = new StringBuilder();
		builder.append(APIAddress);
		builder.append("q=" + latitude + "," + longitude);
		builder.append("&key=" + APIkey);
		builder.append("&num_of_days=4");
		builder.append("&cc=no");
		builder.append("&format=json");
		JSONObject object = getJSON(builder.toString());
		
		String parsedJSON = parseJSONForForecast(object);
		if(parsedJSON == null)
		{
			return null;
		}
		return parsedJSON;
	}
	
	private String parseJSONForWeather(JSONObject object)
	{
		JSONObject data = (JSONObject)object.get("data");
		
		if((JSONArray)data.get("current_condition") == null)
		{
			return null;
		}
		
		JSONObject currentCondition = (JSONObject) ((JSONArray)data.get("current_condition")).get(0);

		String tempC = currentCondition.get("temp_C").toString();
		String tempF = currentCondition.get("temp_F").toString();
		String windspeedMiles = currentCondition.get("windspeedMiles").toString();
		String windspeedKmph = currentCondition.get("windspeedKmph").toString();
		String windDir = currentCondition.get("winddir16Point").toString();
		String desc = ((JSONObject)((JSONArray)currentCondition.get("weatherDesc")).get(0)).get("value").toString();
		String humidity = currentCondition.get("humidity").toString();
		
		return String.format("Temp: %sC / %sF | Weather: %s | Humidity: %s%c | Wind Speed: %s kmph / %s mph | Wind Direction: %s", tempC, tempF, desc, humidity, '%', windspeedKmph, windspeedMiles, windDir);
	}
	
	private String parseJSONForForecast(JSONObject object)
	{
		JSONObject data = (JSONObject)object.get("data");
{'fraction_non_alphanumeric': 0.14388961892247043, 'fraction_numerical': 0.0022996057818659658, 'mean_word_length': 5.470775770456961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5561725', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1843, 'n_words': 373}