aesgcm-dl/aesgcm-dl.py

61 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 et ts=4 sts=4 sw=4
#
# Copyright © 2022 Maxime “pep” Buquet <pep@bouah.net>
#
# This file is licensed under GPL-3.0-or-later
"""
AESGCM URLs downloader
"""
import sys
import asyncio
from io import BytesIO
from pathlib import Path
from aiohttp import ClientSession
from slixmpp.version import __version__ as slixmpp_version
from slixmpp.plugins.xep_0454 import XEP_0454
class InvalidURI(Exception):
"""Raised when an invalid aesgcm URI is specified"""
def __init__(self, url) -> None:
self.url = url
async def handle_file(uri: str) -> None:
"""Downloads, decrypts, and writes specified URI"""
try:
aesurl, fragment = uri.rsplit('#', 1)
except ValueError as exn:
raise InvalidURI(uri) from exn
url = aesurl.replace('aesgcm://', 'https://')
filename = Path(url.rsplit('/', 1)[1])
async with ClientSession(headers={'User-Agent': 'slixmpp ' + slixmpp_version}) as session:
async with session.get(url) as response:
print(f'Got response for {url}')
input_file = BytesIO(await response.read())
with filename.open('wb') as file:
print(f'Writing to {filename}')
file.write(XEP_0454.decrypt(input_file, fragment))
if __name__ == '__main__':
if len(sys.argv) != 2:
print('usage: aesgcm-dl <URI>', file=sys.stderr)
sys.exit(1)
try:
asyncio.run(handle_file(sys.argv[1]))
except InvalidURI as exn:
print(f'Invalid aesgcm URI: {exn.url}', file=sys.stderr)
sys.exit(2)