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] images = 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(images) > 0: for image in images: url += "{}".format(str(image)) await writer('{}\n'.format(url)) await aiodf.fsync() await writer('') await aiodf.fsync()