forked from KayFaraday/pleroma-ebooks
Compare commits
No commits in common. "970903c3c007ad25b2a63b32e483dc42fb232366" and "879eb32b2d0e902b9a760a0ff03965d9b5b59821" have entirely different histories.
970903c3c0
...
879eb32b2d
|
@ -4,6 +4,7 @@
|
||||||
import sys
|
import sys
|
||||||
import anyio
|
import anyio
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import platform
|
||||||
import pendulum
|
import pendulum
|
||||||
import operator
|
import operator
|
||||||
import aiosqlite
|
import aiosqlite
|
||||||
|
@ -13,9 +14,15 @@ from pleroma import Pleroma
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from typing import Iterable, NewType
|
from typing import Iterable, NewType
|
||||||
from utils import shield, HandleRateLimits, suppress, http_session_factory
|
from utils import shield, HandleRateLimits, suppress
|
||||||
from third_party.utils import extract_post_content
|
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')
|
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'
|
||||||
|
@ -33,8 +40,9 @@ class PostFetcher:
|
||||||
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(
|
||||||
http_session_factory(
|
aiohttp.ClientSession(
|
||||||
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,
|
||||||
|
|
|
@ -4,8 +4,6 @@ import sqlite3
|
||||||
from random import expovariate
|
from random import expovariate
|
||||||
import typing
|
import typing
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from utils import http_session_factory
|
|
||||||
|
|
||||||
|
|
||||||
async def make_sentence(cfg):
|
async def make_sentence(cfg):
|
||||||
# set default
|
# set default
|
||||||
|
@ -83,8 +81,8 @@ async def make_sentence(cfg):
|
||||||
# using aiohttp
|
# using aiohttp
|
||||||
|
|
||||||
post = None
|
post = None
|
||||||
async with http_session_factory() as session:
|
while post is None:
|
||||||
while post is None:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(
|
async with session.post(
|
||||||
"https://api.textsynth.com/v1/engines/{}/completions".format(cfg["textsynth_engine_id"]),
|
"https://api.textsynth.com/v1/engines/{}/completions".format(cfg["textsynth_engine_id"]),
|
||||||
headers={
|
headers={
|
||||||
|
@ -105,14 +103,14 @@ async def make_sentence(cfg):
|
||||||
continue
|
continue
|
||||||
post: str = data["text"]
|
post: str = data["text"]
|
||||||
|
|
||||||
# check wether the post only consists of mentions
|
# check wether the post only consists of mentions
|
||||||
# split by words
|
# split by words
|
||||||
words = post.split()
|
words = post.split()
|
||||||
# check if all words are mentions
|
# check if all words are mentions
|
||||||
if all(word.startswith("@") for word in words):
|
if all(word.startswith("@") for word in words):
|
||||||
# generate a new sentence
|
# generate a new sentence
|
||||||
post = None
|
post = None
|
||||||
continue
|
continue
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
12
pleroma.py
12
pleroma.py
|
@ -6,7 +6,17 @@ import json
|
||||||
import hashlib
|
import hashlib
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from utils import http_session_factory
|
|
||||||
|
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},
|
||||||
|
)
|
||||||
|
|
||||||
class BadRequest(Exception):
|
class BadRequest(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
13
utils.py
13
utils.py
|
@ -1,23 +1,10 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
import anyio
|
import anyio
|
||||||
import aiohttp
|
|
||||||
import platform
|
|
||||||
import contextlib
|
import contextlib
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from datetime import datetime, timezone
|
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):
|
def as_corofunc(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
async def wrapped(*args, **kwargs):
|
async def wrapped(*args, **kwargs):
|
||||||
|
|
Loading…
Reference in New Issue