diff --git a/mirror_bot.py b/mirror_bot.py index 8f74544..5b97c00 100755 --- a/mirror_bot.py +++ b/mirror_bot.py @@ -8,22 +8,14 @@ import cursor import aiohttp import pleroma import argparse -import platform import pendulum import aiosqlite import contextlib import qtoml as toml -from utils import suppress, loading_spinner from pleroma import Pleroma from functools import partial +from utils import suppress, loading_spinner, http_session_factory -USER_AGENT = ( - 'mirror-bot; ' - f'{aiohttp.__version__}; ' - f'{platform.python_implementation()}/{platform.python_version()}' -) - -UTC = pendulum.timezone('UTC') JSON_CONTENT_TYPE = 'application/json' ACTIVITYPUB_CONTENT_TYPE = 'application/activity+json' @@ -37,9 +29,8 @@ class PostMirror: Pleroma(api_base_url=self.config['site'], access_token=self.config['access_token']), ) self._http = await stack.enter_async_context( - aiohttp.ClientSession( + http_session_factory( headers={ - 'User-Agent': USER_AGENT, 'Accept': ', '.join([JSON_CONTENT_TYPE, ACTIVITYPUB_CONTENT_TYPE]), }, trust_env=True, diff --git a/pleroma.py b/pleroma.py index dae25ef..d07b571 100644 --- a/pleroma.py +++ b/pleroma.py @@ -5,19 +5,9 @@ import yarl import json import hashlib import aiohttp -from multidict import MultiDict from http import HTTPStatus - -def http_session_factory(headers={}): - py_version = '.'.join(map(str, sys.version_info)) - user_agent = ( - 'pleroma-ebooks (https://lab.freak.university/KayFaraday/pleroma-ebooks); ' - 'aiohttp/{aiohttp.__version__}; ' - 'python/{py_version}' - ) - return aiohttp.ClientSession( - headers={'User-Agent': user_agent, **headers}, - ) +from multidict import MultiDict +from utils import http_session_factory class BadRequest(Exception): pass diff --git a/utils.py b/utils.py index e85fea0..d1dcfaf 100644 --- a/utils.py +++ b/utils.py @@ -1,10 +1,23 @@ # SPDX-License-Identifier: AGPL-3.0-only import anyio +import aiohttp +import platform import itertools import contextlib from functools import wraps +def http_session_factory(headers={}, **kwargs): + user_agent = ( + 'mirror-bot (https://lab.freak.university/KayFaraday/mirror-bot); ' + f'aiohttp/{aiohttp.__version__}; ' + f'{platform.python_implementation()}/{platform.python_version()}' + ) + return aiohttp.ClientSession( + headers={'User-Agent': user_agent, **headers}, + **kwargs, + ) + def as_corofunc(f): @wraps(f) async def wrapped(*args, **kwargs):