# Hezarfen Weather Fetcher
Hezarfen Weather Fetcher is a Python application that retrieves METAR, TAF, and GAMET reports from Turkish Meteorological Service (rasat.mgm.gov.tr) and sends them to a Telegram channel. It also retrieves browser headers from a header API to simulate realistic web requests.
## Installation
1. Clone this repository to your local machine:
```bash
git clone https://github.com/your-repo/hezarfen-weather-fetcher.git
cd hezarfen-weather-fetcher
-
Install the required packages:
pip install -r requirements.txt
-
Create a
.env
file in the root directory and add your API keys:TELEGRAM_BOT_KEY=#yourkey TELEGRAM_CHAT_ID=#yourkey HEADER_API_KEY=#yourkey
-
Run the script:
python hezarfen.py
The script will check the weather reports from the Turkish Meteorological Service every half hour at minute 2 and 31. The retrieved reports will be sent to your configured Telegram channel.
-
You can customize the weather stations by modifying the parameters in the
prepare_the_data
function. -
Change the timezone in the constructor to your local timezone using
pytz
.
requests
pytz
This project is licensed under the MIT License.
This Python code is designed to fetch METAR (Meteorological Aerodrome Report), TAF (Terminal Aerodrome Forecast), and GAMET (general meteorological forecast) reports from the Turkish State Meteorological Service (MGM) Rasat website and send them to a Telegram channel. The application performs the following operations:
- Fetches browser headers from an API to simulate a legitimate browser request to MGM Rasat.
- Parses METAR, TAF, and GAMET reports from the fetched JSON data.
- Sends the parsed reports to a Telegram channel using the Telegram API.
- Runs every 30 minutes to check for new weather reports.
This method initializes the class and sets up some basic configurations:
def __init__(self):
-
self.base_url
andself.url_obs_types
: These define the base URL for making requests to the Rasat website. It fetches weather data for specified airfields. -
self._api_key
andself._api_url
: These are used to fetch browser headers fromscrapeops.io
, which simulates a real browser request to avoid getting blocked. -
self.telegram_bot
: This defines the base URL for sending messages to a specific Telegram channel using the Telegram API. Thechat_id
represents the Telegram channel, and the bot key allows access to the bot. -
self.last_metar_report
: Stores the last METAR report to prevent duplicate messages from being sent repeatedly. -
self.local_time
: This sets the timezone to Istanbul usingpytz
. -
self.accept
: A flag used to control the sending of reports. If new data is available, it allows sending; otherwise, it prevents resending old data.
This function fetches random browser headers from an API (scrapeops.io
) to make requests to the Rasat website:
def get_browser_headers(self):
-
requests.get(self._api_url)
: Sends a GET request to the API URL with the API key, requesting a random browser header to avoid being blocked when making the request to the weather site. -
Exception Handling: If the request fails, it returns a message indicating that the script could not generate a browser header.
This method prepares the data for the API request by formatting the stations into the required URL format:
def prepare_the_data(self, *args) -> str:
*args
: Accepts a list of station codes (e.g., LTAB for Esenboğa Airport) to be used in the weather data request.self.size_of_param
: Stores the number of stations to be queried.self.searched_airfields
: Stores the list of airfields passed as arguments.- Returns: The function returns a complete URL to request data from the Rasat website for the specified stations.
This static method decodes the JSON response received from the Rasat website, extracting the METAR and TAF reports:
@staticmethod
def decoder(json_file, metar_size):
json_file
: The JSON file containing the weather data.metar_size
: The number of airfields (stations) requested.metars
: A list that stores the decoded weather reports for each station.- Returns: A list of METAR reports for each requested airfield.
This method generates the browser headers and sends the request to the Rasat website:
def get_json_file(self, request_url: str):
header = self.get_browser_headers()
: Fetches the headers from the API using the previously definedget_browser_headers()
method.requests.get(request_url, headers=header)
: Sends a GET request to the Rasat website using the generated headers.- If Successful: If the request is successful (
status_code == 200
), the method parses the response using thedecoder()
method and returns the data in JSON format. - Error Handling: If there's an issue (e.g., the Rasat website is down), an error message is returned.
This is the main method that runs continuously, checking the time and fetching data every 30 minutes:
def run_up_program(self):
valid_time
: Fetches the current time using the previously defined Istanbul timezone.- Time Check: The program checks the time and runs at minutes 2 and 31 of each hour, making the API request for the airfield weather data.
self.prepare_the_data("LTAB")
: Prepares the URL to fetch weather data for Esenboğa Airport (LTAB).results_for_report
: The JSON response from the Rasat website.- Parsing: The method loops through the returned data, checking for METAR, TAF, or GAMET reports.
- Telegram Message: If a new METAR report is found (not the same as the last one), it is formatted and sent to the Telegram channel using the API.
while True:
c.run_up_program()
- The program runs in an infinite loop, constantly checking for new weather reports and sending them to the Telegram channel at specified intervals.