Script: emojize.py

Convert emoji shortcodes to unicode.
Author: Thom Wiggers — Version: 1.0.1 — License: CC0
For WeeChat ≥ 1.3, requires: emoji.
Tags: emoji, irc, py3
Added: 2021-04-11 — Updated: 2023-10-06

Download GitHub Repository

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Weechat plugin to convert emoji shortcodes to unicode emoji.

This plugin is a thin wrapper around the emoji package for python.
It converts emoji shortcodes to Unicode emoji.

This package is based on the emoji_aliases.py script by Mike Reinhardt.

License: CC0
Author: Thom Wiggers <thom@thomwiggers.nl>
Repository: https://github.com/thomwiggers/weechat-emojize

This plugin supports python 3 and requires the 'emoji' python package.
Requires at least weechat 1.3

Changelog:
    1.0.1 - 2023-08-06: mva
        Adaptation to modern version of `emoji` package
        (use_aliases => language="alias")

"""


def register():
    weechat.register(
        "emojize",
        "Thom Wiggers",
        "1.0.1",
        "CC0",
        "Convert emoji shortcodes to unicode emoji",
        "",  # shutdown function
        "utf-8",
    )


import_ok = True

try:
    import emoji
except ImportError:
    print("Failed to import emoji package, try installing 'emoji'")
    import_ok = False

import weechat


HOOKS = (
    "away",
    "cnotice",
    "cprivmsg",
    "kick",
    "knock",
    "notice",
    "part",
    "privmsg",
    "quit",
    "wallops",
)


def convert_emoji(_data, modifier, _modifier_data, string):
    """Convert the emoji in event messages"""
    # Check if this message has a segment we shouldn't touch.
    msg = weechat.info_get_hashtable("irc_message_parse", {"message": string})
    pos_text = int(msg["pos_text"])
    if msg["text"] != "" and pos_text > 0:
        return (
            string[:pos_text]
            + emoji.emojize(msg["text"], language="alias")
            + string[(pos_text + len(msg["text"])):]
        )

    if modifier == "input_text_for_buffer":
        return emoji.emojize(string, language="alias")

    return string


if __name__ == "__main__" and import_ok:
    register()
    weechat.hook_modifier("input_text_for_buffer", "convert_emoji", "")
    for hook in HOOKS:
        weechat.hook_modifier("irc_in2_{}".format(hook), "convert_emoji", "")