2020-10-04 13:15:27 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-05-15 17:11:45 +00:00
|
|
|
|
2020-10-04 13:15:27 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2021-04-18 15:06:47 +00:00
|
|
|
from pathlib import Path
|
2020-10-04 13:15:27 +00:00
|
|
|
from configparser import ConfigParser
|
2020-10-14 18:38:41 +00:00
|
|
|
import json
|
2021-04-18 15:06:47 +00:00
|
|
|
from typing import Any, Generator, Optional, Union
|
2021-01-16 17:23:14 +00:00
|
|
|
import urllib.parse
|
2021-03-18 18:25:15 +00:00
|
|
|
import urllib.request
|
2020-05-15 17:11:45 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
|
2020-11-22 21:33:44 +00:00
|
|
|
sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "script-resources"))
|
2021-04-02 18:50:17 +00:00
|
|
|
import common_script_utils
|
2020-05-15 17:11:45 +00:00
|
|
|
|
|
|
|
|
2021-01-03 19:22:34 +00:00
|
|
|
DEFAULT_REGISTRY_DUMP_URL = "https://stronghold.crosscode.ru/~ccbot/emote-registry.json"
|
2020-10-14 18:38:41 +00:00
|
|
|
|
2020-10-04 13:15:27 +00:00
|
|
|
if os.name == "posix":
|
2021-04-26 07:20:57 +00:00
|
|
|
config_path: Path = (common_script_utils.DOTFILES_CONFIG_DIR / "copy-crosscode-emoji-url.ini")
|
|
|
|
default_registry_dump_file: Path = (common_script_utils.DOTFILES_CACHE_DIR / "dotfiles")
|
2020-10-04 13:15:27 +00:00
|
|
|
else:
|
2021-04-26 07:20:57 +00:00
|
|
|
common_script_utils.platform_not_supported_error()
|
2020-10-04 13:15:27 +00:00
|
|
|
config = ConfigParser(interpolation=None)
|
|
|
|
config.read(config_path)
|
2020-05-15 17:11:45 +00:00
|
|
|
|
2021-04-18 15:06:47 +00:00
|
|
|
emotes: list[dict[str, Any]] = []
|
2020-10-04 13:15:27 +00:00
|
|
|
|
|
|
|
|
2021-04-18 15:06:47 +00:00
|
|
|
def emote_downloader_and_iterator() -> Generator[str, None, None]:
|
2021-04-26 07:20:57 +00:00
|
|
|
global emotes
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
registry_dump_file: Optional[Union[
|
|
|
|
str, Path]] = 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
|
2020-10-14 18:38:41 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
registry_dump_url = config.get(
|
|
|
|
"default", "ccbot_emote_registry_dump_url", fallback=DEFAULT_REGISTRY_DUMP_URL
|
|
|
|
)
|
2020-10-14 18:38:41 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
emote_registry_data: dict[str, Any]
|
|
|
|
try:
|
|
|
|
with open(registry_dump_file, "r") as f:
|
|
|
|
emote_registry_data = json.load(f)
|
|
|
|
except FileNotFoundError:
|
|
|
|
with urllib.request.urlopen(registry_dump_url, timeout=10) as response:
|
|
|
|
emote_registry_data = json.load(response)
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
assert emote_registry_data["version"] == 1
|
2020-10-04 13:15:27 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
emotes = [emote for emote in emote_registry_data["list"] if emote["safe"]]
|
2020-12-06 22:01:56 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
for emote in emotes:
|
|
|
|
yield "{emote[ref]} [{emote[guild_name]}]".format(emote=emote)
|
2020-10-04 13:15:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
chosen_index = common_script_utils.run_chooser(
|
2021-04-26 07:20:57 +00:00
|
|
|
emote_downloader_and_iterator(), prompt="emote", async_read=True
|
2020-10-04 13:15:27 +00:00
|
|
|
)
|
2021-04-11 21:53:51 +00:00
|
|
|
if chosen_index >= 0:
|
2021-04-26 07:20:57 +00:00
|
|
|
chosen_emote = emotes[chosen_index]
|
2020-11-22 21:33:44 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
emote_url: urllib.parse.ParseResult = urllib.parse.urlparse(chosen_emote["url"])
|
|
|
|
emote_url_query: dict[str, list[str]] = urllib.parse.parse_qs(emote_url.query)
|
2021-01-16 17:23:14 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
if config.getboolean("default", "add_emote_name_to_url", fallback=False):
|
|
|
|
emote_url_query["name"] = [chosen_emote["name"]]
|
2021-01-18 09:18:34 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
default_emote_image_size = config.getint("default", "default_emote_image_size", fallback=None)
|
|
|
|
if default_emote_image_size is not None:
|
|
|
|
emote_url_query["size"] = [str(default_emote_image_size)]
|
2021-01-16 17:23:14 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
emote_url_query_str = urllib.parse.urlencode(emote_url_query, doseq=True)
|
|
|
|
emote_url_str = urllib.parse.urlunparse(emote_url._replace(query=emote_url_query_str))
|
2020-11-22 21:33:44 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
common_script_utils.set_clipboard(emote_url_str)
|
2020-11-22 21:33:44 +00:00
|
|
|
|
2021-04-26 07:20:57 +00:00
|
|
|
common_script_utils.send_notification(
|
|
|
|
os.path.basename(__file__),
|
|
|
|
"copied URL of {} to clipboard!".format(chosen_emote["ref"]),
|
|
|
|
)
|