Skip to content

Commit

Permalink
Update dependencies, compatibility with telegram api 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
laggykiller committed Apr 12, 2024
1 parent a273537 commit c1a7495
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 87 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/check_and_fix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ jobs:
run: curl -o src/sticker_convert/resources/emoji.json -L https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json
- name: mypy
run: mypy
- name: pyright
run: pyright
# Temporarily disabled due to Pillow typing issue
# https://github.com/python/typeshed/issues/11688
# - name: pyright
# run: pyright
- name: Ruff check
run: ruff check
- name: Ruff format
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ imagequant~=1.1.1
memory-tempfile~=2.2.3
mergedeep~=1.3.4
numpy>=1.22.4
Pillow==10.3.0
Pillow~=10.3.0
pyoxipng~=9.0.0
python-telegram-bot~=20.5
python-telegram-bot~=21.1
requests~=2.31.0
rlottie_python~=1.3.4
signalstickers-client~=3.3.0
signalstickers-client-fork-laggykiller~=3.3.0.post1
sqlcipher3-wheels~=0.5.2.post1
tqdm~=4.66.2
ttkbootstrap-fork-laggykiller~=1.5.1
Expand Down
14 changes: 7 additions & 7 deletions src/sticker_convert/downloaders/download_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from typing import Dict, Optional

import anyio
from signalstickers_client import StickersClient # type: ignore
from signalstickers_client.errors import SignalException # type: ignore
from signalstickers_client.models import StickerPack # type: ignore
from signalstickers_client.errors import SignalException
from signalstickers_client.models import StickerPack
from signalstickers_client.stickersclient import StickersClient

from sticker_convert.downloaders.download_base import DownloadBase
from sticker_convert.job_option import CredOption
Expand All @@ -21,7 +21,7 @@ class DownloadSignal(DownloadBase):
@staticmethod
async def get_pack(pack_id: str, pack_key: str) -> StickerPack:
async with StickersClient() as client:
pack = await client.get_pack(pack_id, pack_key) # type: ignore
pack = await client.get_pack(pack_id, pack_key)

return pack

Expand All @@ -32,14 +32,14 @@ def save_stickers(self, pack: StickerPack) -> None:
None,
{
"set_progress_mode": "determinate",
"steps": len(pack.stickers), # type: ignore
"steps": len(pack.stickers),
},
)
)

emoji_dict: Dict[str, str] = {}
for sticker in pack.stickers: # type: ignore
f_id = str(sticker.id).zfill(3) # type: ignore
for sticker in pack.stickers:
f_id = str(sticker.id).zfill(3)
f_path = Path(self.out_dir, f_id)
with open(f_path, "wb") as f:
f.write(sticker.image_data) # type: ignore
Expand Down
20 changes: 10 additions & 10 deletions src/sticker_convert/uploaders/upload_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import Any, Dict, List

import anyio
from signalstickers_client import StickersClient # type: ignore
from signalstickers_client.errors import SignalException # type: ignore
from signalstickers_client.models import LocalStickerPack, Sticker # type: ignore
from signalstickers_client.errors import SignalException
from signalstickers_client.models import LocalStickerPack, Sticker
from signalstickers_client.stickersclient import StickersClient

from sticker_convert.converter import StickerConvert
from sticker_convert.job_option import CompOption, CredOption, OutputOption
Expand Down Expand Up @@ -39,7 +39,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
@staticmethod
async def upload_pack(pack: LocalStickerPack, uuid: str, password: str) -> str:
async with StickersClient(uuid, password) as client:
pack_id, pack_key = await client.upload_pack(pack) # type: ignore
pack_id, pack_key = await client.upload_pack(pack)

result = (
f"https://signal.art/addstickers/#pack_id={pack_id}&pack_key={pack_key}"
Expand All @@ -53,15 +53,15 @@ def add_stickers_to_pack(
self.cb.put(f"Verifying {src} for uploading to signal")

sticker = Sticker()
sticker.id = pack.nb_stickers # type: ignore
sticker.id = pack.nb_stickers

emoji = emoji_dict.get(Path(src).stem, None)
if not emoji:
self.cb.put(
f"Warning: Cannot find emoji for file {Path(src).name}, skip uploading this file..."
)
continue
sticker.emoji = emoji[:1] # type: ignore
sticker.emoji = emoji[:1]

if Path(src).suffix == ".webp":
spec_choice = self.webp_spec
Expand All @@ -84,10 +84,10 @@ def add_stickers_to_pack(

assert isinstance(image_data, bytes)

sticker.image_data = image_data # type: ignore
sticker.image_data = image_data
else:
with open(src, "rb") as f:
sticker.image_data = f.read() # type: ignore
sticker.image_data = f.read()

pack._addsticker(sticker) # type: ignore

Expand Down Expand Up @@ -140,8 +140,8 @@ def upload_stickers_signal(self) -> List[str]:
)
for pack_title, stickers in packs.items():
pack = LocalStickerPack()
pack.title = pack_title # type: ignore
pack.author = author # type: ignore
pack.title = pack_title
pack.author = author

self.add_stickers_to_pack(pack, stickers, emoji_dict)
self.cb.put(f"Uploading pack {pack_title}")
Expand Down
155 changes: 90 additions & 65 deletions src/sticker_convert/uploaders/upload_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import copy
import re
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union, cast
from typing import Any, Dict, List, Optional, Union, cast

import anyio
from telegram import InputSticker, Sticker
Expand Down Expand Up @@ -96,7 +96,7 @@ async def upload_pack(
"[^0-9a-zA-Z]+", "_", pack_short_name
) # name used in url, only alphanum and underscore only

sticker_set = None
sticker_set: Any = None
try:
sticker_set = await bot.get_sticker_set(
pack_short_name,
Expand Down Expand Up @@ -152,11 +152,10 @@ async def upload_pack(
else:
sticker_type = Sticker.REGULAR

input_stickers: List[Tuple[Path, InputSticker]] = []
init_input_stickers: List[InputSticker] = []
sticker_format = None
cover_spec_choice = None
ext = None
for src in stickers:
sticker_format_prev = None
for count, src in enumerate(stickers):
self.cb.put(f"Verifying {src} for uploading to telegram")

emoji = emoji_dict.get(Path(src).stem, None)
Expand All @@ -175,16 +174,13 @@ async def upload_pack(
ext = Path(src).suffix
if ext == ".tgs":
spec_choice = self.tgs_spec
cover_spec_choice = self.tgs_cover_spec
sticker_format = "animated"
elif ext == ".webm":
spec_choice = self.webm_spec
cover_spec_choice = self.webm_cover_spec
sticker_format = "video"
else:
ext = ".png"
spec_choice = self.png_spec
cover_spec_choice = self.png_cover_spec
sticker_format = "static"

if self.opt_output.option == "telegram_emoji":
Expand All @@ -203,81 +199,98 @@ async def upload_pack(
)
sticker_bytes = cast(bytes, convert_result)

input_stickers.append(
(src, InputSticker(sticker=sticker_bytes, emoji_list=emoji_list))
input_sticker = InputSticker(
sticker=sticker_bytes,
emoji_list=emoji_list,
format=sticker_format,
)

if sticker_set is None:
if count < 50 and (
sticker_format_prev is None
or sticker_format_prev == sticker_format
):
init_input_stickers.append(input_sticker)
else:
start_msg = f"Creating pack and bulk uploading {count} stickers with same format of {pack_short_name}"
finish_msg = f"Created pack and bulk uploaded {count} stickers with same format of {pack_short_name}"
error_msg = f"Cannot create pack and bulk upload {count} stickers with same format of {pack_short_name} due to"
self.cb.put(start_msg)
try:
await bot.create_new_sticker_set(
user_id=self.telegram_userid,
name=pack_short_name,
title=pack_title,
stickers=init_input_stickers,
sticker_type=sticker_type,
)
sticker_set = True
self.cb.put(finish_msg)
except TelegramError as e:
self.cb.put(f"{error_msg} {e}")
return None
else:
try:
# We could use tg.start_soon() here
# But this would disrupt the order of stickers
await bot.add_sticker_to_set(
user_id=self.telegram_userid,
name=pack_short_name,
sticker=input_sticker,
)
self.cb.put(f"Uploaded sticker {src} of {pack_short_name}")
except BadRequest as e:
self.cb.put(
f"Cannot upload sticker {src} of {pack_short_name} due to {e}"
)
if str(e) == "Stickerpack_not_found":
self.cb.put(
"Hint: You might had deleted and recreated pack too quickly. Wait about 3 minutes and try again."
)
except TelegramError as e:
self.cb.put(
f"Cannot upload sticker {src} of {pack_short_name} due to {e}"
)

sticker_format_prev = sticker_format

cover_path = MetadataHandler.get_cover(self.opt_output.dir)
thumbnail_bytes: Union[None, bytes, Path] = None
if cover_path and cover_spec_choice and ext:
if cover_path:
thumbnail_bytes: Union[None, bytes, Path] = None
cover_ext = Path(cover_path).suffix

if cover_ext == ".tgs":
thumbnail_format = "animated"
cover_spec_choice = self.tgs_cover_spec
elif cover_ext == ".webm":
thumbnail_format = "video"
cover_spec_choice = self.webm_cover_spec
else:
cover_ext = ".png"
thumbnail_format = "static"
cover_spec_choice = self.png_cover_spec

if FormatVerify.check_file(cover_path, spec=cover_spec_choice):
with open(cover_path, "rb") as f:
thumbnail_bytes = f.read()
else:
_, _, thumbnail_bytes, _ = StickerConvert.convert(
cover_path,
Path(f"bytes{ext}"),
Path(f"bytes{cover_ext}"),
self.opt_comp_cover_merged,
self.cb,
self.cb_return,
)

if sticker_set is None and sticker_format is not None:
if len(input_stickers) > 50:
amount_str = "first 50"
else:
amount_str = "all"
start_msg = f"Creating pack and bulk uploading {amount_str} stickers of {pack_short_name}"
finish_msg = f"Created pack and bulk uploaded {amount_str} stickers of {pack_short_name}"
error_msg = f"Cannot create pack and bulk upload {amount_str} stickers of {pack_short_name} due to"
self.cb.put(start_msg)
try:
await bot.create_new_sticker_set(
user_id=self.opt_cred.telegram_userid,
name=pack_short_name,
title=pack_title,
stickers=[a for _, a in input_stickers[:50]],
sticker_format=sticker_format,
sticker_type=sticker_type,
)
self.cb.put(finish_msg)
input_stickers = input_stickers[50:]
except TelegramError as e:
self.cb.put(f"{error_msg} {e}")
return None

for src, sticker in input_stickers:
try:
# We could use tg.start_soon() here
# But this would disrupt the order of stickers
await bot.add_sticker_to_set(
user_id=self.opt_cred.telegram_userid,
name=pack_short_name,
sticker=sticker,
)
self.cb.put(f"Uploaded sticker {src} of {pack_short_name}")
except BadRequest as e:
self.cb.put(
f"Cannot upload sticker {src} of {pack_short_name} due to {e}"
)
if str(e) == "Stickerpack_not_found":
self.cb.put(
"Hint: You might had deleted and recreated pack too quickly. Wait about 3 minutes and try again."
)
except TelegramError as e:
self.cb.put(
f"Cannot upload sticker {src} of {pack_short_name} due to {e}"
)

if thumbnail_bytes is not None:
try:
self.cb.put(
f"Uploading cover (thumbnail) of pack {pack_short_name}"
)
await bot.set_sticker_set_thumbnail(
name=pack_short_name,
user_id=self.opt_cred.telegram_userid,
user_id=self.telegram_userid,
thumbnail=thumbnail_bytes,
format=thumbnail_format,
)
self.cb.put(f"Uploaded cover (thumbnail) of pack {pack_short_name}")
except TelegramError as e:
Expand All @@ -300,6 +313,12 @@ def upload_stickers_telegram(self) -> List[str]:
self.cb.put("Token and userid required for uploading to telegram")
return urls

if self.opt_cred.telegram_userid.isnumeric():
self.telegram_userid = int(self.opt_cred.telegram_userid)
else:
self.cb.put("Invalid userid, should contain numbers only")
return urls

title, _, emoji_dict = MetadataHandler.get_metadata(
self.opt_output.dir,
title=self.opt_output.title,
Expand Down Expand Up @@ -327,11 +346,17 @@ def upload_stickers_telegram(self) -> List[str]:
)

assert title is not None

if self.opt_output.option == "telegram_emoji":
file_per_pack = 200
else:
file_per_pack = 120

packs = MetadataHandler.split_sticker_packs(
self.opt_output.dir,
title=title,
file_per_anim_pack=50,
file_per_image_pack=120,
file_per_anim_pack=file_per_pack,
file_per_image_pack=file_per_pack,
separate_image_anim=not self.opt_comp.fake_vid,
)

Expand Down

0 comments on commit c1a7495

Please sign in to comment.