-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
22 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
import asyncio | ||
from aiofile import AIOFile, Reader, Writer | ||
import logging | ||
import os | ||
|
||
|
||
class TextWriter(): | ||
def __init__(self, filename: str): | ||
self.filename = filename | ||
|
||
class TextWriter: | ||
def __init__(self, filepath: str): | ||
self.filepath = filepath | ||
self.dir, self.filename = os.path.split(self.filepath) | ||
|
||
async def write(self, urls): | ||
async with AIOFile(self.filename, 'w') as aiodf: | ||
if self.dir and not os.path.exists(self.dir): | ||
os.makedirs(self.dir) | ||
|
||
async with AIOFile(self.filepath, "w") as aiodf: | ||
writer = Writer(aiodf) | ||
|
||
for url in urls: | ||
await writer("{}\n".format(url)) | ||
await aiodf.fsync() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
import asyncio | ||
from aiofile import AIOFile, Reader, Writer | ||
import logging | ||
import os | ||
|
||
|
||
class XMLWriter(): | ||
def __init__(self, filename: str): | ||
self.filename = filename | ||
|
||
class XMLWriter: | ||
def __init__(self, filepath: str): | ||
self.filepath = filepath | ||
self.dir, self.filename = os.path.split(self.filepath) | ||
|
||
async def write(self, urls): | ||
async with AIOFile(self.filename, 'w') as aiodf: | ||
if self.dir and not os.path.exists(self.dir): | ||
os.makedirs(self.dir) | ||
|
||
async with AIOFile(self.filepath, "w") as aiodf: | ||
writer = Writer(aiodf) | ||
await writer('<?xml version="1.0" encoding="utf-8"?>\n') | ||
await writer( | ||
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' | ||
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' | ||
' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n') | ||
' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n' | ||
) | ||
await aiodf.fsync() | ||
for url in urls: | ||
await writer('<url><loc>{}</loc></url>\n'.format(url)) | ||
await writer("<url><loc>{}</loc></url>\n".format(url)) | ||
await aiodf.fsync() | ||
|
||
await writer('</urlset>') | ||
await writer("</urlset>") | ||
await aiodf.fsync() | ||
|