Compare commits
2 Commits
ef99209d2c
...
b1a9b37c5e
Author | SHA1 | Date |
---|---|---|
Kay Faraday | b1a9b37c5e | |
Kay Faraday | 6e9cae820a |
|
@ -1,3 +1,4 @@
|
||||||
|
__pycache__/
|
||||||
*.toml
|
*.toml
|
||||||
!*.example.toml
|
!*.example.toml
|
||||||
*.timestamp
|
*.timestamp
|
||||||
|
|
|
@ -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,
|
||||||
|
|
14
pleroma.py
14
pleroma.py
|
@ -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
|
||||||
|
|
13
utils.py
13
utils.py
|
@ -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):
|
||||||
|
|
Loading…
Reference in New Issue