Commit 04c5d269 authored by JOSSOUD Olivier's avatar JOSSOUD Olivier
Browse files

FTP OPAR.

parent 81beebdd
......@@ -45,7 +45,7 @@ class FtpCollector:
def _ftp_download_file(self, ftp_session: ftplib.FTP,
source_filepath: str, dest_filepath: str,
delete_if_success: bool = False) -> None:
delete_if_success: bool = False) -> bool:
self.logger.write(self.object_id, "Downloading " + source_filepath + " ...")
with open(dest_filepath, 'wb') as file:
response = ftp_session.retrbinary('RETR %s' % source_filepath, file.write)
......@@ -53,5 +53,7 @@ class FtpCollector:
if delete_if_success:
ftp_session.delete(source_filepath)
self.logger.write(self.object_id, "Done. Destination file: " + dest_filepath)
return True
else:
self.logger.write(self.object_id, "FAILED. Destination file: " + dest_filepath)
return False
import os
import configobj
import datetime
import zipfile
import wimcollect.logger as logger
import wimcollect.ftp as ftp
class Collector(ftp.FtpCollector):
def __init__(self, config_parser: configobj.ConfigObj, log: logger):
self.object_id = "FTPOPAR"
ftp.FtpCollector.__init__(self, config_parser, log, self.object_id)
def download_maido_ftir(self, first_day: datetime.date, last_day: datetime.date):
"""Download all Picarro files from FTP server.
The distant files will be deleted from the server if the transfer is successful.
Parameters
----------
site_id: str
Site's trigram
"""
self.logger.write(self.object_id, "Download Maido's FTIR meteo data.")
self._ftp_connect_()
days_list = [first_day + datetime.timedelta(days=x) for x in range((last_day - first_day).days + 1)]
for day in days_list:
source_filepath = self.base_dir + "/" + day.strftime("%Y%m%d") + "Meteo125HR.xls"
dest_filepath = self.__get_dest_filepath__(day)
success = self._ftp_download_file(self.session, source_filepath, dest_filepath)
if success:
self.__compress_file__(day, dest_filepath)
self.session.quit()
def __get_dest_filepath__(self, day: datetime.date) -> str:
# Build destination directory
dest_dir = os.path.join(self.config["LOCAL"]["base_dir"], "REU", "meteo", "maidoftir")
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# Build destination filename
filename = "REU_maidoftir_" + day.strftime("%Y%m%d") + ".tsv"
# Build full file path
dest_filepath = os.path.join(dest_dir, filename)
return dest_filepath
def __compress_file__(self, day: datetime.date, uncompressed_filepath: str):
# Compress the files
self.logger.write(self.object_id, day.strftime("%Y-%m-%d") + ": Compressing ...")
compressed_filepath = uncompressed_filepath + ".lzma"
zipf = zipfile.ZipFile(compressed_filepath, 'w', zipfile.ZIP_LZMA)
zipf.write(uncompressed_filepath, arcname=os.path.basename(uncompressed_filepath))
zipf.close()
if os.path.exists(compressed_filepath):
self.logger.write(self.object_id, day.strftime("%Y-%m-%d") + ": Done. Archive file: " + compressed_filepath)
os.remove(uncompressed_filepath)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment