Skip to content

Console based weather app written in Python, using OpenWeatherMaps API service. (Final project for CS50P)

Notifications You must be signed in to change notification settings

Jaytak/ConsoleWeatherApp

Repository files navigation

Console Weather App

Version 1.7

Description:

This is a console-based weather app written in Python. It allows users to quickly check their local weather, or weather for anywhere in the world, including at lat: 00.00 lon: 00.00, the middle of nowhere (the Atlantic Ocean.)

The app logs weather data and diagnostic info, see below for more information.

Note

This weather app is pulling weather data from OpenWeatherMap's API service. You need to provide an API key for this app to function.
You can get a free or paid API key at openweathermap.org/price

The weather app can use 3 standard units of measurement:
  1. Metric
  2. Imperial
  3. Kelvin (standard)
The weather app can get weather with varying search criteria:
  1. City name
  2. City name and post/zip code
  3. Post/Zip code and country code
  4. Latitude and longitude coordinates
Example of how the app displays information:
Weather for:... Invercargill City
Timezone:...... Pacific/Auckland
Local Time:.... [17:44:40][27-01-2024]
Sunrise/Sunset: ⬆ [06:30:07] ⬇ [21:27:52]
Weather:....... Scattered clouds
Clouds:........ 50 %
Temp:.......... 19.45 °C
Feels Like:.... 18.89 °C
Dew Point:..... 10.18 °C
Pressure:...... 994 hPa
Humidity:...... 55 %
UV Index:...... 2.03
Wind Speed:.... 6.26 m/s (22.54 km/h), ⬊ 157° (SSE)
Gusting:....... 10.73 m/s (38.63 km/h)

Important

You must install the following dependencies to run this app!

pyfiglet

Install with pip3 install pyfiglet

requests

Install with pip3 install requests

Included Dependent Libraries:
  • JayTAKLib.py
  • JayTAKObjectLib.py
Included Unit Test:
  • test_weather.py

Setup:

Ensure you have installed pyfiglet with pip install pyfiglet
On the first run, the program will look for a saved API key for OpenWeatherMap. If it cannot find one it will prompt the user to enter their key. That is all.

Modes of Operation:

NORMAL MODE:

$ python3 weather.py
When you first run the program in this mode, the program will prompt the user for their preferred units, it will then ask for a 'home' location, and then save this information in userdata/userData.json
Further runs of the program in this mode will print the current weather data for the saved location in the saved units.

USER MODE:

$ python3 weather.py user
This mode allows the user to set the units on every run and then allows the user to query many weather locations subsequently until the user decides to exit the app.

UPDATE API KEY MODE:

$ python3 weather.py update-key
This mode allows the user to rewrite the API key file with a new API key. This file is automatically generated if the file is missing on execution.

PURGE LOGS MODE:

$ python3 weather.py purge-logs
This mode deletes all logs from the logs folders and will only leave the purge-logs session in the weatherAppActions.log file, this will not touch the userdata or API key file. The app does remove logs older than 6 hours automatically on each run, but this is here just in case you feel the need to do it yourself. You could have been running many unit tests without running the program itself.

APP RESET MODE:

$ python3 weather.py app-reset --CONFIRM-RESET
This will remove all directories and files (logs, user data and stored API key) that were created by this app, resetting it back to as it was when it was downloaded.

Possible Server Status Codes:

200
Server response OK, you won't see this as a user, unless you look in the logs.
401
You either entered an invalid API key or it is not activated yet. Within the next couple of hours, it will be activated and ready to use.
404
You can get this error when you specified the wrong city name, POST/ZIP code or city ID
429
You will get the error 429 if you are a user of a Free subscription and make more than 60 API calls per minute (surpassing the limit of your subscription)

Visit OpenWeatherMaps FAQ for more information.

Program Exit Codes:

1 Normal Program Exit
2 Program Finished Purge-Logs Mode
3 Program Finished Update-Key Mode
4 Program Finished Nuking Logs
5 Program Displayed Help
6 validateKey caught WeatherServerException
7 validateKey caught BaseException
8 codeCheck caught WeatherServerException
9 validateKey caught BaseException
10 checkAPIKey caught KeyboardInterrupt
11 updateKey caught KeyboardInterrupt
12 getLocationNameByCoords caught WeatherServerException

Custom Exceptions:

WeatherServerException

Raised when the server does not respond with code 200


HOW TO RUN UNIT TEST:

Important

You need to open the weather.py file, and manually set your API key directly in the Global Variable APIKEY on line 27.
NOTE: CS50 staff do not need to set the API key, a valid key will be in the code for them.

APIKEY = ""

This is because the unit test bypasses the loading of the API key from the APIKEY.json file.

Then run:
pytest test_weather.py
There are 23 unit tests with many more assertions under them.

Note

Do expect the weatherAppActions.log will have some extra lines written in-between
[ALERT] -- Logs closed.
and
[ALERT] -- Logs opening.
as the unit test will bypass those opening and closing log entries, but the functions will still log their actions in the file.


Important

The unit test will bypass the log folder/file pruning. Just run the program in NORMAL or USER mode and it will deal with them for you. Or run it manually using either PURGE-LOGS or APP-RESET mode.

If your unit test output is as follows, you have an invalid or no API key in the weather.py file.

===================================== short test summary info ======================================
FAILED test_python.py::TestMainModeManual::test_mainModeManual_valid - AssertionError: 8 != 1
FAILED test_python.py::TestGetWeather::test_getWeather_valid - AssertionError: 401 != 200
FAILED test_weather.py::TestCoordsByLocationName::test_coordsByLocationName_denver - SystemExit: 8
FAILED test_weather.py::TestCoordsByLocationName::test_coordsByLocationName_seattle - SystemExit: 8
=================================== 4 failed, 20 passed in 3.39s ===================================

Diagnostic Logs:

This weather app does log the information it pulls from OpenWeathers Servers in .json files AND all the actions this app executes, along with some system information, please look at the bottom of the readme for more information on the system data logged. You can find these logs in weatherAppActions.log and all the subdirectories of logs. There is no need to worry about these logs building up as the app will purge logs from the logs folder that are older than 6 hours, and it will also prune the weatherAppActions.log once it reaches 10000 lines long down to 8000 lines. In the event you feel the need to clear the logs yourself you can run the program in either PURGE LOGS or APP RESET mode. See below for more information.

Note on System Information Logged in weatherAppActions.log

This weather app logs some basic system information so in the event you need to get support from me I have some more information on your system so I can better reproduce and diagnose your issue. There is currently no way to disable this information logged, however, the .log file will never leave your system unless you manually send it to us on GitHub.
There is no function in the program or its libraries that can send any data from the logs file. The only data the app can send is solely to OpenWeatherMap's API, and that is with your location information so we can get up-to-date weather data.
Some information is not always available to log, in that case, the data will be blank ``

Example of the information logged:
[00:31:26][27-01-2024][DEBUG] -- Function 'checkUsage' Platform Info(1/3): uname_result(system='Linux', node='codespaces-xxxxxx', release='6.2.0-1018-azure', version='#18~22.04.1-Ubuntu SMP Tue Nov 21 19:25:02 UTC 2023', machine='x86_64')
[00:31:26][27-01-2024][DEBUG] -- Function 'checkUsage' Platform Info(2/3): Platform: 'Linux-6.2.0-1018-azure-x86_64-with-glibc2.35' Architecture: ('64bit', '') Python-Build: ('main', 'Jan  5 2024 01:56:00') Python-Compiler: 'GCC 11.4.0'
[00:31:26][27-01-2024][DEBUG] -- Function 'checkUsage' Platform Info(3/3): Python-Version: '3.11.7' Python-Implementation: 'CPython' Python-Branch: '' Python-Revision: ''
Written by JayTAK

About

Console based weather app written in Python, using OpenWeatherMaps API service. (Final project for CS50P)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages