Skip to content

HayatiYrtgl/metar_taf_gamet_reporter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

README.md

# 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
  1. Install the required packages:

    pip install -r requirements.txt
  2. 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

Usage

  1. 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.

Configuration

  • 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.

Dependencies

  • requests
  • pytz

License

This project is licensed under the MIT License.

Code Overview

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:

  1. Fetches browser headers from an API to simulate a legitimate browser request to MGM Rasat.
  2. Parses METAR, TAF, and GAMET reports from the fetched JSON data.
  3. Sends the parsed reports to a Telegram channel using the Telegram API.
  4. Runs every 30 minutes to check for new weather reports.

Code Sections

1. __init__ Method (Class Constructor)

This method initializes the class and sets up some basic configurations:

def __init__(self):
  • self.base_url and self.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 and self._api_url: These are used to fetch browser headers from scrapeops.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. The chat_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 using pytz.

  • 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.

2. get_browser_headers() Method

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.

3. prepare_the_data() Method

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.

4. decoder() Method

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.

5. get_json_file() Method

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 defined get_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 the decoder() 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.

6. run_up_program() Method

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.

Infinite Loop

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.