|
| 1 | +import os |
| 2 | +from ftplib import FTP, error_perm |
| 3 | + |
| 4 | +from netunicorn.base import Failure, Result, Success, Task |
| 5 | + |
| 6 | + |
| 7 | +class RetrieveFromFTP(Task): |
| 8 | + """ |
| 9 | + Task for retrieving a file from an FTP server to a local directory. Establishes a connection to the specified FTP server, navigates to the desired remote directory and downloads the specified file to a local directory. |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + ftp_remote_filepath: str, |
| 15 | + ftp_url: str, |
| 16 | + username: str, |
| 17 | + password: str, |
| 18 | + local_dir: str = "./", |
| 19 | + *args, |
| 20 | + **kwargs, |
| 21 | + ): |
| 22 | + """ |
| 23 | + Initializes the RetrieveFromFTP task with parameters. |
| 24 | +
|
| 25 | + Parameters: |
| 26 | + ftp_remote_filepath (str): Full path of the file on the FTP server to retrieve. |
| 27 | + ftp_url (str): URL or IP address of the FTP server. |
| 28 | + username (str): Username for FTP authentication. |
| 29 | + password (str): Password for FTP authentication. |
| 30 | + local_dir (str, optional): Local directory to save the retrieved file. Defaults to "./". |
| 31 | + *args: Variable length argument list. |
| 32 | + **kwargs: Arbitrary keyword arguments. |
| 33 | + """ |
| 34 | + super().__init__(*args, **kwargs) |
| 35 | + self.ftp_remote_filepath = ftp_remote_filepath |
| 36 | + self.ftp_url = ftp_url |
| 37 | + self.username = username |
| 38 | + self.password = password |
| 39 | + self.local_dir = local_dir |
| 40 | + |
| 41 | + def run(self) -> Result: |
| 42 | + """ |
| 43 | + Run the FTP file retrieval process. |
| 44 | +
|
| 45 | + Steps: |
| 46 | + 1. Connects to FTP server using provided credentials. |
| 47 | + 2. Navigates to the directory containing the target file. |
| 48 | + 3. Downloads the specified file to the local directory. Defaults to "./". |
| 49 | +
|
| 50 | + Returns: |
| 51 | + Result: |
| 52 | + Success: Contain success message upon successful download. |
| 53 | + --OR-- |
| 54 | + Failure: Contains an error message if the download fails. |
| 55 | +
|
| 56 | + """ |
| 57 | + try: |
| 58 | + ftp = FTP(self.ftp_url, timeout=30) # Modify timeout as needed |
| 59 | + ftp.login(user=self.username, passwd=self.password) |
| 60 | + |
| 61 | + remote_dir, remote_filename = os.path.split(self.ftp_remote_filepath) |
| 62 | + if remote_dir: |
| 63 | + ftp.cwd(remote_dir) |
| 64 | + |
| 65 | + if not os.path.isdir(self.local_dir): |
| 66 | + return Failure(f"Local directory does not exist: {self.local_dir}") |
| 67 | + |
| 68 | + local_filepath = os.path.join(self.local_dir, remote_filename) |
| 69 | + with open(local_filepath, "wb") as f: |
| 70 | + ftp.retrbinary(f"RETR {remote_filename}", f.write) |
| 71 | + |
| 72 | + ftp.quit() |
| 73 | + |
| 74 | + return Success( |
| 75 | + f"Successfully downloaded {self.ftp_remote_filepath} to {local_filepath}" |
| 76 | + ) |
| 77 | + |
| 78 | + except FileNotFoundError: |
| 79 | + return Failure(f"File not found: {self.ftp_remote_filepath}") |
| 80 | + |
| 81 | + except error_perm as e: |
| 82 | + return Failure(f"FTP permission error: {e}") |
| 83 | + |
| 84 | + except Exception as e: |
| 85 | + return Failure(f"An unexpected error occurred: {str(e)}") |
0 commit comments