Skip to content

Commit 522e45a

Browse files
committed
Store IDs as strings and keep all emoticons. Fixes element-hq#12
1 parent c106c99 commit 522e45a

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

import.py

+21-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from telethon import TelegramClient
1919
from telethon.tl.functions.messages import GetAllStickersRequest, GetStickerSetRequest
2020
from telethon.tl.types.messages import AllStickers
21-
from telethon.tl.types import InputStickerSetShortName, Document
21+
from telethon.tl.types import InputStickerSetShortName, Document, DocumentAttributeSticker
2222
from telethon.tl.types.messages import StickerSet as StickerSetFull
2323

2424
parser = argparse.ArgumentParser()
@@ -71,6 +71,9 @@ async def upload(data: bytes, mimetype: str, filename: str) -> str:
7171

7272

7373
if TYPE_CHECKING:
74+
from typing import TypedDict
75+
76+
7477
class MatrixMediaInfo(TypedDict):
7578
w: int
7679
h: int
@@ -110,8 +113,12 @@ async def reupload_document(client: TelegramClient, document: Document) -> 'Matr
110113
else:
111114
width = int(width / (height / 256))
112115
height = 256
116+
body = ""
117+
for attr in document.attributes:
118+
if isinstance(attr, DocumentAttributeSticker):
119+
body = attr.alt
113120
return {
114-
"body": "",
121+
"body": body,
115122
"url": mxc,
116123
"info": {
117124
"w": width,
@@ -163,7 +170,7 @@ async def reupload_pack(client: TelegramClient, pack: StickerSetFull) -> None:
163170
try:
164171
with open(pack_path) as pack_file:
165172
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
167174
for sticker in existing_pack["stickers"]}
168175
print(f"Found {len(already_uploaded)} already reuploaded stickers")
169176
except FileNotFoundError:
@@ -176,26 +183,26 @@ async def reupload_pack(client: TelegramClient, pack: StickerSetFull) -> None:
176183
print(f"Skipped reuploading {document.id}")
177184
except KeyError:
178185
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+
}
179194

180195
for sticker in pack.packs:
181196
for document_id in sticker.documents:
182197
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)
192199

193200
with open(pack_path, "w") as pack_file:
194201
json.dump({
195202
"title": pack.set.title,
196203
"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),
199206
"stickers": list(reuploaded_documents.values()),
200207
}, pack_file, ensure_ascii=False)
201208
print(f"Saved {pack.set.title} as {pack.set.short_name}.json")

0 commit comments

Comments
 (0)