-
Notifications
You must be signed in to change notification settings - Fork 5
/
bottom.py
45 lines (32 loc) · 1.03 KB
/
bottom.py
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
CHARACTER_VALUES = {
200: "🫂",
50: "💖",
10: "✨",
5: "🥺",
1: ",",
0: "❤️"
}
SECTION_SEPERATOR = '👉👈'
def to_bottom(text: str) -> str:
out = bytearray()
for char in text.encode():
while char != 0:
for value, emoji in CHARACTER_VALUES.items():
if char >= value:
char -= value
out += emoji.encode()
break
out += SECTION_SEPERATOR.encode()
return out.decode('utf-8')
def from_bottom(text: str) -> str:
out = bytearray()
text = text.strip().removesuffix(SECTION_SEPERATOR)
if not all(c in CHARACTER_VALUES.values() for c in text.replace(SECTION_SEPERATOR, '')):
raise TypeError(f'Invalid bottom text: {text}')
for char in text.split(SECTION_SEPERATOR):
rev_mapping = {v: k for k, v in CHARACTER_VALUES.items()}
sub = 0
for emoji in char:
sub += rev_mapping[emoji]
out += sub.to_bytes(1, 'big')
return out.decode()