-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_workflow.py
119 lines (91 loc) · 2.67 KB
/
make_workflow.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# coding=utf-8
import os
import json
import base64
from bs4 import BeautifulSoup
import lxml
# Get the Emoji spec
with open('raw-emojis.json', 'r', encoding='utf-8') as f:
emojis = json.load(f)
# Get the emoji library
with open('emojilib.json', 'r', encoding='utf-8') as f:
keyword_library = json.load(f)
with open('unicode-emoji.json', 'r', encoding='utf-8') as f:
library = json.load(f)
def match_emoji(char):
if char == '*\u20e3':
char = "*\ufe0f\u20e3"
if emojis.get(char):
return emojis[char]
return emojis[char.rstrip('\ufe0f')]
for char, library_info in library.items():
name = library_info['name']
keywords = keyword_library.get(char)
info = match_emoji(char)
info['short_name'] = name
info['keywords'] = keywords or []
# Trust emojilib more than Unicode
info['char'] = char
# Enrich with GitHub colon-codes
with open('codes.json') as f:
codes = json.load(f)
for code, char in codes.items():
info = match_emoji(char)
if 'code' in info:
info['code'] += " " + code
else:
info['code'] = code
# Write out icons
os.mkdir('icons')
def choose_filename(info):
if 'short_name' in info:
return info['short_name']
if 'code' in info:
return info['code'].strip(':')
return info['title'].replace(' ', '_')
seen_filenames = set()
for info in emojis.values():
filename = choose_filename(info)
while filename in seen_filenames:
filename = filename + '_'
seen_filenames.add(filename)
path = os.path.join('icons', filename + '.png')
info['path'] = path
with open(path, 'wb') as f:
png_bytes = base64.b64decode(info['png_base64'])
f.write(png_bytes)
del info['png_base64']
# Print out matches
def match(info):
if 'code' in info:
yield info['code'].replace(':', '') #.replace('_', ' ')
if 'short_name' in info:
yield info['short_name'].replace("_", " ")
for k in info['keywords']:
yield k.replace("_", " ")
else:
# for a in info['aliases']:
# yield a.replace("_", " ")
yield info['title']
yield info['char']
def dedupe(seq):
seen = set()
for x in seq:
if x in seen:
continue
yield x
seen.add(x)
def item(info):
return dict(
title = info['title'],
subtitle = info.get('code', ''),
icon = dict(
path = info['path'],
),
arg = info['char'],
match = " ".join(dedupe(" ".join(match(info)).split(" "))),
)
items = map(item, emojis.values())
json.dump(dict(
items = list(items),
), open('alfred-emoji.json', 'w'), ensure_ascii=False, indent=2)