-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrename_emojis.py
More file actions
64 lines (48 loc) · 1.56 KB
/
rename_emojis.py
File metadata and controls
64 lines (48 loc) · 1.56 KB
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
"""
Emojis are downloaded from https://www.emojione.com/download
The emoji module is installed with pip
"""
import json
import shutil
from pathlib import Path
import emoji
def get_emoji_from_unicode_list(unicodes):
return ''.join(chr(int(code, 16)) for code in unicodes)
def get_country_from_emoji(emoji_):
try:
alias = emoji.UNICODE_EMOJI_ALIAS[emoji_]
except KeyError:
return None
if 'flag_for' not in alias:
return None
country = emoji.demojize(emoji_).strip(':')
return country
def beautify_country(country):
country = country.replace('_', ' ')
country = country.replace('&', 'and')
country = country.replace('’', "'")
return country
def main():
SIZE = 128
input_dir = Path(f'/tmp/EmojiOne_3.1.1_{SIZE}x{SIZE}_png')
output_dir = input_dir.parent / 'assets'
emojis_dir = output_dir / 'emoji-flags'
output_list_path = output_dir / 'countries.json'
if output_dir.is_dir():
shutil.rmtree(output_dir)
output_dir.mkdir()
emojis_dir.mkdir()
names = {}
for png_path in input_dir.glob('*.png'):
unicodes = png_path.stem.split('-')
decoded = get_emoji_from_unicode_list(unicodes)
country = get_country_from_emoji(decoded)
if country is None: continue
filename = f'{country}.png'
dst = emojis_dir / filename
shutil.copyfile(png_path, dst)
names[beautify_country(country)] = filename
with open(output_list_path, 'w') as outfile:
json.dump(names, outfile, indent=2)
if __name__ == '__main__':
main()