Compare commits

...

3 Commits

Author SHA1 Message Date
Jakob Neufeld 543120007b
Merge f4af9f679a into dbc3a9fbb8 2024-06-05 12:06:13 +02:00
Tulir Asokan dbc3a9fbb8 Don't prompt for giphy api key by default 2024-06-05 13:02:34 +03:00
Jakob Neufeld f4af9f679a
Added animated telegram sticker support for sticker-import 2023-08-20 22:12:51 +02:00
6 changed files with 33 additions and 24 deletions

View File

@ -55,23 +55,10 @@ async def load_config(path: str) -> None:
config = json.load(config_file) config = json.load(config_file)
homeserver_url = config["homeserver"] homeserver_url = config["homeserver"]
access_token = config["access_token"] access_token = config["access_token"]
try:
giphy_api_key = config["giphy_api_key"]
giphy_mxc_prefix = config["giphy_mxc_prefix"]
except KeyError:
# these two are not mandatory, assume GIF search is disabled
print("Giphy related parameters not found in the config file.")
except FileNotFoundError: except FileNotFoundError:
print("Matrix config file not found. Please enter your homeserver and access token.") print("Matrix config file not found. Please enter your homeserver and access token.")
homeserver_url = input("Homeserver URL: ") homeserver_url = input("Homeserver URL: ")
access_token = input("Access token: ") access_token = input("Access token: ")
print("If you want to enable GIF search, enter your giphy API key. Otherwise, leave it empty.")
giphy_api_key = input("Giphy API key: ").strip()
giphy_mxc_prefix = "mxc://giphy.mau.dev/"
if giphy_api_key:
print("If you want to self-host the matrix->giphy proxy, enter the mxc URI prefix here")
print("Defaults to mxc://giphy.mau.dev/ if left empty.")
giphy_mxc_prefix = input("Giphy MXC prefix: ").strip() or giphy_mxc_prefix
whoami_url = URL(homeserver_url) / "_matrix" / "client" / "r0" / "account" / "whoami" whoami_url = URL(homeserver_url) / "_matrix" / "client" / "r0" / "account" / "whoami"
if whoami_url.scheme not in ("https", "http"): if whoami_url.scheme not in ("https", "http"):
whoami_url = whoami_url.with_scheme("https") whoami_url = whoami_url.with_scheme("https")

View File

@ -57,7 +57,7 @@ def add_to_index(name: str, output_dir: str) -> None:
def make_sticker(mxc: str, width: int, height: int, size: int, def make_sticker(mxc: str, width: int, height: int, size: int,
body: str = "") -> matrix.StickerInfo: body: str = "", mimetype: str = "image/png") -> matrix.StickerInfo:
return { return {
"body": body, "body": body,
"url": mxc, "url": mxc,
@ -65,7 +65,7 @@ def make_sticker(mxc: str, width: int, height: int, size: int,
"w": width, "w": width,
"h": height, "h": height,
"size": size, "size": size,
"mimetype": "image/png", "mimetype": mimetype,
# Element iOS compatibility hack # Element iOS compatibility hack
"thumbnail_url": mxc, "thumbnail_url": mxc,
@ -73,7 +73,7 @@ def make_sticker(mxc: str, width: int, height: int, size: int,
"w": width, "w": width,
"h": height, "h": height,
"size": size, "size": size,
"mimetype": "image/png", "mimetype": mimetype,
}, },
}, },
"msgtype": "m.sticker", "msgtype": "m.sticker",

View File

@ -19,13 +19,13 @@ import asyncio
import os.path import os.path
import json import json
import re import re
import os
from telethon import TelegramClient from telethon import TelegramClient
from telethon.tl.functions.messages import GetAllStickersRequest, GetStickerSetRequest from telethon.tl.functions.messages import GetAllStickersRequest, GetStickerSetRequest
from telethon.tl.types.messages import AllStickers from telethon.tl.types.messages import AllStickers
from telethon.tl.types import InputStickerSetShortName, Document, DocumentAttributeSticker from telethon.tl.types import InputStickerSetShortName, Document, DocumentAttributeSticker
from telethon.tl.types.messages import StickerSet as StickerSetFull from telethon.tl.types.messages import StickerSet as StickerSetFull
from subprocess import run
from .lib import matrix, util from .lib import matrix, util
@ -39,6 +39,19 @@ async def reupload_document(client: TelegramClient, document: Document) -> matri
print(".", flush=True) print(".", flush=True)
return util.make_sticker(mxc, width, height, len(data)) return util.make_sticker(mxc, width, height, len(data))
async def reupload_document_gif(client: TelegramClient, document: Document) -> matrix.StickerInfo:
print(f"Reuploading {document.id}", end="", flush=True)
data = await client.download_media(document, file=bytes)
print(".", end="", flush=True)
# run LottieConverter
convert_data = run(['lottieconverter', '-', '-', 'gif', '512x512', '60'],capture_output = True, input=data).stdout
print(".", end="", flush=True)
mxc = await matrix.upload(convert_data, "image/gif", f"{document.id}.gif")
print(".", flush=True)
# 512x512 is mandatory for all stickers
return util.make_sticker(mxc, 512, 512, len(data), mimetype="image/gif")
def add_meta(document: Document, info: matrix.StickerInfo, pack: StickerSetFull) -> None: def add_meta(document: Document, info: matrix.StickerInfo, pack: StickerSetFull) -> None:
for attr in document.attributes: for attr in document.attributes:
@ -57,8 +70,7 @@ def add_meta(document: Document, info: matrix.StickerInfo, pack: StickerSetFull)
async def reupload_pack(client: TelegramClient, pack: StickerSetFull, output_dir: str) -> None: async def reupload_pack(client: TelegramClient, pack: StickerSetFull, output_dir: str) -> None:
if pack.set.animated: if pack.set.animated:
print("Animated stickerpacks are currently not supported") print("Warning, to convert animated stickers, you need lottieconverter installed")
return
pack_path = os.path.join(output_dir, f"{pack.set.short_name}.json") pack_path = os.path.join(output_dir, f"{pack.set.short_name}.json")
try: try:
@ -85,6 +97,9 @@ async def reupload_pack(client: TelegramClient, pack: StickerSetFull, output_dir
reuploaded_documents[document.id] = already_uploaded[document.id] reuploaded_documents[document.id] = already_uploaded[document.id]
print(f"Skipped reuploading {document.id}") print(f"Skipped reuploading {document.id}")
except KeyError: except KeyError:
if pack.set.animated:
reuploaded_documents[document.id] = await reupload_document_gif(client,document)
else:
reuploaded_documents[document.id] = await reupload_document(client, document) reuploaded_documents[document.id] = await reupload_document(client, document)
# Always ensure the body and telegram metadata is correct # Always ensure the body and telegram metadata is correct
add_meta(document, reuploaded_documents[document.id], pack) add_meta(document, reuploaded_documents[document.id], pack)

View File

@ -1 +1,6 @@
from .get_version import git_tag, git_revision, version, linkified_version # Generated in setup.py
git_tag = None
git_revision = 'f59406a4'
version = '0.1.0+dev.f59406a4'
linkified_version = '0.1.0+dev.[f59406a4](https://github.com/maunium/stickerpicker/commit/f59406a47a6778cd402e656ffb64f667335f665a)'

View File

@ -3,7 +3,7 @@ import * as widgetAPI from "./widget-api.js";
import {SearchBox} from "./search-box.js"; import {SearchBox} from "./search-box.js";
const GIPHY_SEARCH_DEBOUNCE = 1000 const GIPHY_SEARCH_DEBOUNCE = 1000
let GIPHY_API_KEY = "" let GIPHY_API_KEY = "HQku8974Uq5MZn3MZns46kXn2R4GDm75"
let GIPHY_MXC_PREFIX = "mxc://giphy.mau.dev/" let GIPHY_MXC_PREFIX = "mxc://giphy.mau.dev/"
export function giphyIsEnabled() { export function giphyIsEnabled() {

View File

@ -166,7 +166,9 @@ class App extends Component {
} }
const indexData = await indexRes.json() const indexData = await indexRes.json()
HOMESERVER_URL = indexData.homeserver_url || HOMESERVER_URL HOMESERVER_URL = indexData.homeserver_url || HOMESERVER_URL
if (indexData.giphy_api_key !== undefined) {
setGiphyAPIKey(indexData.giphy_api_key, indexData.giphy_mxc_prefix) setGiphyAPIKey(indexData.giphy_api_key, indexData.giphy_mxc_prefix)
}
// TODO only load pack metadata when scrolled into view? // TODO only load pack metadata when scrolled into view?
for (const packFile of indexData.packs) { for (const packFile of indexData.packs) {
let packRes let packRes