Skip to content

Commit 3b9282f

Browse files
committed
Get-Weather-Details-Python-Ninjavin
1 parent 6e486df commit 3b9282f

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

Python/Weather-Details/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config.py

Python/Weather-Details/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Get Weather Details of a city
2+
3+
## How to Run?
4+
+ Run `python3 weather.py <location-name>`
5+
+ The output will present the weather details of the mentioned city
6+
7+
## Example
8+
![image](images/weather.png)
38.1 KB
Loading

Python/Weather-Details/weather.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import requests
2+
import json
3+
import sys
4+
from colorama import Fore, init
5+
import config
6+
# Autoreset on for Colorama
7+
init(autoreset=True)
8+
# Getting API Key from Config.py
9+
api_key=config.api_key
10+
# If the user doesn't enter the location
11+
if len(sys.argv) != 2 :
12+
print("Enter location")
13+
location = sys.argv[1]
14+
# Calling the API
15+
url = f"https://api.openweathermap.org/data/2.5/weather?q={location}&units=metric&appid={api_key}"
16+
response = requests.get(url)
17+
data = json.loads(response.text)
18+
print(Fore.CYAN + "Weather Details for the city " + data["name"])
19+
print(Fore.BLUE + "Weather : " + data["weather"][0]["main"])
20+
print(Fore.BLUE + "Temperature is " + str(data["main"]["temp"]) + "°C and it feels like " + str(data["main"]["feels_like"]) + "°C")
21+
print(Fore.BLUE + "Wind Speed is " + str(data["wind"]["speed"]) + " m/sec")

0 commit comments

Comments
 (0)