def get_files(self, filenames): """ Retrieves the given filenames from the FTP. """ for filename in filenames: util.conditional_print("Getting File: {0}".format(filename)) self.get_file(filename, '.')
def update_image_paths(self): """ Retrieves the list of all the file names and updates them to the relative path. This should be executed before the class is destroyed. """ util.conditional_print("Updating image paths.") updated_rows = [] headers = [] new_file_path = None with open(self.csv_file_path, "rU") as f: csv_reader = csv.DictReader(f) headers = csv_reader.fieldnames for row in csv_reader: if row["Image"]: new_path = self.get_image_name(row["Image"]) row["Image"] = new_path updated_rows.append(row) if not new_file_path: new_file_path = self.build_new_file_path(row) with open(new_file_path, "wb") as f: csv_writer = csv.DictWriter(f, fieldnames=headers) csv_writer.writeheader() for row in updated_rows: csv_writer.writerow(row) if new_file_path: self.rename_image_folder(new_file_path) self.archive_old_file()
def close_ftp(self): """ If a connection exists, it closes it. To avoid having to frequently call this, consider using the `with` statement. """ if self.connection: self.connection.quit() util.conditional_print('Connection to {0} closed.'.format(self.host))
def download_all_images(self): """ Retrieves all the images stored within the `image_paths` attributes. It should be called after `can_download` is True. """ util.conditional_print("Downloading images.") self.image_paths = list(set(self.image_paths)) for image_url in self.image_paths: self.download_image(image_url)
def delete_files(self, files): """ Removes the given filenames from the FTP. """ for f in files: util.conditional_print("About to delete file: {0}".format(f)) try: self.connection.delete(f) except e: print("Unable to delete file: {0}\nError: {1}".format(f, e))
def open_ftp(self): """ Returns the active connection is one is available. If not, it creates a new connection and returns that. """ if not self.connection: self.connection = FTP(self.host) self.connection.login(self.username, self.password) util.conditional_print('Connected to {0}.'.format(self.host)) return self.connection
def compress_images(self): """ Grabs all the images that are allowed in the class via the `allowed_image_types`, and compresses them. """ util.conditional_print("Zipping images.") with zipfile.ZipFile("images.zip", "w") as zipper: for img in os.listdir("."): for extension in self.allowed_image_types: if img.endswith(extension): zipper.write(os.path.join(".", img))
def download_files_from_ftp(configuration, use_debug): """ The main worker for downloading the files from the FTP. It attempts to safely download files from the FTP server. """ files = [] try: with FtpClient(configuration) as ftp_client: files = ftp_client.get_all_files() except e: util.conditional_print( 'An error occured while trying to retrieve files via FTP.', 'ERROR') util.conditional_print(e, 'ERROR') finally: return files