repl.it
@LukeShomper/

Weather API-1

Python

Gets the current weather

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import urllib.request
import json
from os import system
from time import sleep 

APIKey = "714c2da2279a72ec8bb713798409d352"

hasAPIKey = input("Do you have an APIKey for openweathermap.org?\n(1) Yes\n(2) No\n")
if hasAPIKey == "1":
    APIKey = input("APIKey = ")
    system("clear")
else:
    print("You can use mine")
    sleep(1)
    system("clear")

locationZiporNah = input("(1) Weather by Zipcode\n(2) Weather by city name\n")
system("clear")
if locationZiporNah == "1":
    zipcode = input("Zipcode: ")
    system("clear")
    countryCode = input("Example: us\nCountry Code: ")
    url = "https://api.openweathermap.org/data/2.5/weather?zip="+ zipcode + "," + countryCode+ "&APPID=" + APIKey
else:
    cityName = input("City name: ")
    cityName = cityName.lower()
    url = "https://api.openweathermap.org/data/2.5/weather?q=%s&APPID=%s" % (cityName, APIKey)
    
system("clear")
req = urllib.request.urlopen(url).read().decode()
data = json.loads(req)
print("City: %s\nWeather: %s\nTemperature: %s°F | %s°C\nWind Speed: %s MPH\nWind Degree: %s°Degrees" % (data["name"], data["weather"][0]["main"], round((data["main"]["temp"] - 273.15) * 9/5 + 32, 2), round(data["main"]["temp"] - 273.15, 2), data["wind"]["speed"], data["wind"]["deg"]))
?