1
0
Fork 0

Uh, now it should post every media

In separate messages, if they're incompatible with each other
idk i barely tested it, but it seems to be working
This commit is contained in:
Casey 2023-03-07 11:17:46 +03:00
parent 854bb859ec
commit b789cc6015
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 28 additions and 16 deletions

View File

@ -1,7 +1,7 @@
from configparser import SectionProxy from configparser import SectionProxy
from dataclasses import dataclass from dataclasses import dataclass
from logging import getLogger from logging import getLogger
from typing import Any, List, Mapping, Optional from typing import Any, List, Mapping, Optional, Tuple
from httpx import AsyncClient, AsyncHTTPTransport from httpx import AsyncClient, AsyncHTTPTransport
from jinja2 import Template from jinja2 import Template
from mastoposter.integrations.base import BaseIntegration from mastoposter.integrations.base import BaseIntegration
@ -160,13 +160,12 @@ class TelegramIntegration(BaseIntegration):
text: str, text: str,
media: List[Attachment], media: List[Attachment],
spoiler: bool = False, spoiler: bool = False,
) -> TGResponse: ) -> Tuple[TGResponse, List[Attachment]]:
logger.debug("Sendind media group: %r (text=%r)", media, text) logger.debug("Sendind media group: %r (text=%r)", media, text)
media_list: List[dict] = [] media_list: List[dict] = []
allowed_medias = {"image", "gifv", "video", "audio", "unknown"} allowed_medias = {"image", "gifv", "video", "audio", "unknown"}
unused: List[Attachment] = []
for attachment in media: for attachment in media:
if attachment.type not in allowed_medias:
continue
if attachment.type not in MEDIA_COMPATIBILITY: if attachment.type not in MEDIA_COMPATIBILITY:
logger.warning( logger.warning(
"attachment %r is not in %r", "attachment %r is not in %r",
@ -174,7 +173,13 @@ class TelegramIntegration(BaseIntegration):
MEDIA_COMPATIBILITY, MEDIA_COMPATIBILITY,
) )
continue continue
if attachment.type not in allowed_medias:
unused.append(attachment)
continue
allowed_medias &= MEDIA_COMPATIBILITY[attachment.type] allowed_medias &= MEDIA_COMPATIBILITY[attachment.type]
media_list.append( media_list.append(
{ {
"type": MEDIA_MAPPING[attachment.type], "type": MEDIA_MAPPING[attachment.type],
@ -186,6 +191,7 @@ class TelegramIntegration(BaseIntegration):
), ),
} }
) )
if len(media_list) == 1: if len(media_list) == 1:
media_list[0].update( media_list[0].update(
{ {
@ -194,13 +200,16 @@ class TelegramIntegration(BaseIntegration):
} }
) )
return await self._tg_request( return (
client, await self._tg_request(
"sendMediaGroup", client,
disable_notification=self.silent, "sendMediaGroup",
disable_web_page_preview=True, disable_notification=self.silent,
chat_id=self.chat_id, disable_web_page_preview=True,
media=media_list, chat_id=self.chat_id,
media=media_list,
),
unused,
) )
async def _post_poll( async def _post_poll(
@ -243,12 +252,15 @@ class TelegramIntegration(BaseIntegration):
).ok and res.result is not None: ).ok and res.result is not None:
ids.append(res.result["message_id"]) ids.append(res.result["message_id"])
else: else:
if ( pending, i = source.media_attachments, 0
res := await self._post_mediagroup( while len(pending) > 0 and i < 5:
client, text, source.media_attachments, has_spoiler res, left = await self._post_mediagroup(
client, text if i == 0 else "", pending, has_spoiler
) )
).ok and res.result is not None: if res.ok and res.result is not None:
ids.append(res.result["message_id"]) ids.extend([msg["message_id"] for msg in res.result])
pending = left
i += 1
if source.poll: if source.poll:
if ( if (