"""This script will move TIF files from a local dir structure to a target directory, if the UPC portion of their file names are present in a provided list of UPC's.""" import os import re import sys from integration_scripts import logger from integration_scripts.general_use import dedupe_list from integration_scripts.file_utils import copy_file # Paths SOURCE_PATH = '/Volumes/SuburbanCat/Fixed_TIF' TARGET_PATH = '/Volumes/ripped_assets/TIF' MAX_SIZE = 100 START_AT = '8716059006468' track_input_file_name = '/Users/lvona/Documents/python_projects/ripper' \ '-feeder/data/track_list_from_metedata_spreadsheet.txt' upc_input_file_name = '/Users/lvona/Documents/python_projects/ripper' \ '-feeder/data/missing_covers_2020_01_21.txt' # Regex # Both valid and invalid image formats in groups. audio_folder = r'(?P\d{1,13})(?P[\ \-\_]{1,3})(?P.*)' cover_file_extensions = r'(?P^[^\.\_]\d{1,13}\.(TIF|tif))' def save_image(asset_target_file, image): """Save image.""" # if the image is not square if image.width != image.height: # Check how much longer the long side is long_edge = max(image.width, image.height) short_edge = min(image.width, image.height) percent_larger = (abs(short_edge - long_edge) / short_edge) * 100 thresh = 15 force_square = True # Reshape if necessary and within threshold if force_square and percent_larger <= thresh: image = image.resize((long_edge, long_edge)) else: raise RuntimeError( 'Image {} is not square, and has a long edge over {}% ' 'longer than the short edge. Will not auto-resize.'.format( asset_target_file, thresh)) if image.mode != 'RGB': logger.info('Converting {} to RGB.', asset_target_file) image = image.convert('RGB') # Upscale dimensions if image.width < 3000 and True: logger.info('Upscaling image {} to 3000x3000px.', asset_target_file) image = image.resize((3000, 3000)) logger.info('Saving File (at 300dpi): {}', asset_target_file) # Save as new file (extension determines type) image.save(asset_target_file, dpi=(300, 300)) def load_metadata(): # Get list of tracks from the metadata infile. metadata_track_list = [] metadata_upc_list = [] if track_input_file_name: with open(track_input_file_name) as f: metadata_track_list = f.readlines() # Clean off trailing '\n's metadata_track_list = [x.rstrip('\n') for x in metadata_track_list] # TODO - Check all track files fit 'UPC_VOL_TRACK.WAV' pattern # if get_metadata_structure(data_source_folder): # logger.info('Metadata has tracks with bad structure.') # report_bad_structure(metadata_track_list) # metadata_track_list = remove_bad_tracks(metadata_track_list) if upc_input_file_name: # Get list of UPC's from the metadata infile. with open(upc_input_file_name) as f: metadata_upc_list = f.readlines() metadata_upc_list = [x.rstrip('\n') for x in metadata_upc_list] # De-dupe UPC's metadata_upc_list = dedupe_list(metadata_upc_list) # Report UPC's logger.warning('{} unique UPC\'s found in Metadata', len(metadata_upc_list)) return metadata_upc_list, metadata_track_list def main(): upc_list, _ = load_metadata() upc_list = sorted(upc_list) logger.bind(file=True).info( 'Creating folder {} if it does not exist.', TARGET_PATH) try: os.makedirs(TARGET_PATH, exist_ok=True) except Exception as e: logger.bind(file=True).error('Could not make log directory. Aborting.') logger.bind(file=True).error(str(e)) sys.exit() # root = '/Volumes/SuburbanCat/The Orchard' for path, subdirs, files in os.walk(SOURCE_PATH): image_list = [] for file in files: check_audio = re.match(cover_file_extensions, file) if check_audio: image_list.append(file) image_file = check_audio.group('image_file') file_upc = image_file.split('.')[0] if START_AT and file_upc < START_AT: continue if file_upc not in upc_list: logger.bind(file=True).error( '{} is not in ingest list.', file) else: logger.bind(file=True).info('Copying file: {}', file) image = os.path.join(path, file) target_path = os.path.join(TARGET_PATH, file) try: if not os.path.exists(target_path): size = os.stat(image).st_size / 1024 / 1024 if size < MAX_SIZE: copy_file(image, target_path) else: logger.bind(file=True).error( '{} is too large. Max size is {} MB, file ' 'is {}MB', file, MAX_SIZE, size) else: logger.bind(file=True).error( 'File already exists. Skipping {}', file) except Exception as e: logger.bind(file=True).error( 'Could not copy {}: {}', file, str(e)) if __name__ == '__main__': main()