Skip to content

Commit

Permalink
feat: support caption replace
Browse files Browse the repository at this point in the history
  • Loading branch information
tangyoha committed Nov 30, 2023
1 parent 8c034d5 commit bf78b32
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- plus
tags:
- 'v*'

Expand Down Expand Up @@ -75,6 +76,4 @@ jobs:
push: true
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/386,linux/ppc64le
target: runtime-image
tags: |
${{ secrets.DOCKER_HUB_USERNAME }}/telegram_media_downloader:latest
${{ secrets.DOCKER_HUB_USERNAME }}/telegram_media_downloader:${{ github.ref_name }}
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/telegram_media_downloader:${{ github.ref_name }}
16 changes: 16 additions & 0 deletions module/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ def __init__(

self.caption_replace_dict: yaml.comments.CommentedMap = {}
self.default_forward_caption = None
self.caption_regex_replace_dict: yaml.comments.CommentedMap = {}
self.default_forward_additional_caption = None
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)

Expand Down Expand Up @@ -566,6 +568,20 @@ def assign_config(self, _config: dict) -> bool:
_config, "default_forward_caption", self.default_forward_caption, str
)

self.caption_regex_replace_dict = get_config(
_config,
"caption_replace_regex",
self.caption_regex_replace_dict,
yaml.comments.CommentedMap,
)

self.default_forward_additional_caption = get_config(
_config,
"default_forward_additional_caption",
self.default_forward_additional_caption,
str,
)

if _config.get("chat"):
chat = _config["chat"]
for item in chat:
Expand Down
29 changes: 26 additions & 3 deletions module/pyrogram_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import os
import re
import secrets
import struct
import time
Expand Down Expand Up @@ -107,7 +108,11 @@ def get_media_obj(


def replace_caption(
caption: Optional[str], caption_replace_dict, default_caption: Optional[str] = None
caption: Optional[str],
caption_replace_dict,
default_caption: Optional[str] = None,
caption_regex_replace_dict=None,
default_additional_caption: Optional[str] = None,
):
"""
Replaces certain items in a caption string
Expand All @@ -125,6 +130,16 @@ def replace_caption(
caption = caption.replace(item, caption_replace_dict[item])
else:
caption = default_caption

if not caption:
return default_additional_caption

if caption_regex_replace_dict:
for item in caption_regex_replace_dict:
caption = re.sub(item, caption_regex_replace_dict[item], caption)

if default_additional_caption:
caption += default_additional_caption
return caption


Expand Down Expand Up @@ -368,7 +383,11 @@ async def _upload_signal_message(
)

caption = replace_caption(
message.caption, app.caption_replace_dict, app.default_forward_caption
message.caption,
app.caption_replace_dict,
app.default_forward_caption,
app.caption_regex_replace_dict,
app.default_forward_additional_caption,
)
if message.video:
# Download thumbnail
Expand Down Expand Up @@ -499,7 +518,11 @@ async def forward_multi_media(
caption = app.get_caption_name(node.chat_id, message.media_group_id)

caption = replace_caption(
caption, app.caption_replace_dict, app.default_forward_caption
caption,
app.caption_replace_dict,
app.default_forward_caption,
app.caption_regex_replace_dict,
app.default_forward_additional_caption,
)

media_obj = get_media_obj(message, file_name, caption)
Expand Down

0 comments on commit bf78b32

Please sign in to comment.