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: image_xml = "" if 'src' in image: image_xml += "{}".format(image['src']) if 'title' in image: image_xml += "{}".format(image['title']) if 'caption' in image: image_xml += "{}".format(image['caption']) if 'geo_location' in image: image_xml += "{}".format(image['geo_location']) if 'license' in image: image_xml += "{}".format(image['license']) url += "{}".format(image_xml) await writer('{}\n'.format(url)) await aiodf.fsync() await writer('') await aiodf.fsync()