ScreteMonge / 3D-Weather

BSD 2-Clause "Simplified" License
5 stars 4 forks source link

Feature Request: Tie weather to IRL weather #22

Open ForTimeBeing opened 1 year ago

ForTimeBeing commented 1 year ago

It would be cool if we could enable a setting that would mimic the weather outside locally within runescape.

Ex. It is raining outside, so it is raining in runescape Ex. It is summer where I am, so it should be summer in runescape.

Other enhancements: It is 1am locally, it is dark in runescape. (Might be out of scope of 3D-weather)

Could possibly call: https://open-meteo.com/

One popular way is to use the IP geolocation service to determine the approximate location based on the IP address of the local PC.

Here's an example of how you can use the "ip-api" service to get the approximate latitude and longitude of the local PC:

Make an HTTP request to the "ip-api" service with the local PC's public IP address. Parse the JSON response to extract the latitude and longitude.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class LocalPCGeolocation {
    public static void main(String[] args) {
        try {
            String publicIP = getPublicIP();
            if (publicIP != null) {
                String geoLocationData = getGeolocationData(publicIP);
                if (geoLocationData != null) {
                    double latitude = parseLatitude(geoLocationData);
                    double longitude = parseLongitude(geoLocationData);
                    System.out.println("Latitude: " + latitude);
                    System.out.println("Longitude: " + longitude);
                } else {
                    System.out.println("Failed to fetch geolocation data.");
                }
            } else {
                System.out.println("Failed to get public IP address.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getPublicIP() throws IOException {
        URL url = new URL("https://api.ipify.org?format=json");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        connection.disconnect();
        return response.toString();
    }

    private static String getGeolocationData(String publicIP) throws IOException {
        URL url = new URL("http://ip-api.com/json/" + publicIP);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        connection.disconnect();
        return response.toString();
    }

    private static double parseLatitude(String geoLocationData) {
        // Parse the latitude from the JSON response
        // Assuming the JSON structure is {"lat": 123.456, ...}
        int startIndex = geoLocationData.indexOf("\"lat\":") + 6;
        int endIndex = geoLocationData.indexOf(",", startIndex);
        return Double.parseDouble(geoLocationData.substring(startIndex, endIndex));
    }

    private static double parseLongitude(String geoLocationData) {
        // Parse the longitude from the JSON response
        // Assuming the JSON structure is {"lon": 123.456, ...}
        int startIndex = geoLocationData.indexOf("\"lon\":") + 6;
        int endIndex = geoLocationData.indexOf(",", startIndex);
        return Double.parseDouble(geoLocationData.substring(startIndex, endIndex));
    }
}
ScreteMonge commented 1 year ago

Thank you for the suggestion! It's been requested a few times before and something I'm definitely interested in doing at some point.