Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed weather #117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

fixed weather #117

wants to merge 5 commits into from

Conversation

JulianStiebler
Copy link

@JulianStiebler JulianStiebler commented Jan 17, 2025

import location has no reverse_geocode anymore;
and seems solely purposed on chinese city codes and general geospatial calculations.
A truer 10liner would be possible also using geocoder, which resolves the position by IP.

The returned data has the following scheme:

return_schema = {
  "coord": {
    "lon": "float",  // Longitude of the location
    "lat": "float"   // Latitude of the location
  },
  "weather": [
    {
      "id": "int",        // Weather condition ID
      "main": "string",   // Short description of the weather condition (e.g., 'Clouds')
      "description": "string", // Detailed description of the weather (e.g., 'overcast clouds')
      "icon": "string"    // Weather icon identifier
    }
  ],
  "base": "string",      // Data source (usually 'stations' for weather stations)
  "main": {
    "temp": "float",     // Temperature in Kelvin
    "feels_like": "float", // Temperature as it feels in Kelvin
    "temp_min": "float", // Minimum temperature in Kelvin
    "temp_max": "float", // Maximum temperature in Kelvin
    "pressure": "float", // Atmospheric pressure in hPa
    "humidity": "int",   // Humidity percentage (0 - 100)
    "sea_level": "float", // Atmospheric pressure at sea level in hPa
    "grnd_level": "float" // Atmospheric pressure at ground level in hPa
  },
  "visibility": "int",   // Visibility in meters (e.g., 10000 meters = 10 km)
  "wind": {
    "speed": "float",    // Wind speed in meters per second
    "deg": "int"         // Wind direction in degrees (meteorological)
  },
  "clouds": {
    "all": "int"         // Cloud coverage percentage (0 - 100)
  },
  "dt": "int",           // Date and time of the data calculation (Unix timestamp)
  "sys": {
    "type": "int",       // Internal parameter indicating the type of weather data
    "id": "int",         // Identifier for the system
    "country": "string", // Country code (e.g., 'DE' for Germany)
    "sunrise": "int",    // Sunrise time in Unix timestamp
    "sunset": "int"      // Sunset time in Unix timestamp
  },
  "timezone": "int",      // Timezone offset in seconds from UTC
  "id": "int",            // City ID in the OpenWeatherMap database
  "name": "string",       // City name (e.g., 'Nuremberg')
  "cod": "int"            // Response code (e.g., 200 indicates success)
}

@JulianStiebler
Copy link
Author

pre-commit moved some things;
other than that it was a true 10liner.

import geocoder
import requests
HTTP_STATUS_OK = 200
loc = geocoder.ip('me').json # Resolves location by IP
city_country = f"{loc['city']},{loc['country']}" # City string for URL
APPID = "beb97c1ce62559bba4e81e28de8be095"
weather = requests.get(f"http://api.openweathermap.org/data/2.5/weather?APPID={APPID}&q={city_country}").json()
if weather.get("cod") == HTTP_STATUS_OK:
    print(f"Weather in {city_country}\n------\n" + "\n".join(f"{key}: {value}" for key, value in weather.items()))
else: print("Error fetching data.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant