-
Notifications
You must be signed in to change notification settings - Fork 0
/
emoji_shortlist.py
75 lines (65 loc) · 2.29 KB
/
emoji_shortlist.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
"""
prints my favorite emojis
* fetch all emojis
* filter by shortcode
* max string length: 7
* must be in dictionary:
"""
import json
import requests
from pathlib import Path
import pandas as pd
def is_in_dict(word):
print(f'looking up {word}')
url = f'https://api.dictionaryapi.dev/api/v2/entries/en/{word}'
r = requests.get(url)
if r.status_code in (200, 404):
json_data = r.json()
if isinstance(json_data, list):
return True
if isinstance(json_data, dict):
return False
raise RuntimeError(f'dict request status code: {r.status_code}')
def fetch_emojis():
fname = 'emojis.json'
if not Path(fname).exists():
url = 'https://gist.githubusercontent.com/'
url += 'oliveratgithub/0bf11a9aff0d6da7b46f1490f86a71eb/'
url += 'raw/d8e4b78cfe66862cf3809443c1dba017f37b61db/emojis.json'
r = requests.get(url)
# for chunk in r.iter_content(chunk_size=512 * 1024):
if r.status_code != 200:
raise RuntimeError(f'emoji request status code: {r.status_code}')
f = open(fname, 'wb')
for chunk in r.iter_content(chunk_size=512 * 1024):
if chunk:
f.write(chunk)
f.close()
return json.loads(Path(fname).read_text())['emojis']
outfile = 'emojis.pkl'
if Path(outfile).exists():
print('loading cached shortname lookups')
df_emojis = pd.read_pickle(outfile)
else:
emojis = fetch_emojis()
df_emojis = pd.DataFrame(emojis)
# filter by length
df_emojis = df_emojis[df_emojis.shortname.str.len().isin(range(6,10))]
# check if shortname in dictionary
print('looking up shortnames')
df_emojis['realword'] = (
df_emojis
.shortname
.str.replace(':', '')
# .apply(lambda x: is_in_dict(x))
)
# df_emojis.to_pickle(outfile)
df_emojis = df_emojis[df_emojis.realword]
selected_columns = ['emoji', 'shortname', 'category']
print(df_emojis[selected_columns].to_markdown())
df_emojis['escaped'] = df_emojis.shortname.apply(lambda x: f'`{x}`')
selected_columns = ['emoji', 'shortname', 'escaped']
emojis_md_str = df_emojis[selected_columns].to_markdown(index=False)
outfile_md = 'emojis.md'
print(f'writing markdown file: {outfile_md}')
Path(outfile_md).write_text(emojis_md_str)