"""Docstring.""" import asyncio import os import sys import xml.etree.ElementTree as ET import aiohttp async def main(): """Entrypoint.""" dms_id = int(sys.argv[1]) upc = sys.argv[2] filename = f'{dms_id}_{upc}.xml' if os.path.isfile(filename): with open(filename, 'rb') as f: xml = f.read().decode('utf-8') else: xml = await fetch(dms_id, upc) with open(filename, 'wb') as f: f.write(xml.encode('utf-8')) xml = '\n'.join(xml.split('\n')[1:-1]) root = ET.fromstring(xml) print(root) if dms_id == 1: path = '/'.join( [ f'{{http://apple.com/itunes/importer}}{x}' for x in ['album', 'tracks', 'track'] ] ) for track in root.findall(path): print(track) # audio_file else: for track in root.findall('ResourceList/SoundRecording'): print(track) # SoundRecordingDetailsByTerritory / TechnicalSoundRecordingDetails async def fetch(dms_id, upc): """Get XML from ows-metadata.""" if dms_id == 1: url = f'https://webservices.qaorch.com/oa/metadata/generate/dms_id/{dms_id}/subformat/music/returnErr/1/xml_only/0/delivery_type/complete_album/upc/{upc}' # noqa:E501 else: url = f'https://webservices.qaorch.com/oa/metadata/generate/dms_id/{dms_id}/storename/ddexern/version/3.8.0/returnErr/1/xml_only/0/delivery_type/complete_album/upc/{upc}' # noqa:E501 async with aiohttp.ClientSession() as session: # noqa:E501 async with session.get(url) as response: body = await response.text() return body if __name__ == '__main__': asyncio.run(main())