mirror of
https://github.com/bcye/structured-wikivoyage-exports.git
synced 2025-09-19 03:14:48 +00:00
add bunny storage handler
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
from .base_handler import BaseHandler
|
||||
from .filesystem import FilesystemHandler
|
||||
from .filesystem import FilesystemHandler
|
||||
from .bunny_storage import BunnyStorageHandler
|
48
output_handlers/bunny_storage.py
Normal file
48
output_handlers/bunny_storage.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import json
|
||||
import aiohttp
|
||||
from .base_handler import BaseHandler
|
||||
|
||||
class BunnyStorageHandler(BaseHandler):
|
||||
def __init__(
|
||||
self,
|
||||
region: str,
|
||||
base_path: str,
|
||||
api_key: str,
|
||||
fail_on_error: bool = True,
|
||||
keepalive_timeout: int = 75,
|
||||
):
|
||||
super().__init__(fail_on_error=fail_on_error)
|
||||
self.region = region
|
||||
self.base_path = base_path
|
||||
self.api_key = api_key
|
||||
|
||||
# no explicit 'limit'; use the default (100)
|
||||
self._connector = aiohttp.TCPConnector(
|
||||
keepalive_timeout=keepalive_timeout
|
||||
)
|
||||
self._session = aiohttp.ClientSession(connector=self._connector)
|
||||
|
||||
async def _write_entry(self, entry: dict, uid: str) -> bool:
|
||||
url = f"https://{self.region}.bunnycdn.com/{self.base_path}/{uid}.json"
|
||||
headers = {
|
||||
"AccessKey": self.api_key,
|
||||
"Content-Type": "application/json",
|
||||
"accept": "application/json",
|
||||
}
|
||||
payload = json.dumps(entry).encode("utf-8")
|
||||
|
||||
try:
|
||||
async with self._session.put(url, data=payload, headers=headers) as resp:
|
||||
if resp.status in (200, 201, 204):
|
||||
return True
|
||||
body = await resp.text()
|
||||
self.logger.error(f"Upload failed UID={uid} status={resp.status} body={body}")
|
||||
return False
|
||||
|
||||
except Exception:
|
||||
self.logger.exception(f"Exception while uploading UID={uid}")
|
||||
return False
|
||||
|
||||
async def close(self):
|
||||
await self._session.close()
|
||||
await self._connector.close()
|
Reference in New Issue
Block a user