#!/usr/bin/env python3 import sys import os from configparser import ConfigParser import requests import json sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "script-resources")) import common_script_utils # noqa: E402 DEFAULT_REGISTRY_DUMP_URL = ( "https://stronghold.crosscode.ru/~crosscodebot/emote-registry.json" ) if os.name == "posix": config_path = ( common_script_utils.DOTFILES_CONFIG_DIR / "copy-crosscode-emoji-url.ini" ) default_registry_dump_file = common_script_utils.DOTFILES_CACHE_DIR / "dotfiles" else: common_script_utils.platform_not_supported_error() config = ConfigParser(interpolation=None) config.read(config_path) emotes = [] def emote_downloader_and_iterator(): global emotes registry_dump_file = config.get( "default", "ccbot_emote_registry_dump_file", fallback=None ) if registry_dump_file is not None: registry_dump_file = os.path.expanduser(registry_dump_file) else: registry_dump_file = default_registry_dump_file registry_dump_url = config.get( "default", "ccbot_emote_registry_dump_url", fallback=DEFAULT_REGISTRY_DUMP_URL ) try: with open(registry_dump_file, "r") as f: emote_registry_data = json.load(f) except FileNotFoundError: response = requests.get(registry_dump_url, timeout=10) response.raise_for_status() emote_registry_data = response.json() assert emote_registry_data["version"] == 1 emotes = [emote for emote in emote_registry_data["list"] if emote["safe"]] for emote in emotes: yield "{emote[ref]} [{emote[guild_name]}]".format(emote=emote) chosen_index = common_script_utils.run_chooser( emote_downloader_and_iterator(), prompt="emote", async_read=True ) chosen_emote = emotes[chosen_index] emote_url = chosen_emote["url"] default_emote_image_size = config.getint( "default", "default_emote_image_size", fallback=None ) if default_emote_image_size is not None: emote_url += "?size={}".format(default_emote_image_size) common_script_utils.set_clipboard(emote_url) common_script_utils.send_notification( os.path.basename(__file__), "copied URL of {} to clipboard!".format(chosen_emote["ref"]), )