18
18
from telethon import TelegramClient
19
19
from telethon .tl .functions .messages import GetAllStickersRequest , GetStickerSetRequest
20
20
from telethon .tl .types .messages import AllStickers
21
- from telethon .tl .types import InputStickerSetShortName , Document
21
+ from telethon .tl .types import InputStickerSetShortName , Document , DocumentAttributeSticker
22
22
from telethon .tl .types .messages import StickerSet as StickerSetFull
23
23
24
24
parser = argparse .ArgumentParser ()
@@ -71,6 +71,9 @@ async def upload(data: bytes, mimetype: str, filename: str) -> str:
71
71
72
72
73
73
if TYPE_CHECKING :
74
+ from typing import TypedDict
75
+
76
+
74
77
class MatrixMediaInfo (TypedDict ):
75
78
w : int
76
79
h : int
@@ -110,8 +113,12 @@ async def reupload_document(client: TelegramClient, document: Document) -> 'Matr
110
113
else :
111
114
width = int (width / (height / 256 ))
112
115
height = 256
116
+ body = ""
117
+ for attr in document .attributes :
118
+ if isinstance (attr , DocumentAttributeSticker ):
119
+ body = attr .alt
113
120
return {
114
- "body" : "" ,
121
+ "body" : body ,
115
122
"url" : mxc ,
116
123
"info" : {
117
124
"w" : width ,
@@ -163,7 +170,7 @@ async def reupload_pack(client: TelegramClient, pack: StickerSetFull) -> None:
163
170
try :
164
171
with open (pack_path ) as pack_file :
165
172
existing_pack = json .load (pack_file )
166
- already_uploaded = {sticker ["net.maunium.telegram.sticker" ]["id" ]: sticker
173
+ already_uploaded = {int ( sticker ["net.maunium.telegram.sticker" ]["id" ]) : sticker
167
174
for sticker in existing_pack ["stickers" ]}
168
175
print (f"Found { len (already_uploaded )} already reuploaded stickers" )
169
176
except FileNotFoundError :
@@ -176,26 +183,26 @@ async def reupload_pack(client: TelegramClient, pack: StickerSetFull) -> None:
176
183
print (f"Skipped reuploading { document .id } " )
177
184
except KeyError :
178
185
reuploaded_documents [document .id ] = await reupload_document (client , document )
186
+ reuploaded_documents [document .id ]["net.maunium.telegram.sticker" ] = {
187
+ "pack" : {
188
+ "id" : str (pack .set .id ),
189
+ "short_name" : pack .set .short_name ,
190
+ },
191
+ "id" : str (document .id ),
192
+ "emoticons" : [],
193
+ }
179
194
180
195
for sticker in pack .packs :
181
196
for document_id in sticker .documents :
182
197
doc = reuploaded_documents [document_id ]
183
- doc ["body" ] = sticker .emoticon
184
- doc ["net.maunium.telegram.sticker" ] = {
185
- "pack" : {
186
- "id" : pack .set .id ,
187
- "short_name" : pack .set .short_name ,
188
- },
189
- "id" : document_id ,
190
- "emoticon" : sticker .emoticon ,
191
- }
198
+ doc ["net.maunium.telegram.sticker" ]["emoticons" ].append (sticker .emoticon )
192
199
193
200
with open (pack_path , "w" ) as pack_file :
194
201
json .dump ({
195
202
"title" : pack .set .title ,
196
203
"short_name" : pack .set .short_name ,
197
- "id" : pack .set .id ,
198
- "hash" : pack .set .hash ,
204
+ "id" : str ( pack .set .id ) ,
205
+ "hash" : str ( pack .set .hash ) ,
199
206
"stickers" : list (reuploaded_documents .values ()),
200
207
}, pack_file , ensure_ascii = False )
201
208
print (f"Saved { pack .set .title } as { pack .set .short_name } .json" )
0 commit comments