|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# ##### BEGIN GPL LICENSE BLOCK ##### |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU General Public License |
| 8 | +# as published by the Free Software Foundation; either version 2 |
| 9 | +# of the License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program; if not, write to the Free Software Foundation, |
| 18 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | +# |
| 20 | +# ##### END GPL LICENSE BLOCK ##### |
| 21 | + |
| 22 | +# Name : |
| 23 | +# Weather Logger |
| 24 | +# Author : |
| 25 | +# ▄▄▄▄▄▄▄ ▄ ▄▄ ▄▄▄▄▄▄▄ |
| 26 | +# █ ▄▄▄ █ ██ ▀▄ █ ▄▄▄ █ |
| 27 | +# █ ███ █ ▄▀ ▀▄ █ ███ █ |
| 28 | +# █▄▄▄▄▄█ █ ▄▀█ █▄▄▄▄▄█ |
| 29 | +# ▄▄ ▄ ▄▄▀██▀▀ ▄▄▄ ▄▄ |
| 30 | +# ▀█▄█▄▄▄█▀▀ ▄▄▀█ █▄▀█ |
| 31 | +# █ █▀▄▄▄▀██▀▄ █▄▄█ ▀█ |
| 32 | +# ▄▄▄▄▄▄▄ █▄█▀ ▄ ██ ▄█ |
| 33 | +# █ ▄▄▄ █ █▀█▀ ▄▀▀ ▄▀ |
| 34 | +# █ ███ █ ▀▄ ▄▀▀▄▄▀█▀█ |
| 35 | +# █▄▄▄▄▄█ ███▀▄▀ ▀██ ▄ |
| 36 | + |
| 37 | +# DEPENDENCIES |
| 38 | + |
| 39 | +import sys |
| 40 | +import os.path |
| 41 | +import argparse |
| 42 | +import datetime |
| 43 | +import configparser |
| 44 | +import urllib.request |
| 45 | +from lxml import etree |
| 46 | + |
| 47 | +# CONFIGURATION |
| 48 | + |
| 49 | +PROGRAM_NAME = "Weather Logger" |
| 50 | +PROGRAM_VERSION = "2.0" |
| 51 | + |
| 52 | +argParser = argparse.ArgumentParser(description=PROGRAM_NAME + " " + PROGRAM_VERSION) |
| 53 | +argParser.add_argument('-c', '--config', metavar='PATH', help='"config.ini" configuration file path', required=True) |
| 54 | +args = vars(argParser.parse_args()) |
| 55 | +sConfigFilePath = args['config'] |
| 56 | + |
| 57 | +if not os.path.isfile(sConfigFilePath): |
| 58 | + print("Invalid INI configuration file path") |
| 59 | + sys.exit(1) |
| 60 | +try: |
| 61 | + configObj = configparser.RawConfigParser() |
| 62 | + configObj.read(sConfigFilePath) |
| 63 | +except: |
| 64 | + print("Badly written INI configuration file") |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | +sWebsite = "http://aviation.meteo.fr" |
| 68 | +sBaseUrl = sWebsite + "/FR/aviation/serveur_donnees.jsp?ID=" + configObj.get('General', 'user_code') + "&TYPE_DONNEES=" |
| 69 | + |
| 70 | +sLocationCodes = configObj.get('General', 'ICAO_airport_codes').replace(" ", "").split(",") |
| 71 | + |
| 72 | +# METAR and TAF |
| 73 | + |
| 74 | +if configObj.getboolean('General', 'METAR_logging') or configObj.getboolean('General', 'TAF_logging'): |
| 75 | + |
| 76 | + # URL OPENING |
| 77 | + |
| 78 | + sWeatherDataUrl = sBaseUrl + "OPMET&LIEUID=" + "%7C".join(sLocationCodes) + "&METAR=oui&TAF=Deux" |
| 79 | + oFile = urllib.request.urlopen(sWeatherDataUrl) |
| 80 | + sFileContent = oFile.read() |
| 81 | + |
| 82 | + # XML PARSING |
| 83 | + |
| 84 | + rootXML = etree.fromstring(sFileContent) |
| 85 | + if not rootXML.find('code') is None: |
| 86 | + print("Invalid 10-digits aeronautical code") |
| 87 | + sys.exit(1) |
| 88 | + |
| 89 | + for sLocationCode in sLocationCodes: |
| 90 | + |
| 91 | + # DATA EXTRACTION |
| 92 | + |
| 93 | + sDataMETAR = rootXML.xpath("///messages[attribute::oaci='" + sLocationCode + "']/message[attribute::type='METAR']/texte")[0].text.replace('\n', '') + '\n' |
| 94 | + sDataTAF = rootXML.xpath("///messages[attribute::oaci='" + sLocationCode + "']/message[attribute::type='TAFL']/texte")[0].text.replace('\n', '') + '\n' |
| 95 | + |
| 96 | + # CSV FILES |
| 97 | + |
| 98 | + sOutputPath = configObj.get('Directory', 'csv_directory') |
| 99 | + if not os.path.exists(sOutputPath): |
| 100 | + os.makedirs(sOutputPath) |
| 101 | + sDateTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M ') |
| 102 | + |
| 103 | + if configObj.getboolean('General', 'METAR_logging'): |
| 104 | + csvFileMETAR = open(os.path.join(sOutputPath, "METAR_" + sLocationCode + ".csv"), 'a') |
| 105 | + csvFileMETAR.write(sDateTime + sDataMETAR) |
| 106 | + csvFileMETAR.close() |
| 107 | + |
| 108 | + if configObj.getboolean('General', 'TAF_logging'): |
| 109 | + csvFileTAF = open(os.path.join(sOutputPath, "TAF_" + sLocationCode + ".csv"), 'a') |
| 110 | + csvFileTAF.write(sDateTime + sDataTAF) |
| 111 | + csvFileTAF.close() |
| 112 | + |
| 113 | +# WINTEM, TEMSI FRANCE and EUROC |
| 114 | + |
| 115 | +if configObj.getboolean('General', 'WINTEM_logging') or configObj.getboolean('General', 'TEMSI_logging') or configObj.getboolean('General', 'EUROC_logging'): |
| 116 | + |
| 117 | + dicUrl = {} |
| 118 | + |
| 119 | + sOutputPath = configObj.get('Directory', 'pdf_directory') |
| 120 | + if not os.path.exists(sOutputPath): |
| 121 | + os.makedirs(sOutputPath) |
| 122 | + |
| 123 | + # WINTEM |
| 124 | + if configObj.getboolean('General', 'WINTEM_logging'): |
| 125 | + sWeatherDataUrl = sBaseUrl + "CARTES&BASE_COMPLETE=non&VUE_CARTE=AERO_WINTEM&ALTITUDE=020" |
| 126 | + dicUrl['WINTEM'] = sWeatherDataUrl |
| 127 | + # TEMSI FRANCE |
| 128 | + if configObj.getboolean('General', 'TEMSI_logging'): |
| 129 | + sWeatherDataUrl = sBaseUrl + "CARTES&BASE_COMPLETE=non&VUE_CARTE=AERO_TEMSI" |
| 130 | + dicUrl['TEMSI'] = sWeatherDataUrl |
| 131 | + # TEMSI EUROC |
| 132 | + if configObj.getboolean('General', 'EUROC_logging'): |
| 133 | + sWeatherDataUrl = sBaseUrl + "CARTES&BASE_COMPLETE=non&VUE_CARTE=AERO_TEMSI&ZONE=AERO_EUROC" |
| 134 | + dicUrl['EUROC'] = sWeatherDataUrl |
| 135 | + |
| 136 | + for key in dicUrl: |
| 137 | + |
| 138 | + # URL OPENING |
| 139 | + |
| 140 | + oFile = urllib.request.urlopen(dicUrl[key]) |
| 141 | + sFileContent = oFile.read() |
| 142 | + |
| 143 | + # XML PARSING |
| 144 | + |
| 145 | + rootXML = etree.fromstring(sFileContent) |
| 146 | + if not rootXML.find('code') is None: |
| 147 | + print("Invalid 10-digits aeronautical code") |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | + # PDF FILES |
| 151 | + |
| 152 | + for i in range(0, len(rootXML.xpath("///carte/lien"))): |
| 153 | + oFile = urllib.request.urlretrieve(sWebsite + rootXML.xpath("///carte/lien")[i].text, os.path.join(configObj.get('Directory', 'pdf_directory'), key + "_" + str(i) + ".pdf")) |
0 commit comments