Skip to content

Latest commit

 

History

History
91 lines (56 loc) · 4.9 KB

README.md

File metadata and controls

91 lines (56 loc) · 4.9 KB

Weather Application

This project is a Web application that provides current and forecast weather info through an API from Open Weather

Services

There are a series of utility functions and a weather module made for this project in the services package. All of them are documented with comments in the code.

Weather API

In the project there's a weather module with functions to make asynchronous requests and retrieve various weather information. All of the functions use @cached from aiocache for better performance and to litimate API call usage. The API keys are loaded using Django's environ library. The keys are stored in pseudo private variables. The lang parameter has a default value retrieved from the settings LANGUAGE_CODE, it can be specified a different languange as a parameter.

  • get_weather(city: str="", lat: str="", lon: str="", mode: str="current", lang: str=lang) -> Optional[dict]: makes an API call to Open Weather retrieving the current or forecast weather conditions, of the requested city, in the requested language.
# Retrieves current weather conditions
weather_info = get_weather("milan") 

# Retrievies one week daily, and 24 hours, hourly forecast
forecast_info = get_weather("milan", mode="forecast") 
  • get_air_condition(lat, lon, lang) -> Optional[dict]: makes an API call using the google Air Quality API to retrieve the current and general air conditions of the zone. The call requires a KEY provided by google, and returns in json format the local aqi, the dominant pollutant and its concentration. The call is made using the request helper in the utilities module.
air_conditions = get_air_conditions(your_city_latitude, you_city_longitude)

Google Maps Embed API

This project uses an API provided by Google to embed a map of the requested city. The key is retrieved from the settings.

https://www.google.com/maps/embed/v1/place?key={{developer key}}&q={{city name}}

to make the call, is required to provide the name of the place and a developer key provided by google. Review the API documentation here

Functions

Utility functions in utilities.py

  • unix_timestamp_formatter(timestamp:int, date_format:str): Given a Unix timestamp, returns a human-readable date. The date format attribute is required to make the function reusable.
readable_date = unix_timestamp_formatter(your_timestamp, date_format="%A %d/%m/%Y")
  • get_user_info(): Using the geocoder library, this function returns the user's country code based on their IP address and their Language and Timezone. This function is called in the Settings Django file to change default Timezone and Language for the application
user_info = get_user_info()
  • make_link(match): To be called with the re.sub method of a regex object. It takes a match object as input and returns the match wrapped in an <a> tag with the same match as the href attribute. converts plain text into clickable links.
alert = alertRegex.sub(make_link, alert)
  • suggest_city(user_input :str, matches_num :int=5) -> list[str]: Receives a user input and with a score system returns the most similar results from the cities database table, the number of matches can be specified as a parameter.

Requests Helper

In utilities there are also 2 helpers to make asynchronous requests using the aiohttp library.

  • get_request(url:str, params:dict) -> Optional[dict]
  • post_request(url:str, params:dict, json) -> Optional[dict] Thery're used by the weather functions to make calls, and upon failure returns None

Libraries & Dependencies

python3.11 or later and it's relative package manager are required.

In the Requirements Document is listed every needed library.

Run this command to install needed libraries:

 pip install -r requirements.txt

This command will read line by line the Requirements document passing every package through pip installing them.

How to Contribute

This application is built using django, a python framework specifically meant for web development. To contribute is required a basic understanding of django, consult official documentation here.