1
0
Fork 0

More logging?

This commit is contained in:
Casey 2022-11-01 13:37:47 +03:00
parent 1ec78d5f53
commit 95c9a523e0
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
4 changed files with 28 additions and 3 deletions

View File

@ -19,12 +19,13 @@ VERIFY_CREDS_TEMPLATE = "https://{instance}/api/v1/accounts/verify_credentials"
logger = getLogger()
def init_logger():
def init_logger(loglevel: int = DEBUG):
stdout_handler = StreamHandler(stdout)
stdout_handler.setLevel(DEBUG)
formatter = Formatter("[%(asctime)s][%(levelname)5s:%(name)s] %(message)s")
stdout_handler.setFormatter(formatter)
logger.addHandler(stdout_handler)
logger.setLevel(loglevel)
async def listen(

View File

@ -15,7 +15,7 @@ logger = getLogger(__name__)
def run_filters(filters: List[FilterInstance], status: Status) -> bool:
logger.debug("Running filters on %r", status)
logger.debug("Running filters on %r", status.id)
if not filters:
logger.debug("No filters, returning True")

View File

@ -1,4 +1,5 @@
from configparser import SectionProxy
from logging import getLogger
from typing import List, Optional
from httpx import AsyncClient
from zlib import crc32
@ -10,6 +11,8 @@ from mastoposter.integrations.discord.types import (
)
from mastoposter.types import Status
logger = getLogger(__name__)
class DiscordIntegration(BaseIntegration):
def __init__(self, webhook: str):
@ -35,6 +38,9 @@ class DiscordIntegration(BaseIntegration):
if embeds is not None
else [],
}
logger.debug("Executing webhook with %r", json)
return (
await c.post(
self.webhook,

View File

@ -1,5 +1,6 @@
from configparser import SectionProxy
from dataclasses import dataclass
from logging import getLogger
from typing import Any, List, Mapping, Optional
from httpx import AsyncClient
from jinja2 import Template
@ -8,6 +9,9 @@ from mastoposter.types import Attachment, Poll, Status
from emoji import emojize
logger = getLogger(__name__)
@dataclass
class TGResponse:
ok: bool
@ -89,6 +93,7 @@ class TelegramIntegration(BaseIntegration):
)
async def _post_plaintext(self, text: str) -> TGResponse:
logger.debug("Sending HTML message: %r", text)
return await self._tg_request(
"sendMessage",
parse_mode="HTML",
@ -101,6 +106,9 @@ class TelegramIntegration(BaseIntegration):
async def _post_media(self, text: str, media: Attachment) -> TGResponse:
# Just to be safe
if media.type not in MEDIA_MAPPING:
logger.warning(
"Media %r has unknown type, falling back to plaintext", media
)
return await self._post_plaintext(text)
return await self._tg_request(
@ -116,6 +124,7 @@ class TelegramIntegration(BaseIntegration):
async def _post_mediagroup(
self, text: str, media: List[Attachment]
) -> TGResponse:
logger.debug("Sendind media group: %r (text=%r)", media, text)
media_list: List[dict] = []
allowed_medias = {"image", "gifv", "video", "audio", "unknown"}
for attachment in media:
@ -149,6 +158,7 @@ class TelegramIntegration(BaseIntegration):
async def _post_poll(
self, poll: Poll, reply_to: Optional[str] = None
) -> TGResponse:
logger.debug("Sending poll: %r", poll)
return await self._tg_request(
"sendPoll",
disable_notification=self.silent,
@ -198,9 +208,17 @@ class TelegramIntegration(BaseIntegration):
return str.join(",", map(str, ids))
def __repr__(self) -> str:
bot_uid, key = self.token.split(":")
return (
"<TelegramIntegration "
"chat_id={chat!r} "
"template={template!r} "
"token={bot_uid}:{key} "
"silent={silent!r}>"
).format(chat=self.chat_id, silent=self.silent, template=self.template)
).format(
chat=self.chat_id,
silent=self.silent,
template=self.template,
bot_uid=bot_uid,
key=str.join("", ("X" for _ in key)),
)