"""Apple packager.""" import xml.etree.ElementTree as ET from src.packagers.base import AbstractPackager class ApplePackager(AbstractPackager): """Apple specific package functions.""" dms_id = 1 def get_xml_url(self, stereo_upc, spatial_upc): """Format xml fetching url specifically for this dms.""" return f'/oa/metadata/generate/dms_id/{self.dms_id}/subformat/music/returnErr/1/xml_only/0/delivery_type/complete_album/upc/{stereo_upc}' # noqa:E501 def transform_xml( self, input_xml, stereo_metadata, spatial_metadata, spatial_audio_details, spatial_artwork_details): """Format xml specifically for this dms.""" namespace = 'http://apple.com/itunes/importer' spatial_files = self._format_spatial_file_list(spatial_audio_details) ET.register_namespace('', namespace) root = ET.fromstring(input_xml) for album in root.findall(f'{{{namespace}}}album'): # remove artwork_files for artwork_files in album.findall(f'{{{namespace}}}artwork_files'): # noqa:E501 album.remove(artwork_files) for tracks in album.findall(f'{{{namespace}}}tracks'): for track in tracks.findall(f'{{{namespace}}}track'): # remove audio_file or assets for tag in ('audio_file', 'assets'): for el in track.findall(f'{{{namespace}}}{tag}'): track.remove(el) # add assets if spatial file exists volume_index = \ int(track.find(f'{{{namespace}}}volume_number').text) track_index = \ int(track.find(f'{{{namespace}}}track_number').text) spatial_file = ( spatial_files .get(volume_index, {}) .get(track_index, None) ) if spatial_file: assets = ET.SubElement(track, 'assets') asset = ( ET.SubElement( assets, 'asset', {'type': 'full'} ) ) data_file = ( ET.SubElement( asset, 'data_file', {'role': 'audio.object_based'} ) ) ET.SubElement( data_file, 'file_name' ).text = self.output_audio_filename(spatial_file['path']) # noqa:E501 ET.SubElement( data_file, 'size' ).text = str(spatial_file['size']) ET.SubElement( data_file, 'checksum', {'type': 'md5'} ).text = spatial_file['md5'] ET.SubElement( data_file, 'attribute', {'name': 'external_identifier.isrc'} ).text = spatial_file['isrc'] # remove booklet for booklet in tracks.findall(f'{{{namespace}}}booklet'): tracks.remove(booklet) # remove video assets for video in tracks.findall(f'{{{namespace}}}video'): for assets in video.findall(f'{{{namespace}}}assets'): video.remove(assets) return self.format_xml(root) async def download_artwork(self, product_id): """Download image specifically for this dms.""" pass def get_image_details(self, filename): """Do nothing, no image for this dms.""" pass def transform_image(self, image_location): """Do nothing, no image for this dms.""" pass def analyze_image(self, image_location): """Do nothing, no image for this dms.""" pass def output_base_path(self, job_id, spatial_upc, stereo_upc): """DMS specfic upload dir.""" return f'{job_id}/{stereo_upc}.itmsp' def output_metadata_filepath(self, base, upc): """DMS specific metadata upload location.""" return f'{base}/metadata.xml' def output_image_filepath(self, base, upc): """DMS specific image upload location.""" raise NotImplementedError() def output_audio_path(self, base, upc): """DMS specific audio upload location.""" return base