You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
6.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. # maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from typing import Dict
  17. import argparse
  18. import asyncio
  19. import os.path
  20. import json
  21. import re
  22. from telethon import TelegramClient
  23. from telethon.tl.functions.messages import GetAllStickersRequest, GetStickerSetRequest
  24. from telethon.tl.types.messages import AllStickers
  25. from telethon.tl.types import InputStickerSetShortName, Document, DocumentAttributeSticker
  26. from telethon.tl.types.messages import StickerSet as StickerSetFull
  27. from .lib import matrix, util
  28. async def reupload_document(client: TelegramClient, document: Document) -> matrix.StickerInfo:
  29. print(f"Reuploading {document.id}", end="", flush=True)
  30. data = await client.download_media(document, file=bytes)
  31. print(".", end="", flush=True)
  32. data, width, height = util.convert_image(data)
  33. print(".", end="", flush=True)
  34. mxc = await matrix.upload(data, "image/png", f"{document.id}.png")
  35. print(".", flush=True)
  36. return util.make_sticker(mxc, width, height, len(data))
  37. def add_meta(document: Document, info: matrix.StickerInfo, pack: StickerSetFull) -> None:
  38. for attr in document.attributes:
  39. if isinstance(attr, DocumentAttributeSticker):
  40. info["body"] = attr.alt
  41. info["id"] = f"tg-{document.id}"
  42. info["net.maunium.telegram.sticker"] = {
  43. "pack": {
  44. "id": str(pack.set.id),
  45. "short_name": pack.set.short_name,
  46. },
  47. "id": str(document.id),
  48. "emoticons": [],
  49. }
  50. async def reupload_pack(client: TelegramClient, pack: StickerSetFull, output_dir: str) -> None:
  51. if pack.set.animated:
  52. print("Animated stickerpacks are currently not supported")
  53. return
  54. pack_path = os.path.join(output_dir, f"{pack.set.short_name}.json")
  55. try:
  56. os.mkdir(os.path.dirname(pack_path))
  57. except FileExistsError:
  58. pass
  59. print(f"Reuploading {pack.set.title} with {pack.set.count} stickers "
  60. f"and writing output to {pack_path}")
  61. already_uploaded = {}
  62. try:
  63. with open(pack_path) as pack_file:
  64. existing_pack = json.load(pack_file)
  65. already_uploaded = {int(sticker["net.maunium.telegram.sticker"]["id"]): sticker
  66. for sticker in existing_pack["stickers"]}
  67. print(f"Found {len(already_uploaded)} already reuploaded stickers")
  68. except FileNotFoundError:
  69. pass
  70. reuploaded_documents: Dict[int, matrix.StickerInfo] = {}
  71. for document in pack.documents:
  72. try:
  73. reuploaded_documents[document.id] = already_uploaded[document.id]
  74. print(f"Skipped reuploading {document.id}")
  75. except KeyError:
  76. reuploaded_documents[document.id] = await reupload_document(client, document)
  77. # Always ensure the body and telegram metadata is correct
  78. add_meta(document, reuploaded_documents[document.id], pack)
  79. for sticker in pack.packs:
  80. if not sticker.emoticon:
  81. continue
  82. for document_id in sticker.documents:
  83. doc = reuploaded_documents[document_id]
  84. # If there was no sticker metadata, use the first emoji we find
  85. if doc["body"] == "":
  86. doc["body"] = sticker.emoticon
  87. doc["net.maunium.telegram.sticker"]["emoticons"].append(sticker.emoticon)
  88. with open(pack_path, "w") as pack_file:
  89. json.dump({
  90. "title": pack.set.title,
  91. "id": f"tg-{pack.set.id}",
  92. "net.maunium.telegram.pack": {
  93. "short_name": pack.set.short_name,
  94. "hash": str(pack.set.hash),
  95. },
  96. "stickers": list(reuploaded_documents.values()),
  97. }, pack_file, ensure_ascii=False)
  98. print(f"Saved {pack.set.title} as {pack.set.short_name}.json")
  99. util.add_to_index(os.path.basename(pack_path), output_dir)
  100. pack_url_regex = re.compile(r"^(?:(?:https?://)?(?:t|telegram)\.(?:me|dog)/addstickers/)?"
  101. r"([A-Za-z0-9-_]+)"
  102. r"(?:\.json)?$")
  103. parser = argparse.ArgumentParser()
  104. parser.add_argument("--list", help="List your saved sticker packs", action="store_true")
  105. parser.add_argument("--session", help="Telethon session file name", default="sticker-import")
  106. parser.add_argument("--config",
  107. help="Path to JSON file with Matrix homeserver and access_token",
  108. type=str, default="config.json")
  109. parser.add_argument("--output-dir", help="Directory to write packs to", default="web/packs/",
  110. type=str)
  111. parser.add_argument("pack", help="Sticker pack URLs to import", action="append", nargs="*")
  112. async def main(args: argparse.Namespace) -> None:
  113. await matrix.load_config(args.config)
  114. client = TelegramClient(args.session, 298751, "cb676d6bae20553c9996996a8f52b4d7")
  115. await client.start()
  116. if args.list:
  117. stickers: AllStickers = await client(GetAllStickersRequest(hash=0))
  118. index = 1
  119. width = len(str(stickers.sets))
  120. print("Your saved sticker packs:")
  121. for saved_pack in stickers.sets:
  122. print(f"{index:>{width}}. {saved_pack.title} "
  123. f"(t.me/addstickers/{saved_pack.short_name})")
  124. elif args.pack[0]:
  125. input_packs = []
  126. for pack_url in args.pack[0]:
  127. match = pack_url_regex.match(pack_url)
  128. if not match:
  129. print(f"'{pack_url}' doesn't look like a sticker pack URL")
  130. return
  131. input_packs.append(InputStickerSetShortName(short_name=match.group(1)))
  132. for input_pack in input_packs:
  133. pack: StickerSetFull = await client(GetStickerSetRequest(input_pack))
  134. await reupload_pack(client, pack, args.output_dir)
  135. else:
  136. parser.print_help()
  137. await client.disconnect()
  138. def cmd() -> None:
  139. asyncio.get_event_loop().run_until_complete(main(parser.parse_args()))
  140. if __name__ == "__main__":
  141. cmd()