Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal",
"justMyCode": true
}
]
}
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
10 changes: 10 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
isort:
python3 -m isort *.py
python3 -m isort tests/*.py

black:
python3 -m black *.py
python3 -m black tests/*.py

install:
python3 -m pip install -r requirements.txt
127 changes: 127 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import os
import sys
from time import sleep

import requests

TOKEN = "c439e1209216cc7e7c73a3a8d1d12bfd"
BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
BREAK = "\n\n=====\n\n"
# TOKEN = os.getenv("TOKEN")


def breakliner():
print(f"{BREAK:-^30}")
return


def clearScreen():
if sys.platform == "windows":
os.system("cls")
else:
os.system("clear")
return


def exitQuery():
exitQ = input("Exit? ")
if exitQ == "y":
exit()
elif exitQ == "n":
clearScreen()
OWMCITY()


def OWMCITY():
# City Name request
global CITY
CITY = input(
"Enter City Name (should be in compliance to OpenWeatherMap's City Index): "
)
if CITY == "exit":
clearScreen()
print("Three")
sleep(1)
clearScreen()
print("Two")
sleep(1)
clearScreen()
print("One.")
sleep(1)
clearScreen()
print("Exiting...")
sleep(0.5)
exit()

else:
pass
# The URL in actuality be like:
URL = BASE_URL + "q=" + CITY + "&appid=" + TOKEN

# PROD: Request Response
global resp_PROD
resp_PROD = requests.get(URL)
if resp_PROD.status_code == 200:
pass
else:
raise TypeError("Oops, wrong city name or code?")

global data
data = resp_PROD.json()


OWMCITY()


def data_processing():
main = data["main"]
humidity = main["humidity"]
pressure = main["pressure"]
temp = main["temp"]
temp_min = main["temp_min"]
temp_max = main["temp_max"]

def ktc(temp, temp_min, temp_max):
temp = temp - 273.15
temp_min = temp_min - 273.15
temp_max = temp_max - 273.15
print("Temperature: ", temp, "°C")
print("Minimum Temperature: ", temp_min, "°C")
print("Maximum Temperature: ", temp_max, "°C")

CITY = data["name"]
sys = data["sys"]
country = sys["country"]
CITY2 = CITY + "," + " " + country
if CITY == CITY2:
pass
else:
CITY = data["name"]
sys = data["sys"]
country = sys["country"]
CITY2 = CITY + "," + " " + country

w_main = data["weather"][0]["main"]
w_desc = data["weather"][0]["description"]
pressure = main["pressure"]
visibility = data["visibility"]
visibility_new = visibility / 1000
wind = data["wind"]["speed"]

def printData():
breakliner()
print(f"{CITY2:-^30}")
print(f"Weather: {w_main}:- {w_desc}")
ktc(temp, temp_min, temp_max)
print(f"Humidity: {humidity}%")
print(f"Pressure: {pressure} hPa")
print(f"Wind speed: {wind} m/s")
print(f"Visibility: {visibility} m (or) {visibility_new} km")

printData()


data_processing()


exitQuery()
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pyowm
requests
python-dotenv
python-unittest
isort
black
52 changes: 52 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
import unittest
from time import sleep

import requests
from dotenv import load_dotenv

TOKEN = os.getenv("TOKEN")
print(TOKEN)
CITY = "Norway" # test *only*
BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
URL = BASE_URL + "q=" + CITY + "&appid=" + TOKEN
resp_PROD = requests.get(URL)


class testAPILoad(unittest.TestCase):
load_dotenv()

def test_request(self):
TOKEN = os.getenv("TOKEN")
if resp_PROD.status_code == 200:
print(resp_PROD.status_code)
pass
else:
raise TypeError("Oops, wrong city name or code?")
sleep(5)
exit()

def test_data_processcode(self):
TOKEN = os.getenv("TOKEN")
data = resp_PROD.json()
main = data["main"]
humidity = main["humidity"]
pressure = main["pressure"]
temp = main["temp"]
temp_min = main["temp_min"]
temp_max = main["temp_max"]

def ktc(temp, temp_min, temp_max):
temp = temp - 273.15
temp_min = temp_min - 273.15
temp_max = temp_max - 273.15
print("Temperature: ", temp, "°C")
print("Minimum Temperature: ", temp_min, "°C")
print("Maximum Temperature: ", temp_max, "°C")

print(ktc(temp, temp_min, temp_max))
print(data["weather"][0]["main"])


if __name__ == "__main__":
unittest.main()