mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-11-15 03:04:28 +00:00
Black/isort
This commit is contained in:
parent
c5295524c7
commit
d9362adb25
16 changed files with 814 additions and 672 deletions
|
@ -1,23 +1,30 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Any
|
||||||
|
from typing import Dict
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from bson.objectid import ObjectId
|
from bson.objectid import ObjectId
|
||||||
from html2text import html2text
|
|
||||||
from feedgen.feed import FeedGenerator
|
from feedgen.feed import FeedGenerator
|
||||||
|
from html2text import html2text
|
||||||
|
|
||||||
|
import tasks
|
||||||
|
from config import BASE_URL
|
||||||
|
from config import DB
|
||||||
|
from config import ID
|
||||||
|
from config import ME
|
||||||
|
from config import USER_AGENT
|
||||||
|
from config import USERNAME
|
||||||
from little_boxes import activitypub as ap
|
from little_boxes import activitypub as ap
|
||||||
from little_boxes.backend import Backend
|
from little_boxes.backend import Backend
|
||||||
from little_boxes.collection import parse_collection as ap_parse_collection
|
from little_boxes.collection import parse_collection as ap_parse_collection
|
||||||
|
|
||||||
from config import USERNAME, BASE_URL, ID
|
|
||||||
from config import DB, ME
|
|
||||||
import tasks
|
|
||||||
|
|
||||||
from typing import List, Optional, Dict, Any, Union
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
MY_PERSON = ap.Person(**ME)
|
||||||
|
|
||||||
|
|
||||||
def _remove_id(doc: ap.ObjectType) -> ap.ObjectType:
|
def _remove_id(doc: ap.ObjectType) -> ap.ObjectType:
|
||||||
"""Helper for removing MongoDB's `_id` field."""
|
"""Helper for removing MongoDB's `_id` field."""
|
||||||
|
@ -35,6 +42,9 @@ def _to_list(data: Union[List[Any], Any]) -> List[Any]:
|
||||||
|
|
||||||
|
|
||||||
class MicroblogPubBackend(Backend):
|
class MicroblogPubBackend(Backend):
|
||||||
|
def user_agent(self) -> str:
|
||||||
|
return USER_AGENT
|
||||||
|
|
||||||
def base_url(self) -> str:
|
def base_url(self) -> str:
|
||||||
return BASE_URL
|
return BASE_URL
|
||||||
|
|
||||||
|
|
17
config.py
17
config.py
|
@ -1,13 +1,16 @@
|
||||||
import subprocess
|
|
||||||
import os
|
import os
|
||||||
import yaml
|
import subprocess
|
||||||
from pymongo import MongoClient
|
|
||||||
import requests
|
|
||||||
from itsdangerous import JSONWebSignatureSerializer
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from little_boxes.utils import strtobool
|
import requests
|
||||||
from utils.key import KEY_DIR, get_key, get_secret_key
|
import yaml
|
||||||
|
from itsdangerous import JSONWebSignatureSerializer
|
||||||
|
from pymongo import MongoClient
|
||||||
|
|
||||||
|
from little_boxes import strtobool
|
||||||
|
from utils.key import KEY_DIR
|
||||||
|
from utils.key import get_key
|
||||||
|
from utils.key import get_secret_key
|
||||||
|
|
||||||
|
|
||||||
def noop():
|
def noop():
|
||||||
|
|
|
@ -4,3 +4,4 @@ html2text
|
||||||
pyyaml
|
pyyaml
|
||||||
flake8
|
flake8
|
||||||
mypy
|
mypy
|
||||||
|
black
|
||||||
|
|
58
tasks.py
58
tasks.py
|
@ -1,47 +1,53 @@
|
||||||
import os
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import random
|
import random
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from celery import Celery
|
from celery import Celery
|
||||||
from requests.exceptions import HTTPError
|
from requests.exceptions import HTTPError
|
||||||
|
|
||||||
|
from config import DB
|
||||||
from config import HEADERS
|
from config import HEADERS
|
||||||
from config import ID
|
from config import ID
|
||||||
from config import DB
|
|
||||||
from config import KEY
|
from config import KEY
|
||||||
from config import USER_AGENT
|
from config import USER_AGENT
|
||||||
from utils.httpsig import HTTPSigAuth
|
from utils.httpsig import HTTPSigAuth
|
||||||
from utils.opengraph import fetch_og_metadata
|
|
||||||
from utils.linked_data_sig import generate_signature
|
from utils.linked_data_sig import generate_signature
|
||||||
|
from utils.opengraph import fetch_og_metadata
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
app = Celery('tasks', broker=os.getenv('MICROBLOGPUB_AMQP_BROKER', 'pyamqp://guest@localhost//'))
|
app = Celery(
|
||||||
SigAuth = HTTPSigAuth(ID+'#main-key', KEY.privkey)
|
"tasks", broker=os.getenv("MICROBLOGPUB_AMQP_BROKER", "pyamqp://guest@localhost//")
|
||||||
|
)
|
||||||
|
SigAuth = HTTPSigAuth(ID + "#main-key", KEY.privkey)
|
||||||
|
|
||||||
|
|
||||||
@app.task(bind=True, max_retries=12)
|
@app.task(bind=True, max_retries=12)
|
||||||
def post_to_inbox(self, payload: str, to: str) -> None:
|
def post_to_inbox(self, payload: str, to: str) -> None:
|
||||||
try:
|
try:
|
||||||
log.info('payload=%s', payload)
|
log.info("payload=%s", payload)
|
||||||
log.info('generating sig')
|
log.info("generating sig")
|
||||||
signed_payload = json.loads(payload)
|
signed_payload = json.loads(payload)
|
||||||
generate_signature(signed_payload, KEY.privkey)
|
generate_signature(signed_payload, KEY.privkey)
|
||||||
log.info('to=%s', to)
|
log.info("to=%s", to)
|
||||||
resp = requests.post(to, data=json.dumps(signed_payload), auth=SigAuth, headers={
|
resp = requests.post(
|
||||||
'Content-Type': HEADERS[1],
|
to,
|
||||||
'Accept': HEADERS[1],
|
data=json.dumps(signed_payload),
|
||||||
'User-Agent': USER_AGENT,
|
auth=SigAuth,
|
||||||
})
|
headers={
|
||||||
log.info('resp=%s', resp)
|
"Content-Type": HEADERS[1],
|
||||||
log.info('resp_body=%s', resp.text)
|
"Accept": HEADERS[1],
|
||||||
|
"User-Agent": USER_AGENT,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
log.info("resp=%s", resp)
|
||||||
|
log.info("resp_body=%s", resp.text)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
except HTTPError as err:
|
except HTTPError as err:
|
||||||
log.exception('request failed')
|
log.exception("request failed")
|
||||||
if 400 >= err.response.status_code >= 499:
|
if 400 >= err.response.status_code >= 499:
|
||||||
log.info('client error, no retry')
|
log.info("client error, no retry")
|
||||||
return
|
return
|
||||||
self.retry(exc=err, countdown=int(random.uniform(2, 4) ** self.request.retries))
|
self.retry(exc=err, countdown=int(random.uniform(2, 4) ** self.request.retries))
|
||||||
|
|
||||||
|
@ -49,11 +55,15 @@ def post_to_inbox(self, payload: str, to: str) -> None:
|
||||||
@app.task(bind=True, max_retries=12)
|
@app.task(bind=True, max_retries=12)
|
||||||
def fetch_og(self, col, remote_id):
|
def fetch_og(self, col, remote_id):
|
||||||
try:
|
try:
|
||||||
log.info('fetch_og_meta remote_id=%s col=%s', remote_id, col)
|
log.info("fetch_og_meta remote_id=%s col=%s", remote_id, col)
|
||||||
if col == 'INBOX':
|
if col == "INBOX":
|
||||||
log.info('%d links saved', fetch_og_metadata(USER_AGENT, DB.inbox, remote_id))
|
log.info(
|
||||||
elif col == 'OUTBOX':
|
"%d links saved", fetch_og_metadata(USER_AGENT, DB.inbox, remote_id)
|
||||||
log.info('%d links saved', fetch_og_metadata(USER_AGENT, DB.outbox, remote_id))
|
)
|
||||||
|
elif col == "OUTBOX":
|
||||||
|
log.info(
|
||||||
|
"%d links saved", fetch_og_metadata(USER_AGENT, DB.outbox, remote_id)
|
||||||
|
)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
self.log.exception('failed')
|
self.log.exception("failed")
|
||||||
self.retry(exc=err, countdown=int(random.uniform(2, 4) ** self.request.retries))
|
self.retry(exc=err, countdown=int(random.uniform(2, 4) ** self.request.retries))
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import time
|
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
from typing import List
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from html2text import html2text
|
from html2text import html2text
|
||||||
from utils import activitypub_utils
|
|
||||||
|
|
||||||
from typing import Tuple
|
from utils import activitypub_utils
|
||||||
from typing import List
|
|
||||||
|
|
||||||
|
|
||||||
def resp2plaintext(resp):
|
def resp2plaintext(resp):
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
from typing import Optional, Dict, List, Any
|
from typing import Any
|
||||||
|
from typing import Dict
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import logging
|
import logging
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from urllib.parse import urlparse
|
|
||||||
from Crypto.PublicKey import RSA
|
from Crypto.PublicKey import RSA
|
||||||
|
|
||||||
from .urlutils import check_url
|
|
||||||
from .errors import ActivityNotFoundError
|
from .errors import ActivityNotFoundError
|
||||||
|
from .urlutils import check_url
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,21 @@
|
||||||
import typing
|
import re
|
||||||
import re
|
import typing
|
||||||
|
from typing import Any
|
||||||
|
from typing import Dict
|
||||||
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Tuple
|
||||||
|
from typing import Type
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from bleach.linkifier import Linker
|
from bleach.linkifier import Linker
|
||||||
from markdown import markdown
|
from markdown import markdown
|
||||||
|
|
||||||
from utils.webfinger import get_actor_url
|
from config import ACTOR_SERVICE
|
||||||
from config import USERNAME, BASE_URL, ID
|
from config import BASE_URL
|
||||||
from config import ACTOR_SERVICE
|
from config import ID
|
||||||
|
from config import USERNAME
|
||||||
from typing import List, Optional, Tuple, Dict, Any, Union, Type
|
from utils.webfinger import get_actor_url
|
||||||
|
|
||||||
|
|
||||||
def set_attrs(attrs, new=False):
|
def set_attrs(attrs, new=False):
|
||||||
|
|
|
@ -3,19 +3,20 @@
|
||||||
Mastodon instances won't accept requests that are not signed using this scheme.
|
Mastodon instances won't accept requests that are not signed using this scheme.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from datetime import datetime
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
from typing import Any, Dict, Optional
|
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Any
|
||||||
|
from typing import Dict
|
||||||
|
from typing import Optional
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from Crypto.Hash import SHA256
|
||||||
|
from Crypto.Signature import PKCS1_v1_5
|
||||||
from flask import request
|
from flask import request
|
||||||
from requests.auth import AuthBase
|
from requests.auth import AuthBase
|
||||||
|
|
||||||
from Crypto.Signature import PKCS1_v1_5
|
|
||||||
from Crypto.Hash import SHA256
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import os
|
|
||||||
import binascii
|
import binascii
|
||||||
|
import os
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
from little_boxes.key import Key
|
from little_boxes.key import Key
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
from pyld import jsonld
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Any
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
from Crypto.Signature import PKCS1_v1_5
|
|
||||||
from Crypto.Hash import SHA256
|
from Crypto.Hash import SHA256
|
||||||
import base64
|
from Crypto.Signature import PKCS1_v1_5
|
||||||
|
from pyld import jsonld
|
||||||
from typing import Any, Dict
|
|
||||||
|
|
||||||
|
|
||||||
# cache the downloaded "schemas", otherwise the library is super slow
|
# cache the downloaded "schemas", otherwise the library is super slow
|
||||||
# (https://github.com/digitalbazaar/pyld/issues/70)
|
# (https://github.com/digitalbazaar/pyld/issues/70)
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import requests
|
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from .urlutils import check_url
|
import requests
|
||||||
|
|
||||||
from .errors import ActivityNotFoundError
|
from .errors import ActivityNotFoundError
|
||||||
|
from .urlutils import check_url
|
||||||
|
|
||||||
|
|
||||||
class ObjectService(object):
|
class ObjectService(object):
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
|
import ipaddress
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import ipaddress
|
|
||||||
import opengraph
|
import opengraph
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
from .urlutils import is_url_valid, check_url
|
from .urlutils import check_url
|
||||||
|
from .urlutils import is_url_valid
|
||||||
|
|
||||||
|
|
||||||
def links_from_note(note):
|
def links_from_note(note):
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
import ipaddress
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import ipaddress
|
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from . import strtobool
|
from . import strtobool
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
from urllib.parse import urlparse
|
|
||||||
from typing import Dict, Any
|
|
||||||
from typing import Optional
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
from typing import Dict
|
||||||
|
from typing import Optional
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from .urlutils import check_url
|
from .urlutils import check_url
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue