deduplicate user-agent setting code
This commit is contained in:
parent
bc5e8af32b
commit
5e50bccd53
|
@ -4,7 +4,6 @@
|
|||
import sys
|
||||
import anyio
|
||||
import aiohttp
|
||||
import platform
|
||||
import pendulum
|
||||
import operator
|
||||
import aiosqlite
|
||||
|
@ -14,15 +13,9 @@ from pleroma import Pleroma
|
|||
from bs4 import BeautifulSoup
|
||||
from functools import partial
|
||||
from typing import Iterable, NewType
|
||||
from utils import shield, HandleRateLimits, suppress
|
||||
from utils import shield, HandleRateLimits, suppress, http_session_factory
|
||||
from third_party.utils import extract_post_content
|
||||
|
||||
USER_AGENT = (
|
||||
'pleroma-ebooks; '
|
||||
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'
|
||||
|
@ -40,9 +33,8 @@ class PostFetcher:
|
|||
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,
|
||||
|
|
12
pleroma.py
12
pleroma.py
|
@ -6,17 +6,7 @@ import json
|
|||
import hashlib
|
||||
import aiohttp
|
||||
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 utils import http_session_factory
|
||||
|
||||
class BadRequest(Exception):
|
||||
pass
|
||||
|
|
13
utils.py
13
utils.py
|
@ -1,10 +1,23 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import anyio
|
||||
import aiohttp
|
||||
import platform
|
||||
import contextlib
|
||||
from functools import wraps
|
||||
from datetime import datetime, timezone
|
||||
|
||||
def http_session_factory(headers={}, **kwargs):
|
||||
user_agent = (
|
||||
'pleroma-ebooks (https://lab.freak.university/KayFaraday/pleroma-ebooks); '
|
||||
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):
|
||||
|
|
Loading…
Reference in New Issue