home

Weather-Analysis


Description


The purpose of the project is to historicize and analyze the meteorological data of a certain area, to understand how the weather will change and carry out predictive analysis based on the data collected using machine learning.

The project consists of a python script (to be scheduled at 00:00 every day) and a python application with flask.


The script (telegramForecast.py):


1) retrieves the weather forecast of the day with three-hour intervals through the free api provided by OpenWeatherMap;

2) sends these weather forecasts to a telegram bot;

3) saves the data to a MySQL database;


The flask application displays the graphs of the various weather parameters through the Frappe Charts library.



Web Application Code


secrets.py


The script.py script is a simple python file that contains a dictionary with the db credentials and other information that you don’t want shown on the repository. In fact, it is enough not to commit the file by inserting it in the .gitignore.


secrets = {
    'DB_HOST': "host_name",
    'DB_USR': "db user",
    'DB_NAME': 'db_name',
    'DB_PORT': 'db_port',
    'DB_PWD': 'db_pwd',
    #OPENWEATHERMAP CONF
    'OWM_API_KEY': 'omw_key',
    'OWM_CITY': 'name_of_city',
    #TELEGRAM CONF
    'TELEGRAM_BOT_KEY': 'telegram_key',
    'TELEGRAM_CHAT_ID': 'chat_id'
}

Telegram Script


Forecast Configuration



The API used provides the weather forecast every three hours, scheduling the script at 00:00 will send 8 forecasts as shown above. If you want to send fewer forecasts, just change the number at the following line of the script:


forecast = get_response().json()["list"][0:8]

Deployment


I have deployed the whole application on PythonAnywhere, the script that sends the weather forecast, the web application and the MySql database.


Database Dump


The following is a very useful command which allows you to export the database from PythonAnywhere. The command must be run via a bash console and not a db console. Replace USER and HOSTNAME in three occurrences:


mysqldump -u USER -h HOSTNAME --databases    'USER$default' --single-transaction --no-tablespaces > backup.sql;

The command will generate the .sql script at /home/user on PythonAnywhere:


image


Task Scheduling


To schedule the weather forecast script on PythonAnywhere (or on your server) remember to set the correct version of python:


image


GitHub Repository