Compare commits

...

2 Commits

Author SHA1 Message Date
Kay Faraday b1a9b37c5e deduplicate aiohttp session creation 2022-07-02 23:09:46 +00:00
Kay Faraday 6e9cae820a gitignore __pycache__/ 2022-07-02 23:06:45 +00:00
4 changed files with 18 additions and 23 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
__pycache__/
*.toml *.toml
!*.example.toml !*.example.toml
*.timestamp *.timestamp

View File

@ -8,22 +8,14 @@ import cursor
import aiohttp import aiohttp
import pleroma import pleroma
import argparse import argparse
import platform
import pendulum import pendulum
import aiosqlite import aiosqlite
import contextlib import contextlib
import qtoml as toml import qtoml as toml
from utils import suppress, loading_spinner
from pleroma import Pleroma from pleroma import Pleroma
from functools import partial 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' JSON_CONTENT_TYPE = 'application/json'
ACTIVITYPUB_CONTENT_TYPE = 'application/activity+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']), Pleroma(api_base_url=self.config['site'], access_token=self.config['access_token']),
) )
self._http = await stack.enter_async_context( self._http = await stack.enter_async_context(
aiohttp.ClientSession( http_session_factory(
headers={ headers={
'User-Agent': USER_AGENT,
'Accept': ', '.join([JSON_CONTENT_TYPE, ACTIVITYPUB_CONTENT_TYPE]), 'Accept': ', '.join([JSON_CONTENT_TYPE, ACTIVITYPUB_CONTENT_TYPE]),
}, },
trust_env=True, trust_env=True,

View File

@ -5,19 +5,9 @@ import yarl
import json import json
import hashlib import hashlib
import aiohttp import aiohttp
from multidict import MultiDict
from http import HTTPStatus from http import HTTPStatus
from multidict import MultiDict
def http_session_factory(headers={}): from utils import http_session_factory
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},
)
class BadRequest(Exception): class BadRequest(Exception):
pass pass

View File

@ -1,10 +1,23 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import anyio import anyio
import aiohttp
import platform
import itertools import itertools
import contextlib import contextlib
from functools import wraps 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): def as_corofunc(f):
@wraps(f) @wraps(f)
async def wrapped(*args, **kwargs): async def wrapped(*args, **kwargs):