-
Notifications
You must be signed in to change notification settings - Fork 1
/
wfetch.py
119 lines (93 loc) · 3.03 KB
/
wfetch.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
"""
wfetch - show weather in your terminal (version 0.1)
"""
# Loads dependencies.
import os
import sys
import requests
# Loads settings.
import config as cfg
# Fetches raw data from OpenWeatherMap API and converts it to JSON list.
def fetchData() -> str:
j = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={cfg.city}&APPID={cfg.api}&units={cfg.unit}&lang={cfg.lang}")
source = j.json()
# Checks, if return code is 200 and if isn't, displays error message.
if source['cod'] == 200:
pass
elif source['cod'] == "404":
print("Invalid city name given.")
sys.exit()
elif source['cod'] == 401:
print("Invalid API key given.")
sys.exit()
else:
print("Unknown error occured.")
sys.exit()
return source
source = fetchData()
# Returns ascii image.
# ascii weather art used from wego project: https://github.com/schachmat/wego
#pylint: disable=anomalous-backslash-in-string
def viewAscii() -> None:
iconName = source['weather'][0]['main']
if iconName == "Clear":
print(" \ / ",
" .-. ",
" ‒ ( ) ‒ ",
" `-᾿ ",
" / \ ",
" ", sep = '\n')
elif iconName == "Clouds":
print(" .--. ",
" .-( ). ",
" (___.__)__) ",
" ", sep = '\n')
elif iconName == "Rain":
print(" .--. ",
" .-( ). ",
" (___.__)__) ",
" ʻ‚ʻ‚ʻ‚ʻ‚ʻ‚ ",
" ", sep = '\n')
elif iconName == "Snow":
print(" .--. ",
" .-( ). ",
" (___.__)__) ",
" * * * * * ",
" ", sep = '\n')
else:
print(" .--. ",
" .-( ). ",
" (___.__)__) ",
" ", sep = '\n')
# Checks which measurement unit to use.
def unitCheck() -> str:
if cfg.unit == "standard":
tempUnit = "K"
speedUnit = "m/s"
elif cfg.unit == "metric":
tempUnit = "°C"
speedUnit = "m/s"
elif cfg.unit == "imperial":
tempUnit = "°F"
speedUnit = "mph"
return tempUnit, speedUnit
tempUnit, speedUnit = unitCheck()
# Prints ascii image and weather info.
def printInfo() -> None:
mainGroup = source['main']
temp = mainGroup['temp']
humidity = mainGroup['humidity']
windGroup = source['wind']
speed = windGroup['speed']
descGroup = source['weather'][0]
desc = descGroup['description']
viewAscii()
print(f"Weather in {cfg.city}",
f"Description: {desc}",
f"Temperature: {str(temp)}{tempUnit}",
f"Humidity: {str(humidity)}%",
f"Wind: {str(speed)} {speedUnit}", sep = '\n')
os.system("clear||cls")
printInfo()
input("")