import asyncio from aiofile import AIOFile, Reader, Writer import logging from datetime import datetime, timezone, timedelta class XMLWriter(): def __init__(self, filename: str): self.filename = filename async def write(self, urls, timezone_offset): async with AIOFile(self.filename, 'w') as aiodf: writer = Writer(aiodf) await writer('\n') await writer( '\n') await aiodf.fsync() for data in urls: timestamp = data[1][1] changefreq = data[1][2] priority = data[1][3] image_data = data[1][4] url = "{}".format(data[0]) if timestamp is not None: timestamp = datetime.strptime(timestamp, "%a, %d %b %Y %H:%M:%S %Z").astimezone(tz=timezone(timedelta(hours=timezone_offset))).isoformat() url += "{}".format(str(timestamp)) if changefreq is not None: url += "{}".format(str(changefreq)) if priority is not None: url += "{}".format(str(priority)) if len(image_data) > 0: for image in image_data: for arg in image: image_xml = "" if 'src' in arg: image_xml += "{}".format(arg['src']) if 'title' in arg: image_xml += "{}".format(arg['title']) if 'caption' in arg: image_xml += "{}".format(arg['caption']) if 'geo_location' in arg: image_xml += "{}".format(arg['geo_location']) if 'license' in arg: image_xml += "{}".format(arg['license']) url += "{}".format(image_xml) await writer('{}\n'.format(url)) await aiodf.fsync() await writer('') await aiodf.fsync()