Sitemap generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
920 B

  1. import asyncio
  2. from aiofile import AIOFile, Reader, Writer
  3. import logging
  4. class XMLWriter():
  5. def __init__(self, filename: str):
  6. self.filename = filename
  7. async def write(self, urls):
  8. async with AIOFile(self.filename, 'w') as aiodf:
  9. writer = Writer(aiodf)
  10. await writer('<?xml version="1.0" encoding="utf-8"?>\n')
  11. await writer(
  12. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
  13. ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
  14. ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n')
  15. await aiodf.fsync()
  16. for url in urls:
  17. await writer('<url><loc>{}</loc></url>\n'.format(url))
  18. await aiodf.fsync()
  19. await writer('</urlset>')
  20. await aiodf.fsync()