Compare commits

...

2 Commits

Author SHA1 Message Date
Casey 3904e475a4
MORE. LOGS. 2022-11-02 20:18:31 +03:00
Casey 94f477bc45
Added configurable logging level 2022-11-02 20:11:38 +03:00
5 changed files with 35 additions and 4 deletions

View File

@ -104,6 +104,10 @@ connection error!!!)
#### modules #### modules
More about them later More about them later
#### loglevel
Self-explanatory, logging level. Can be either `DEBUG`, `INFO`, `WARNING` or
`ERROR`. Defaults to `INFO`
### Modules ### Modules
There's two types of modules supported at this point: `telegram` and `discord`. There's two types of modules supported at this point: `telegram` and `discord`.
Both of them are self-explanatory, but we'll go over them real quick. Both of them are self-explanatory, but we'll go over them real quick.

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from asyncio import run from asyncio import run
from configparser import ConfigParser, ExtendedInterpolation from configparser import ConfigParser, ExtendedInterpolation
from logging import INFO, Formatter, StreamHandler, getLogger from logging import INFO, Formatter, StreamHandler, getLevelName, getLogger
from sys import stdout from sys import stdout
from mastoposter import execute_integrations, load_integrations_from from mastoposter import execute_integrations, load_integrations_from
from mastoposter.integrations import FilteredIntegration from mastoposter.integrations import FilteredIntegration
@ -72,10 +72,10 @@ async def listen(
def main(config_path: str): def main(config_path: str):
init_logger()
conf = ConfigParser(interpolation=ExtendedInterpolation()) conf = ConfigParser(interpolation=ExtendedInterpolation())
conf.read(config_path) conf.read(config_path)
init_logger(getLevelName(conf["main"].get("loglevel", "INFO")))
normalize_config(conf) normalize_config(conf)
modules: List[FilteredIntegration] = load_integrations_from(conf) modules: List[FilteredIntegration] = load_integrations_from(conf)

View File

@ -41,12 +41,14 @@ class DiscordIntegration(BaseIntegration):
logger.debug("Executing webhook with %r", json) logger.debug("Executing webhook with %r", json)
return ( result = (
await c.post( await c.post(
self.webhook, self.webhook,
json=json, json=json,
) )
).json() ).json()
logger.debug("Result: %r", result)
return result
async def __call__(self, status: Status) -> Optional[str]: async def __call__(self, status: Status) -> Optional[str]:
source = status.reblog or status source = status.reblog or status
@ -88,6 +90,11 @@ class DiscordIntegration(BaseIntegration):
), ),
) )
) )
else:
logger.warn(
"Unsupported attachment %r for Discord Embed",
attachment.type,
)
await self.execute_webhook( await self.execute_webhook(
username=status.account.acct, username=status.account.acct,

View File

@ -88,12 +88,15 @@ class TelegramIntegration(BaseIntegration):
async def _tg_request(self, method: str, **kwargs) -> TGResponse: async def _tg_request(self, method: str, **kwargs) -> TGResponse:
url = API_URL.format(self.token, method) url = API_URL.format(self.token, method)
async with AsyncClient() as client: async with AsyncClient() as client:
logger.debug("TG request: %s(%r)", method, kwargs)
response = TGResponse.from_dict( response = TGResponse.from_dict(
(await client.post(url, json=kwargs)).json(), kwargs (await client.post(url, json=kwargs)).json(), kwargs
) )
if not response.ok: if not response.ok:
logger.error("TG error: %r", response.error) logger.error("TG error: %r", response.error)
logger.error("parameters: %r", kwargs) logger.error("parameters: %r", kwargs)
else:
logger.debug("Result: %r", response.result)
return response return response
async def _post_plaintext(self, text: str) -> TGResponse: async def _post_plaintext(self, text: str) -> TGResponse:

View File

@ -1,10 +1,13 @@
from asyncio import exceptions from asyncio import exceptions
from json import loads from json import loads
from logging import getLogger
from typing import AsyncGenerator from typing import AsyncGenerator
from urllib.parse import urlencode from urllib.parse import urlencode
from mastoposter.types import Status from mastoposter.types import Status
logger = getLogger("sources")
async def websocket_source( async def websocket_source(
url: str, reconnect: bool = False, **params url: str, reconnect: bool = False, **params
@ -22,6 +25,20 @@ async def websocket_source(
raise Exception(event["error"]) raise Exception(event["error"])
if event["event"] == "update": if event["event"] == "update":
yield Status.from_dict(loads(event["payload"])) yield Status.from_dict(loads(event["payload"]))
except (WebSocketException, TimeoutError, exceptions.TimeoutError): else:
logger.warn("unknown event type %r", event["event"])
logger.debug("data: %r", event)
except (
WebSocketException,
TimeoutError,
exceptions.TimeoutError,
) as e:
if not reconnect: if not reconnect:
raise raise
else:
logger.warn("%r caught, reconnecting", e)
else:
logger.info(
"WebSocket closed connection without any errors, "
"but we're not done yet"
)