-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemoji.py
executable file
·272 lines (220 loc) · 8.84 KB
/
emoji.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python3
#
# parse Unicode's emoji data into db (just a json file for the moment)
#
import argparse
import collections
import datetime
import json
import os
import re
import shutil
import sys
import tempfile
import time
import urllib.parse
import urllib.request
default_output = os.path.abspath("./output")
datafiles = {
"test": "emoji-test.txt",
"data": "emoji-data.txt"
}
parser = argparse.ArgumentParser()
parser.add_argument("-q", "--quiet", help="hide status messages", default=True, dest='verbose', action="store_false")
parser.add_argument("--output", help="output directory (default=%s)" % default_output, action="store", default=default_output)
args = parser.parse_args()
if args.verbose:
print("INFO: emoji.py starting at %s" % datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
def to_hex(i):
if i > 0xFFFF:
return "%x" % i
else:
return "%04x" % i
def to_handle(s):
return re.sub(r'[^a-z0-9]+', '_', s.strip().lower()).strip('_')
emojis = collections.OrderedDict()
#
# emoji-test.txt
#
line_pattern = re.compile("([A-F0-9 ]+);([-a-z ]+)# ([^ ]+) E([^ ]+) (.*)$")
filename = datafiles["test"]
sys.stdout.write("INFO: processing file '%s'" % filename)
f = open(os.path.join(args.output, filename), mode='r', encoding='utf-8')
line_count = 0
emoji_count = 0
for rawline in f:
line_count += 1
if line_count % 100 == 0:
sys.stdout.write(".")
line = rawline.strip()
if len(line) == 0 or line[0] == '#':
continue
emoji_count += 1
matcher = line_pattern.search(line)
if not matcher:
sys.stdout.write("\nERROR: no match on line %d ('%s')" % (line_count, line))
continue
codepoint = matcher.group(1).replace(' FE0F', '').strip().lower().replace(' ', '_')
if codepoint in emojis:
emoji = emojis[codepoint]
else:
emoji = {}
emoji['codepoints'] = [ matcher.group(1).replace(' FE0F', '').strip().lower() ]
if ' FEOF' in matcher.group(1):
emoji['codepoints'].append(matcher.group(1).strip().lower())
#emoji['status'] = matcher.group(2).strip()
#emoji['chars'] = matcher.group(3)
emoji['version'] = matcher.group(4)
emoji['names'] = [ matcher.group(5) ]
emoji['handles'] = [ to_handle(matcher.group(5)) ]
emojis[codepoint] = emoji
if " FEOF" in matcher.group(1):
emoji['fully-qualified'] = matcher.group(1).strip().lower().replace(' ', '_')
f.close()
sys.stdout.write("\n")
sys.stdout.write("INFO: complete %d lines processed\n" % line_count)
sys.stdout.write("INFO: complete %d emoji processed\n" % emoji_count)
sys.stdout.write("INFO: total emoji: %d\n" % len(emojis))
#
# emoji-data.txt - standalone (single-codepoint) emoji
#
line_pattern = re.compile("([.A-F0-9 ]+);([-A-Za-z_ ]+)# +([^ ]+) (.*)$")
filename = datafiles["data"]
sys.stdout.write("INFO: processing file '%s'" % filename)
f = open(os.path.join(args.output, filename), mode='r', encoding='utf-8')
line_count = 0
emoji_count = 0
new_count = 0
for rawline in f:
line_count += 1
if line_count % 100 == 0:
sys.stdout.write(".")
line = rawline.strip()
if len(line) == 0 or line[0] == '#':
continue
matcher = line_pattern.search(line)
if not matcher:
sys.stdout.write("\nERROR: no match on line %d ('%s')" % (line_count, line))
continue
#if '<reserved-' in matcher.group(4):
# continue
if matcher.group(3).startswith("E0.0"):
continue
str = matcher.group(1).strip()
if ".." not in str:
codepoints = [ str.lower() ]
else:
codepoints = []
split = str.split("..")
for loop in range(int(split[0], 16), int(split[1], 16)+1):
codepoints.append(to_hex(loop))
for codepoint in codepoints:
emoji_count += 1
if codepoint in emojis:
emoji = emojis[codepoint]
else:
#if args.verbose:
# sys.stdout.write("\nDEBUG: new emoji codepoint '%s'\n" % codepoint)
new_count += 1
emoji = {}
emoji['codepoints'] = codepoint
emoji['chars'] = chr(int(codepoint, 16))
emoji['status'] = "component-only"
emojis[codepoint] = emoji
if 'property' not in emoji:
emoji['property'] = {}
emoji['property'][matcher.group(2).strip()] = True
emoji['version'] = matcher.group(3).strip()
f.close()
sys.stdout.write("\n")
sys.stdout.write("INFO: complete %d lines processed\n" % line_count)
sys.stdout.write("INFO: complete %d emoji processed\n" % emoji_count)
sys.stdout.write("INFO: complete %d emoji added\n" % new_count)
sys.stdout.write("INFO: total emoji: %d\n" % len(emojis))
# WTF: why does my own static website return 403 for the default user-agent?!?!?
req = urllib.request.Request("https://emoji.fileformat.info/emoji-images.json", None, {'User-Agent': 'FileFormatInfo/1.0'})
try:
response = urllib.request.urlopen(req)
except urllib.error.URLError as e:
print(e)
images = json.loads(response.read())
sys.stdout.write("INFO: %d images\n" % len(images))
for image in images.keys():
if image not in emojis:
sys.stdout.write("WARNING: no emoji for %s\n" % image)
for emoji in emojis.keys():
if emoji not in images:
sys.stdout.write("WARNING: no image for %s\n" % emoji)
else:
emojis[emoji]['image'] = True
#
# hack for missing 20e3
#
#emojis['20e3'] = { "codepoints": "20e3", 'chars': chr(0x20e3), 'status': 'component-only', 'text': 'combining enclosing keycap' }
#
# link non-fully-qualified to their parents
#
if False:
count = 0
for key in emojis.keys():
if emojis[key]["status"] == "fully-qualified" and "_fe0f" in key:
unqualified = key.replace("_fe0f", "", 1)
if unqualified in emojis:
count = count + 1
#sys.stdout.write("DEBUG: %s -> %s (1)\n" % (unqualified, key))
emojis[unqualified]['fully-qualified'] = key
else:
sys.stdout.write("WARNING: no unqualified found for %s" % unqualified)
# multiple instance of FE0F, so need to map with missing only 2nd instance, or missing both instances
if "_fe0f" in unqualified:
unqualified = key.replace("_fe0f", "")
if unqualified in emojis:
count = count + 1
sys.stdout.write("DEBUG: %s -> %s (both)\n" % (unqualified, key))
emojis[unqualified]['fully-qualified'] = key
else:
sys.stdout.write("WARNING: no unqualified found for %s" % unqualifed)
unqualified = key[0:-5] # HACK, but it is always at the end
if unqualified in emojis:
count = count + 1
sys.stdout.write("DEBUG: %s -> %s (2)\n" % (unqualified, key))
emojis[unqualified]['fully-qualified'] = key
else:
sys.stdout.write("WARNING: no unqualified found for %s" % unqualifed)
#sys.stdout.write("\n")
sys.stdout.write("INFO: %d not-fully-qualified emojis mapped\n" % count)
if False:
count = 0
for key in emojis.keys():
if emojis[key]["status"] == "non-fully-qualified":
if "fully-qualified" not in emojis[key]:
count = count + 1
sys.stdout.write("ERROR: no fully qualified version of %s\n" % key)
if count > 0:
sys.stdout.write("ERROR: %d non-fully-qualified emoji remain unmapped\n" % count)
sys.exit(5)
else:
sys.stdout.write("INFO: all non-fully-qualified emoji are mapped\n")
filename = "emoji.json"
sys.stdout.write("INFO: saving to file '%s'\n" % filename)
f = open(os.path.join(args.output, filename), mode='w', encoding='utf-8')
f.write(json.dumps(emojis, ensure_ascii=False, sort_keys=False, indent=2, separators=(',', ': ')))
f.close()
sys.stdout.write("INFO: save complete: %d emoji\n" % len(emojis))
if False:
normalize = {}
for key in emojis.keys():
if emojis[key]["status"] == "non-fully-qualified":
normalize[key] = emojis[key]['fully-qualified']
elif emojis[key]["status"] == "component-only":
normalize[key] = key
else:
normalize[key] = key
filename = "normalize.json"
sys.stdout.write("INFO: saving to file '%s'\n" % filename)
f = open(os.path.join(args.output, filename), mode='w', encoding='utf-8')
f.write(json.dumps(normalize, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': ')))
f.close()
sys.stdout.write("INFO: save complete: %d emoji\n" % len(emojis))
if args.verbose:
print("INFO: unicode emoji data update complete at %s" % datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))